simonw / datasette-app

The Datasette macOS application
https://datasette.io/desktop
121 stars 8 forks source link

GitHub Actions workflow for creating packages for releases #51

Closed simonw closed 3 years ago

simonw commented 3 years ago

Follows #20 and #50 - when I create a release in this GitHub repo it should attach the resulting .zip file to the release.

simonw commented 3 years ago

I'm going to call this release.yml.

It will be very similar to test.yml - https://github.com/simonw/datasette-app/blob/5c3c0ab193f779856a53d68f2c0934575a733de4/.github/workflows/test.yml - but will be triggered by a release and will attach something to the release instead of publishing an artifact.

simonw commented 3 years ago

Weird that GitHub have archived their official action for this: https://github.com/actions/upload-release-asset

simonw commented 3 years ago

From https://github.com/actions/upload-release-asset/blob/main/src/upload-release-asset.js it looks like I can do this using a actions/github-script step, as seen in https://github.com/simonw/datasette-plugin-template-repository/blob/7a0af5da0a4141c81b9866d585ccde954f06dd95/.github/workflows/cookiecutter.yml#L18-L43

simonw commented 3 years ago

Relevant docs: https://octokit.github.io/octokit.js/v18/#repos-upload-release-asset

simonw commented 3 years ago

Found a couple of examples using GitHub code search:

simonw commented 3 years ago

I'm going to figure this out in my https://github.com/simonw/playing-with-actions repo.

simonw commented 3 years ago

After a few iterations I managed to ship https://github.com/simonw/playing-with-actions/releases/tag/v4 using the code from https://github.com/simonw/playing-with-actions/blob/v4/.github/workflows/release.yml - in particular this script block:

    - uses: actions/github-script@v4
      name: Upload release attachment
      with:
        script: |
          const fs = require('fs');
          const tag = context.ref.replace("refs/tags/", "");
          console.log("tag = ", tag);
          // Get release for this tag
          const release = await github.repos.getReleaseByTag({
            owner: context.repo.owner,
            repo: context.repo.repo,
            tag
          });
          console.log("release = ", release);
          // Upload the release asset
          await github.repos.uploadReleaseAsset({
            owner: context.repo.owner,
            repo: context.repo.repo,
            release_id: release.data.id,
            name: "My.zip",
            data: await fs.readFileSync("My.zip")
          });
simonw commented 3 years ago

It worked! https://github.com/simonw/datasette-app/releases/tag/0.1.0

simonw commented 3 years ago

Wrote up a TIL: https://til.simonwillison.net/github-actions/attach-generated-file-to-release

And https://til.simonwillison.net/electron/sign-notarize-electron-macos