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:
1on:2 release:3 types: [created]Assuming previous steps that create a file called app.zip in the root of the checkout, here’s the final action step which worked for me:
1 - name: Upload release attachment2 uses: actions/github-script@v43 with:4 script: |5 const fs = require('fs');6 const tag = context.ref.replace("refs/tags/", "");7 // Get release for this tag8 const release = await github.repos.getReleaseByTag({9 owner: context.repo.owner,10 repo: context.repo.repo,11 tag12 });13 // Upload the release asset14 await github.repos.uploadReleaseAsset({15 owner: context.repo.owner,16 repo: context.repo.repo,17 release_id: release.data.id,18 name: "app.zip",19 data: await fs.readFileSync("app.zip")20 });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. release.data.id will then be the numeric release ID.
My full workflow is at https://github.com/simonw/datasette-app/blob/0.1.0/.github/workflows/release.yml