diff --git a/.github/scripts/release.js b/.github/scripts/release.js new file mode 100644 index 0000000..287f80b --- /dev/null +++ b/.github/scripts/release.js @@ -0,0 +1,28 @@ +const path = require("path"); +const fs = require("fs").promises; +const crypto = require("crypto"); + +/** actions/github-script でのリリース成果物のアップロード */ +module.exports = async function ({ github, context, glob }) { + const version = context.payload.release.tag_name.replace(/^v/i, ""); + const target = { + ...context.repo, + release_id: context.payload.release.id, + }; + let body = "## Megabit OS\n"; + const globber = await glob.create("megabit-armhf.*.gz"); + for await (const file of globber.globGenerator()) { + const name = path + .basename(file) + .replace(/^megabit-armhf[.]/, `megabit-${version}-armhf.`); + const data = await fs.readFile(file); + await github.repos.uploadReleaseAsset({ ...target, name, data }); + const hash = crypto.createHash("sha256").update(data).digest("hex"); + body = `${body} +${name.split(".").includes("img") ? "OS イメージ" : "ファイルシステム"} + +- ${name} (SHA256: \`${hash}\`) +`; + } + await github.repos.updateRelease({ ...target, body, prerelease: false }); +}; diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d667a4d..7ddaa85 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,8 @@ name: build on: push: { branches: [main] } + release: + types: [published] jobs: main: runs-on: ubuntu-latest @@ -14,3 +16,16 @@ jobs: with: name: megabit-armhf path: megabit-armhf.* + release: + if: github.event.release.prerelease + needs: main + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/download-artifact@v2 + with: { name: megabit-armhf } + - uses: actions/github-script@v4 + with: + script: | + const script = require("./.github/scripts/release.js"); + await script({ github, context, glob });