Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/trim-actions-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@selftune/desktop": patch
---

Cancel superseded pull-request CI runs and check macOS desktop packages on merge and release. Documentation-only pull requests skip heavy CI jobs while retaining required check names.
76 changes: 76 additions & 0 deletions .github/scripts/ci-policy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { execFileSync } from "node:child_process";
import { appendFileSync, readFileSync } from "node:fs";
import { pathToFileURL } from "node:url";

const desktopTargets = [
{
os: "macos-15",
platform: "mac",
target: "bun-darwin-arm64",
package_script: "package:mac",
arch_flag: "--arm64",
artifact: "selftune-desktop-macos-arm64",
},
{
os: "macos-15-intel",
platform: "mac",
target: "bun-darwin-x64",
package_script: "package:mac",
arch_flag: "--x64",
artifact: "selftune-desktop-macos-x64",
},
{
os: "ubuntu-latest",
platform: "linux",
target: "bun-linux-x64",
package_script: "package:linux",
arch_flag: "--x64",
artifact: "selftune-desktop-linux-x64",
},
{
os: "windows-latest",
platform: "win",
target: "bun-windows-x64",
package_script: "package:win",
arch_flag: "--x64",
artifact: "selftune-desktop-windows-x64",
},
];

export function isDocsOnly(paths) {
return (
paths.length > 0 && paths.every((path) => path.endsWith(".md") || path.startsWith("docs/"))
);
}

export function selectPolicy(eventName, paths) {
return {
run_heavy: eventName !== "pull_request" || !isDocsOnly(paths),
desktop_matrix: {
include: desktopTargets.filter(
(target) => eventName !== "pull_request" || target.platform !== "mac",
),
},
};
}

export function changedPaths(event, cwd = process.cwd()) {
const base = event.pull_request?.base?.sha;
if (!/^[a-f0-9]{40}$/i.test(base ?? "")) throw new Error("Expected a full PR base commit SHA");
return execFileSync("git", ["diff", "--name-only", "--no-renames", "-z", base, "HEAD"], {
cwd,
encoding: "utf8",
maxBuffer: 32 * 1024 * 1024,
})
.split("\0")
.filter(Boolean);
}

if (process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url) {
const eventName = process.env.GITHUB_EVENT_NAME;
const event = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, "utf8"));
const policy = selectPolicy(eventName, eventName === "pull_request" ? changedPaths(event) : []);
const output = `run_heavy=${policy.run_heavy}\ndesktop_matrix=${JSON.stringify(policy.desktop_matrix)}\n`;
if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, output);
process.stdout.write(output);
}
79 changes: 79 additions & 0 deletions .github/scripts/ci-policy.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdtempSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { changedPaths, isDocsOnly, selectPolicy } from "./ci-policy.mjs";

