home / tils / til

Menu
  • GraphQL API

til: github-actions_attach-generated-file-to-release.md

This data as json

path topic title url body html shot created created_utc updated updated_utc shot_hash slug
github-actions_attach-generated-file-to-release.md github-actions Attaching a generated file to a GitHub release using Actions https://github.com/simonw/til/blob/main/github-actions/attach-generated-file-to-release.md For [Datasette Desktop](https://github.com/simonw/datasette-app) 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: ```yaml on: release: 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: ```yaml - name: Upload release attachment uses: actions/github-script@v4 with: 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, tag }); // Upload the release asset await github.repos.uploadReleaseAsset({ owner: context.repo.owner, repo: context.repo.repo, release_id: release.data.id, name: "app.zip", data: await fs.readFileSync("app.zip") }); ``` It uses [actions/github-script](https://github.com/actions/github-script) which provides a pre-configured [octokit/rest.js](https://octokit.github.io/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 <p>For <a href="https://github.com/simonw/datasette-app">Datasette Desktop</a> 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.</p> <p>I triggered my action on the creation of a new release, like so:</p> <div class="highlight highlight-source-yaml"><pre><span class="pl-ent">on</span>: <span class="pl-ent">release</span>: <span class="pl-ent">types</span>: <span class="pl-s">[created]</span></pre></div> <p>Assuming previous steps that create a file called <code>app.zip</code> in the root of the checkout, here's the final action step which worked for me:</p> <div class="highlight highlight-source-yaml"><pre> - <span class="pl-ent">name</span>: <span class="pl-s">Upload release attachment</span> <span class="pl-ent">uses</span>: <span class="pl-s">actions/github-script@v4</span> <span class="pl-ent">with</span>: <span class="pl-ent">script</span>: <span class="pl-s">|</span> <span class="pl-s"> const fs = require('fs');</span> <span class="pl-s"> const tag = context.ref.replace("refs/tags/", "");</span> <span class="pl-s"> // Get release for this tag</span> <span class="pl-s"> const release = await github.repos.getReleaseByTag({</span> <span class="pl-s"> owner: context.repo.owner,</span> <span class="pl-s"> repo: context.repo.repo,</span> <span class="pl-s"> tag</span> <span class="pl-s"> });</span> <span class="pl-s"> // Upload the release asset</span> <span class="pl-s"> await github.repos.uploadReleaseAsset({</span> <span class="pl-s"> owner: context.repo.owner,</span> <span class="pl-s"> repo: context.repo.repo,</span> <span class="pl-s"> release_id: release.data.id,</span> <span class="pl-s"> name: "app.zip",</span> <span class="pl-s"> data: await fs.readFileSync("app.zip")</span> <span class="pl-s"> });</span></pre></div> <p>It uses <a href="https://github.com/actions/github-script">actions/github-script</a> which provides a pre-configured <a href="https://octokit.github.io/rest.js/" rel="nofollow">octokit/rest.js</a> client object.</p> <p>The <code>uploadReleaseAsset()</code> method needs the <code>owner</code>, <code>repo</code>, <code>release_id</code>, <code>name</code> (filename) and the file data.</p> <p>These are mostly available, with the exception of <code>release_id</code>. That can be derived for the current release based on the <code>context.ref</code> value - strip that down to just the tag, then use <code>getReleaseByTag()</code> to get a release object. <code>release.data.id</code> will then be the numeric release ID.</p> <p>My full workflow is at <a href="https://github.com/simonw/datasette-app/blob/0.1.0/.github/workflows/release.yml">https://github.com/simonw/datasette-app/blob/0.1.0/.github/workflows/release.yml</a></p> <Binary: 55,244 bytes> 2021-09-07T22:04:28-07:00 2021-09-08T05:04:28+00:00 2021-09-07T22:04:28-07:00 2021-09-08T05:04:28+00:00 d3d35e3e7c1982434b617edf7ebb8060 attach-generated-file-to-release
Powered by Datasette · How this site works · Code of conduct