关于GitHub Action中workflow_run场景下未使用download-artifact动作的疑问
workflow_run Example Uses github-script Instead of download-artifact Great question! Let me break down the key points you might have missed, and why both approaches have their place.
1. The Default Behavior of actions/download-artifact
The actions/download-artifact action, by default, only looks for artifacts generated by the current workflow run. But in your workflow_run scenario, you need to download artifacts from a different workflow (the "Upload data" run that triggered your current workflow).
Your simplified code is missing a critical parameter: run-id. Without it, the action will search for an artifact named pr_number in your current workflow's artifacts (which don't exist, since your workflow only downloads things). To make it work, you need to point it to the upstream workflow run ID:
on: workflow_run: workflows: [Upload data] types: - completed jobs: download: runs-on: ubuntu-latest steps: - uses: actions/download-artifact@v3 with: name: pr_number run-id: ${{ github.event.workflow_run.id }} # This is the key missing piece path: ${{ github.workspace }}
2. Why the Official Example Uses github-script
There are a few reasons the official docs use the more complex github-script approach:
- Backward compatibility: Earlier versions of
actions/download-artifactdidn't support therun-idparameter. Before this feature existed, the only way to download artifacts from another workflow run was to call the GitHub REST API directly. - Flexibility: The script approach lets you add custom logic—like checking if the artifact exists before downloading, filtering through multiple artifacts, or handling errors gracefully. If your "Upload data" workflow might generate multiple artifacts, or you need to handle edge cases (e.g., artifact not found), the script gives you more control.
- Educational purpose: The example also serves to demonstrate how to interact with the GitHub Actions API directly, which can be useful for more advanced workflows that aren't covered by pre-built actions.
3. Is Your Simplified Approach Valid?
Absolutely! Now that actions/download-artifact supports the run-id parameter, your simplified approach is the recommended way for most cases—it's cleaner and easier to maintain. The only time you'd need the github-script method is if you require custom logic that the pre-built action doesn't handle.
内容的提问来源于stack exchange,提问作者chae seongwoo

