Skip to content

Commit

Permalink
build: add release workflows for packages
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Jul 19, 2024
1 parent c43d916 commit 327b295
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 59 deletions.
39 changes: 39 additions & 0 deletions .github/scripts/check-changed.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { execSync } from "node:child_process";
import * as fs from "node:fs";
import { resolve } from "node:path";

if (process.argv.length === 2) {
console.error("Expected one argument (directory)");
process.exit(1);
}

const packagePath = resolve(process.argv[2], "package.json");
if (!fs.existsSync(packagePath)) {
console.error(`Could not find package.json at ${packagePath}`);
process.exit(1);
}

const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));

const nightly = process.argv.length === 4 && process.argv[3] === "true";
const tag = nightly ? "next" : "latest";
const latest = execSync(`npm view ${packageJson.name} dist-tags.${tag}`)
.toString("utf8")
.trim();

let changed = false;
if (nightly) {
const hash = latest.split("-").pop();
const result = execSync(
`git diff --quiet ${hash} HEAD -- ${process.argv[2]} || echo changed`,
)
.toString("utf8")
.trim();
changed = result === "changed";
} else if (
packageJson.version.localeCompare(latest, undefined, { numeric: true }) === 1
) {
changed = true;
}

console.log(changed);
18 changes: 18 additions & 0 deletions .github/scripts/get-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as fs from "node:fs";
import { resolve } from "node:path";
import { getDevVersion } from "./util.mjs";

if (process.argv.length === 2) {
console.error("Expected one argument (directory)");
process.exit(1);
}

const packagePath = resolve(process.argv[2], "package.json");
if (!fs.existsSync(packagePath)) {
console.error(`Could not find package.json at ${packagePath}`);
process.exit(1);
}

const nightly = process.argv.length === 4 && process.argv[3] === "true";
const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
console.log(nightly ? getDevVersion(packageJson.version) : packageJson.version);
23 changes: 23 additions & 0 deletions .github/scripts/util.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { execSync } from "node:child_process";

export function getDevVersion(version) {
const [major, minor, patch] = version
.split(".")
.map((num) => Number.parseInt(num));

const commitHash = getCommitHash();
return `${major}.${minor}.${patch + 1}-dev-${commitHash.substring(0, 7)}`;
}

function getCommitHash() {
let commitHash;
if (
typeof process.env.GITHUB_SHA === "string" &&
process.env.GITHUB_SHA !== ""
) {
commitHash = process.env.GITHUB_SHA;
} else {
commitHash = execSync("git rev-parse HEAD").toString("utf8");
}
return commitHash;
}
59 changes: 0 additions & 59 deletions .github/workflows/publish-next.yml

This file was deleted.

77 changes: 77 additions & 0 deletions .github/workflows/release-core.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Release core

on:
workflow_dispatch:
schedule:
- cron: "0 7 * * *"
push:
branches:
- main
paths:
- packages/core/package.json

jobs:
check:
name: Check version
runs-on: ubuntu-latest
outputs:
version_changed: ${{ steps.check.outputs.version_changed }}
nightly: ${{steps.nightly.outputs.nightly }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Enable Corepack
run: corepack enable

- name: Check nightly status
id: nightly
run: echo "nightly=${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}" >> "$GITHUB_OUTPUT"

- name: Check version
id: check
run: |
CHANGED=$(node .github/scripts/check-changed.mjs packages/core ${{ steps.nightly.outputs.nightly }})
echo "version_changed=$CHANGED" >> "$GITHUB_OUTPUT"
publish:
runs-on: ubuntu-latest
needs: check
if: needs.check.outputs.version_changed == 'true'
env:
nightly: ${{ needs.check.outputs.nightly }}
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Enable Corepack
run: corepack enable

- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: "https://registry.npmjs.org"
cache: "yarn"

- name: Install dependencies
run: yarn install

- name: Set nightly version
if: env.nightly
run: |
VERSION=$(node .github/scripts/get-version.mjs @rbxts/centurion ${{ env.nightly }})
yarn workspace @rbxts/centurion version $VERSION
- name: Publish @latest
if: !env.nightly
run: yarn workspace @rbxts/centurion npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Publish @next
if: env.nightly
run: yarn workspace @rbxts/centurion npm publish --tag next
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
77 changes: 77 additions & 0 deletions .github/workflows/release-ui.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Release UI

on:
workflow_dispatch:
schedule:
- cron: "0 7 * * *"
push:
branches:
- main
paths:
- packages/ui/package.json

jobs:
check:
name: Check version
runs-on: ubuntu-latest
outputs:
version_changed: ${{ steps.check.outputs.version_changed }}
nightly: ${{steps.nightly.outputs.nightly }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Enable Corepack
run: corepack enable

- name: Check nightly status
id: nightly
run: echo "nightly=${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}" >> "$GITHUB_OUTPUT"

- name: Check version
id: check
run: |
CHANGED=$(node .github/scripts/check-changed.mjs packages/ui ${{ steps.nightly.outputs.nightly }})
echo "version_changed=$CHANGED" >> "$GITHUB_OUTPUT"
publish:
runs-on: ubuntu-latest
needs: check
if: needs.check.outputs.version_changed == 'true'
env:
nightly: ${{ needs.check.outputs.nightly }}
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Enable Corepack
run: corepack enable

- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: "https://registry.npmjs.org"
cache: "yarn"

- name: Install dependencies
run: yarn install

- name: Set nightly version
if: env.nightly
run: |
VERSION=$(node .github/scripts/get-version.mjs @rbxts/centurion-ui ${{ env.nightly }})
yarn workspace @rbxts/centurion-ui version $VERSION
- name: Publish @latest
if: !env.nightly
run: yarn workspace @rbxts/centurion-ui npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Publish @next
if: env.nightly
run: yarn workspace @rbxts/centurion-ui npm publish --tag next
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 comments on commit 327b295

Please sign in to comment.