如何设计GitHub Action在手动创建Release时上传build artefacts作为资产?
Upload Build Artifacts to Manually Created GitHub Releases via GitHub Action
I get exactly what you're facing—most examples focus on creating a Release from a tag via Actions, but you need to upload assets after manually creating the Release through the GitHub UI. Here's a straightforward solution that works perfectly for your scenario:
Core Idea
When you manually create and publish a Release on GitHub, it triggers the release event (specifically the published type). We can leverage this event to run a GitHub Action that builds your artifacts (if needed) and uploads them directly to the existing Release.
Step-by-Step Implementation
Create a Workflow File
In your repo, create a new file at.github/workflows/upload-release-assets.ymlwith the following content:name: Upload Release Assets on: release: types: [published] # Triggers when a Release is published (including manual ones) permissions: contents: write # Required to upload assets to the Release jobs: upload-assets: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 # Optional: Add your build steps here to generate artifacts # For example, if you're building a Node.js project: # - name: Install dependencies # run: npm install # - name: Build project # run: npm run build - name: Upload Release Assets uses: softprops/action-gh-release@v2 with: tag_name: ${{ github.event.release.tag_name }} files: | ./path/to/your/artifact1.zip ./path/to/your/artifact2.exe ./dist/**/* # Use glob patterns to match multiple files env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}Key Details Explained
- Trigger Event:
on: release: types: [published]ensures the Action runs every time you publish a Release (whether manual or automated). When you create a Release via the UI and hit "Publish release", this event fires immediately. - Permissions:
contents: writegrants the Action permission to modify repository contents, which is mandatory for uploading assets to the Release. - Upload Action:
softprops/action-gh-releaseis a well-maintained, popular tool that supports uploading assets to existing Releases. We use${{ github.event.release.tag_name }}to target the exact Release you just created manually. - File Patterns: The
filesfield accepts specific file paths or glob patterns (like./dist/**/*) to upload multiple artifacts in one go.
- Trigger Event:
Test It Out
- Commit and push this workflow file to your repo.
- Go to the GitHub UI, create a new Release (with a tag), fill in the release notes, and click "Publish release".
- Head over to the "Actions" tab in your repo—you should see the "Upload Release Assets" workflow running. Once it completes, your artifacts will appear in the Release's "Assets" section.
Notes
- If your artifacts are pre-built and stored elsewhere (not generated in the workflow), you can skip the build steps and directly point the
filesfield to their location (just ensure they're accessible in the workflow environment). - The
GITHUB_TOKENsecret is automatically provided by GitHub, so you don't need to create or configure it manually.
内容的提问来源于stack exchange,提问作者sdgfsdh




