Search code examples
githubgithub-actionsgithub-api

How to upload a release in GitHub Action using github-script action


Now that some of the standard GitHub actions have been archived and are no longer supported I need to create a new release and upload the artefacts using the GitHub Script action.

NOTE: I can't use actions that are not supported by GitHub.

I've got the release done and working, but now when trying to upload a release artefact I cannot find a way to upload the content of the release (which is a tarball).

The documentation for the API endpoint for uploading the release asset suggests the data should be a part of the input to the call rather than referencing a file.

How do I get the contents of the file into the data argument below:

- name: Upload Assets
  uses: "actions/github-script@v6"
  with:
    script: |
        try {
          return await github.rest.repos.uploadReleaseAsset({
            owner: context.repo.owner,
            repo: context.repo.repo,
            name: "release.tar.xz",
            release_id: ${{ fromJSON(steps.deploy.outputs.result).data.id }}
            data: 
          })
        } catch (error) {
          core.setFailed(error.message);
        }

Solution

  • A great way to create releases and other GitHub actions, is the GitHub CLI (gh).

    The GitHub workflow already has a GitHub token ${{ github.TOKEN }} and you can pass it to the env, the CLI will automatically pick it up.

    permissions:
      contents: write
    
    jobs:
      release:
        steps:
          - run: |
              gh release create v1.2.3 release.tar.xz
            env:
              GITHUB_TOKEN: ${{ github.TOKEN }}
            shell: bash
            name: Creates a release in GitHub and uploads attachments
    

    Note: For some reason, gh will do one call to see if the file is already uploaded, the another call to upload the file. So I've burned through the API rate limits using this approach trying to attach 500 files to a release. Normally you should be able to do 1000 API calls per hour across workflows.