Attaching a generated file to a GitHub release using Actions

For Datasette Desktop I wanted to run an action which, when I created a release, would build an asset for that release and then upload and attach it.

I triggered my action on the creation of a new release, like so:

    types: [created]

Assuming previous steps that create a file called in the root of the checkout, here's the final action step which worked for me:

      - name: Upload release attachment
        uses: actions/github-script@v4
          script: |
            const fs = require('fs');
            const tag = context.ref.replace("refs/tags/", "");
            // Get release for this tag
            const release = await github.repos.getReleaseByTag({
              owner: context.repo.owner,
              repo: context.repo.repo,
            // Upload the release asset
            await github.repos.uploadReleaseAsset({
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: "",
              data: await fs.readFileSync("")

It uses actions/github-script which provides a pre-configured octokit/rest.js client object.

The uploadReleaseAsset() method needs the owner, repo, release_id, name (filename) and the file data.

These are mostly available, with the exception of release_id. That can be derived for the current release based on the context.ref value - strip that down to just the tag, then use getReleaseByTag() to get a release object. will then be the numeric release ID.

My full workflow is at

Created 2021-09-07T22:04:28-07:00 · Edit