From 3e57519eac70e4942c5cff776d94553cb9bce140 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Tue, 19 Nov 2024 18:01:58 +0900 Subject: [PATCH] dev: add release flow --- .github/workflows/tag.yml | 19 +++++++++++++++++++ tools/release.mjs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .github/workflows/tag.yml create mode 100644 tools/release.mjs diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml new file mode 100644 index 0000000..c778483 --- /dev/null +++ b/.github/workflows/tag.yml @@ -0,0 +1,19 @@ +name: release +on: + push: + tags: + - "[0-9]+.[0-9]+.[0-9]+" +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: "22" + - run: node tools/release.mjs + env: + GITHUB_TOKEN: ${{ github.token }} diff --git a/tools/release.mjs b/tools/release.mjs new file mode 100644 index 0000000..d9b2bed --- /dev/null +++ b/tools/release.mjs @@ -0,0 +1,33 @@ +import fs from "node:fs"; +import cp from "node:child_process"; + +function praseRef(ref) { + const m = ref.match(/^refs\/tags\/(.+?)$/); + if (!m) throw new Error("no tags in ref: " + ref); + return m[1]; +} + +// GHAでGHPRにnpmリリースする +async function main() { + let ref = process.env["GITHUB_REF"]; + let token = process.env["GITHUB_TOKEN"]; + if (!ref) throw new Error("no github ref"); + if (!token) throw new Error("no github token"); + const tag = praseRef(ref); + const packageJson = JSON.parse(await fs.promises.readFile("package.json")); + packageJson["version"] = tag; + await fs.promises.writeFile("package.json", JSON.stringify(packageJson)); + const npmRc = "//npm.pkg.github.com/:_authToken=" + token; + await fs.promises.writeFile(".npmrc", npmRc); + const p = cp.spawn("npm", ["publish"]); + await new Promise((resolve, reject) => { + p.stderr.pipe(process.stderr); + p.stdout.pipe(process.stdout); + p.on("close", (code) => { + code === 0 ? resolve() : reject(code); + }); + }); + console.log(`📦 package ${tag} released!`); +} + +main();