for (const paths of [
["README.md"],
["apps/desktop/README.md"],
["docs/image.png", "docs/setup.md"],
]) {
test(`documentation skips heavy PR jobs: ${paths.join(", ")}`, () => {
assert.equal(isDocsOnly(paths), true);
assert.equal(selectPolicy("pull_request", paths).run_heavy, false);
});
}
for (const paths of [
[],
["package.json"],
["docs/setup.md", "apps/local/main.ts"],
[".github/scripts/ci-policy.mjs"],
]) {
test(`code or unknown changes run PR jobs: ${paths.join(", ")}`, () => {
assert.equal(selectPolicy("pull_request", paths).run_heavy, true);
});
}
test("PRs keep Linux and Windows, while merge, push and release retain all platforms", () => {
assert.deepEqual(
selectPolicy("pull_request", ["apps/desktop/main.ts"]).desktop_matrix.include.map(
(target) => target.platform,
),
["linux", "win"],
);
for (const event of ["push", "merge_group", "workflow_dispatch", "release"]) {
const policy = selectPolicy(event, ["README.md"]);
assert.equal(policy.run_heavy, true);
assert.deepEqual(
policy.desktop_matrix.include.map((target) => target.platform),
["mac", "mac", "linux", "win"],
);
}
});
test("renames out of source, deleted code and newline names cannot masquerade as docs", () => {
const root = mkdtempSync(join(tmpdir(), "selftune-ci-policy-"));
const git = (...args) =>
execFileSync(
"git",
["-c", "user.name=CI fixture", "-c", "user.email=ci@example.invalid", ...args],
{ cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
).trim();
try {
git("init", "-b", "main");
mkdirSync(join(root, "docs"));
writeFileSync(join(root, "source.js"), "source");
writeFileSync(join(root, "delete.js"), "delete");
git("add", ".");
git("commit", "-m", "base");
const base = git("rev-parse", "HEAD");
renameSync(join(root, "source.js"), join(root, "docs/source.md"));
rmSync(join(root, "delete.js"));
writeFileSync(join(root, "code\nname.js"), "code");
git("add", ".");
git("commit", "-m", "changes");
const paths = changedPaths({ pull_request: { base: { sha: base } } }, root);
assert.deepEqual(
new Set(paths),
new Set(["source.js", "delete.js", "docs/source.md", "code\nname.js"]),
);
assert.equal(isDocsOnly(paths), false);
assert.throws(
() => changedPaths({ pull_request: { base: { sha: "--bad" } } }, root),
/full PR base/,
);
} finally {
rmSync(root, { force: true, recursive: true });
}
});
42 changes: 39 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
name: CI

on:
merge_group:
pull_request:
branches: [main]
workflow_dispatch: {}

concurrency:
group: ci-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions: {}

jobs:
changes:
runs-on: ubuntu-latest
timeout-minutes: 3
permissions:
contents: read
outputs:
run_heavy: ${{ steps.policy.outputs.run_heavy }}
desktop_matrix: ${{ steps.policy.outputs.desktop_matrix }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
fetch-depth: 0
persist-credentials: false
- name: Verify CI selection
run: node --test .github/scripts/ci-policy.test.mjs
- name: Select CI jobs
id: policy
run: node .github/scripts/ci-policy.mjs

lint:
needs: changes
if: ${{ !cancelled() && (needs.changes.result != 'success' || needs.changes.outputs.run_heavy == 'true') }}
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Require successful change selection
env:
RESULT: ${{ needs.changes.result }}
run: test "$RESULT" = success
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
with:
Expand All @@ -28,7 +57,8 @@ jobs:
- run: bun run lint-architecture.ts

build-dashboard:
needs: [lint]
needs: [changes, lint]
if: needs.changes.outputs.run_heavy == 'true'
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
Expand Down Expand Up @@ -71,11 +101,17 @@ jobs:
run: bash .github/scripts/generate-sbom.sh

test:
needs: [lint]
needs: [changes, lint]
if: ${{ !cancelled() && (needs.changes.result != 'success' || needs.changes.outputs.run_heavy == 'true') }}
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Require successful selection and lint
env:
SELECTION: ${{ needs.changes.result }}
LINT: ${{ needs.lint.result }}
run: test "$SELECTION" = success && test "$LINT" = success
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
with:
Expand Down
73 changes: 40 additions & 33 deletions .github/workflows/desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ on:
description: Immutable 40-character source commit for the release tag
required: true
type: string
push:
branches: [main]
merge_group:
pull_request:
paths:
- apps/desktop/**
Expand All @@ -58,16 +61,41 @@ on:
- scripts/release-plan.ts
- scripts/validate-release-ref.ts
- tests/scripts/release-workflows.test.ts
- .github/scripts/ci-policy.mjs
- .github/scripts/ci-policy.test.mjs
- "!**/*.md"
- "!docs/**"

permissions:
contents: read

concurrency:
group: desktop-${{ inputs.release_tag || github.ref }}
cancel-in-progress: false
group: desktop-${{ inputs.release_tag || github.event.pull_request.number || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
changes:
runs-on: ubuntu-latest
timeout-minutes: 3
permissions:
contents: read
outputs:
run_heavy: ${{ steps.policy.outputs.run_heavy }}
desktop_matrix: ${{ steps.policy.outputs.desktop_matrix }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
fetch-depth: 0
persist-credentials: false
- name: Verify CI selection
run: node --test .github/scripts/ci-policy.test.mjs
- name: Select CI jobs
id: policy
run: node .github/scripts/ci-policy.mjs

build-windows-sidecar:
needs: changes
if: needs.changes.outputs.run_heavy == 'true'
runs-on: ubuntu-latest
timeout-minutes: 20
env:
Expand Down Expand Up @@ -128,39 +156,16 @@ jobs:
if-no-files-found: error

build:
needs: build-windows-sidecar
needs: [changes, build-windows-sidecar]
strategy:
fail-fast: false
matrix:
include:
- os: macos-15
platform: mac
target: bun-darwin-arm64
package_script: package:mac
arch_flag: --arm64
artifact: selftune-desktop-macos-arm64
- os: macos-15-intel
platform: mac
target: bun-darwin-x64
package_script: package:mac
arch_flag: --x64
artifact: selftune-desktop-macos-x64
- os: ubuntu-latest
platform: linux
target: bun-linux-x64
package_script: package:linux
arch_flag: --x64
artifact: selftune-desktop-linux-x64
- os: windows-latest
platform: win
target: bun-windows-x64
package_script: package:win
arch_flag: --x64
artifact: selftune-desktop-windows-x64
matrix: ${{ fromJSON(needs.changes.outputs.desktop_matrix) }}
runs-on: ${{ matrix.os }}
timeout-minutes: 60
env:
BUN_TARGET: ${{ matrix.target }}
DESKTOP_PACKAGE_SCRIPT: ${{ matrix.package_script }}
DESKTOP_ARCH_FLAG: ${{ matrix.arch_flag }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
Expand Down Expand Up @@ -281,7 +286,8 @@ jobs:
if: inputs.release_tag == ''
env:
CSC_IDENTITY_AUTO_DISCOVERY: "false"
run: bun run --cwd apps/desktop ${{ matrix.package_script }} -- ${{ matrix.arch_flag }} --publish never
shell: bash
run: bun run --cwd apps/desktop "$DESKTOP_PACKAGE_SCRIPT" -- "$DESKTOP_ARCH_FLAG" --publish never

- name: Build signed macOS release distributable
if: inputs.release_tag != '' && matrix.platform == 'mac'
Expand Down Expand Up @@ -324,7 +330,7 @@ jobs:
max_attempts=3
for attempt in $(seq 1 "$max_attempts"); do
set +e
bun run --cwd apps/desktop "${{ matrix.package_script }}" -- "${{ matrix.arch_flag }}" --publish never
bun run --cwd apps/desktop "$DESKTOP_PACKAGE_SCRIPT" -- "$DESKTOP_ARCH_FLAG" --publish never
status=$?
set -e

Expand All @@ -345,7 +351,8 @@ jobs:
if: inputs.release_tag != '' && matrix.platform != 'mac'
env:
CSC_IDENTITY_AUTO_DISCOVERY: "false"
run: bun run --cwd apps/desktop ${{ matrix.package_script }} -- ${{ matrix.arch_flag }} --publish never
shell: bash
run: bun run --cwd apps/desktop "$DESKTOP_PACKAGE_SCRIPT" -- "$DESKTOP_ARCH_FLAG" --publish never

- name: Verify signed macOS application bundle
if: inputs.release_tag != '' && matrix.platform == 'mac'
Expand Down Expand Up @@ -373,7 +380,7 @@ jobs:
- name: Preserve macOS update manifest per architecture
if: matrix.platform == 'mac'
shell: bash
run: mv apps/desktop/dist/latest-mac.yml "apps/desktop/dist/latest-mac-${{ matrix.target }}.yml"
run: mv apps/desktop/dist/latest-mac.yml "apps/desktop/dist/latest-mac-${BUN_TARGET}.yml"

- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
Expand Down
Loading
Loading