Skip to content

Commit

Permalink
dev: add release flow
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Nov 19, 2024
1 parent 3ecbb2e commit 3e57519
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -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 }}
33 changes: 33 additions & 0 deletions tools/release.mjs
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 3e57519

Please sign in to comment.