Search code examples
githubgithub-actions

upload_url for GitHub's upload-release-asset action when the trigger is a release?


I am trying to create a GitHub Action that is triggered when a release is published and will build my project and upload certain artefacts as release assets. The only examples I can find rely on using the create-release action, which fails as the release has already been created. My workflow file is like so:

name: Build for Release

on:
  release:
    types: [published]

jobs:
  build:

    runs-on: macos-latest

    steps:
    - uses: actions/checkout@v2
      
    - name: Build
      run: ...
      
    - name: Zip Asset
      run: ...

    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ ???? What Goes Here ???? }}
        asset_path: build/MyProject.zip
        asset_name: MyProject.zip
        asset_content_type: application/zip

My tags will be v0.1.0, v0.1.1, v0.2.0 etc and the release name matches these.


Solution

  • You can access it on the event data in the github context:

    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
    

    See also Example data for the release event.