Skip to content

Commit 76c8e83

Browse files
committed
Merge branch 'codex/sdk-ci-pr-triggers' into sdk-candidate-ci
2 parents 7fcf05c + 61663a6 commit 76c8e83

4 files changed

Lines changed: 169 additions & 17 deletions

File tree

‎.fernignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Specify files that shouldn't be modified by Fern
22

33
.github/workflows/sdk-release-pr-notification.yml
4+
.github/workflows/ci.yml
5+
.github/scripts/validate-release.cjs
6+
.github/scripts/validate-release.test.cjs
47
README.md
58
changelog.md
69
.fern/replay.lock
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { readFileSync } = require("node:fs");
2+
3+
const { GITHUB_REF: ref = "", GITHUB_REF_NAME: tag = "" } = process.env;
4+
5+
if (!tag || ref !== `refs/tags/${tag}`) {
6+
console.error("Release requires a tag ref matching GITHUB_REF_NAME.");
7+
process.exit(1);
8+
}
9+
10+
const { version } = JSON.parse(readFileSync("package.json", "utf8"));
11+
12+
if (tag.replace(/^v/, "") !== version) {
13+
console.error(`Release tag ${tag} does not match package version ${version}.`);
14+
process.exit(1);
15+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const assert = require("node:assert/strict");
2+
const { mkdtempSync, readFileSync, rmSync, writeFileSync } = require("node:fs");
3+
const { tmpdir } = require("node:os");
4+
const { join, resolve } = require("node:path");
5+
const { spawnSync } = require("node:child_process");
6+
const { test } = require("node:test");
7+
8+
const validator = resolve(__dirname, "validate-release.cjs");
9+
10+
for (const version of ["3.0.0", "3.0.1-alpha.1", "3.0.1-beta.2", "2.9.0"]) {
11+
for (const prefix of ["", "v"]) {
12+
const tag = `${prefix}${version}`;
13+
test(`accepts matching release ${tag}`, () => {
14+
const cwd = mkdtempSync(join(tmpdir(), "sdk-release-"));
15+
try {
16+
writeFileSync(join(cwd, "package.json"), JSON.stringify({ version }));
17+
const result = spawnSync(process.execPath, [validator], {
18+
cwd,
19+
env: { ...process.env, GITHUB_REF: `refs/tags/${tag}`, GITHUB_REF_NAME: tag },
20+
encoding: "utf8",
21+
});
22+
assert.equal(result.status, 0, result.stderr);
23+
} finally {
24+
rmSync(cwd, { recursive: true, force: true });
25+
}
26+
});
27+
}
28+
}
29+
30+
for (const [ref, tag] of [
31+
["refs/heads/refs/tags/attack", "refs/tags/attack"],
32+
["refs/heads/refs/tags/v3.0.0", "v3.0.0"],
33+
["refs/heads/main", "main"],
34+
["refs/tags/v999.0.0", "v999.0.0"],
35+
["refs/tags/v3.0.0-beta.1", "v3.0.0-beta.1"],
36+
["refs/tags/vv3.0.0", "vv3.0.0"],
37+
["refs/tags/v3.0.0", "3.0.0"],
38+
["refs/tags/", ""],
39+
["", ""],
40+
]) {
41+
test(`rejects invalid release ${ref} / ${tag}`, () => {
42+
const cwd = mkdtempSync(join(tmpdir(), "sdk-release-"));
43+
try {
44+
writeFileSync(join(cwd, "package.json"), JSON.stringify({ version: "3.0.0" }));
45+
const result = spawnSync(process.execPath, [validator], {
46+
cwd,
47+
env: { ...process.env, GITHUB_REF: ref, GITHUB_REF_NAME: tag },
48+
encoding: "utf8",
49+
});
50+
assert.equal(result.status, 1, result.stderr);
51+
assert.match(result.stderr, /Release (requires a tag ref|tag .* does not match package version)/);
52+
} finally {
53+
rmSync(cwd, { recursive: true, force: true });
54+
}
55+
});
56+
}
57+
58+
test("package job rejects branch refs before any release steps run", () => {
59+
const workflow = readFileSync(resolve(__dirname, "../workflows/ci.yml"), "utf8");
60+
assert.match(workflow, /if: github\.event_name == 'push' && startsWith\(github\.ref, 'refs\/tags\/'\)/);
61+
});
62+
63+
test("package job validates the release before installing or building", () => {
64+
const workflow = readFileSync(resolve(__dirname, "../workflows/ci.yml"), "utf8");
65+
const packageJob = workflow.split(" package:\n")[1].split(" publish:\n")[0];
66+
const validation = packageJob.indexOf("run: node .github/scripts/validate-release.cjs");
67+
assert.ok(validation >= 0, "release validation is missing");
68+
assert.ok(validation < packageJob.indexOf("run: pnpm install"));
69+
assert.ok(validation < packageJob.indexOf("run: pnpm build"));
70+
assert.ok(validation < packageJob.indexOf("npm pack"));
71+
});

‎.github/workflows/ci.yml‎

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: ci
22

3-
on: [push]
3+
on: [push, pull_request, workflow_dispatch]
4+
5+
permissions:
6+
contents: read
47

58
concurrency:
69
group: ${{ github.workflow }}-${{ github.ref }}
@@ -35,7 +38,10 @@ jobs:
3538

3639
- name: Set up node
3740
uses: actions/setup-node@v6
38-
41+
42+
- name: Test release validation
43+
run: node --test .github/scripts/validate-release.test.cjs
44+
3945
- name: Install pnpm
4046
uses: pnpm/action-setup@v4
4147

@@ -45,19 +51,29 @@ jobs:
4551
- name: Test
4652
run: pnpm test
4753

48-
publish:
54+
package:
4955
needs: [ compile, test ]
50-
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
56+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
5157
runs-on: ubuntu-latest
58+
timeout-minutes: 15
5259
permissions:
53-
contents: read # Required for checkout
54-
id-token: write # Required for OIDC
60+
contents: read
61+
outputs:
62+
dist-tag: ${{ steps.release.outputs.dist-tag }}
5563
steps:
5664
- name: Checkout repo
5765
uses: actions/checkout@v6
66+
with:
67+
persist-credentials: false
5868

5969
- name: Set up node
60-
uses: actions/setup-node@v6
70+
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
71+
with:
72+
node-version: "24"
73+
package-manager-cache: false
74+
75+
- name: Validate release tag
76+
run: node .github/scripts/validate-release.cjs
6177

6278
- name: Install pnpm
6379
uses: pnpm/action-setup@v4
@@ -68,23 +84,70 @@ jobs:
6884
- name: Build
6985
run: pnpm build
7086

71-
- name: Publish to npm
87+
- name: Select npm dist-tag
88+
id: release
7289
run: |
73-
publish() { # use latest npm to ensure OIDC support
74-
npx -y npm@latest publish "$@"
75-
}
7690
if [[ ${GITHUB_REF} == *alpha* ]]; then
77-
publish --access public --tag alpha
91+
dist_tag=alpha
7892
elif [[ ${GITHUB_REF} == *beta* ]]; then
79-
publish --access public --tag beta
93+
dist_tag=beta
8094
else
8195
PKG_NAME=$(node -p "require('./package.json').name")
8296
PKG_VERSION=$(node -p "require('./package.json').version")
8397
CURRENT_LATEST=$(npm view "${PKG_NAME}" dist-tags.latest 2>/dev/null || echo "0.0.0")
8498
if npx -y semver "${PKG_VERSION}" -r "<${CURRENT_LATEST}" > /dev/null 2>&1; then
85-
echo "Publishing ${PKG_VERSION} with --tag backport (current latest is ${CURRENT_LATEST})"
86-
publish --access public --tag backport
99+
dist_tag=backport
87100
else
88-
publish --access public
101+
dist_tag=latest
89102
fi
90-
fi
103+
fi
104+
echo "dist-tag=${dist_tag}" >> "$GITHUB_OUTPUT"
105+
106+
- name: Pack npm artifact
107+
run: |
108+
mkdir -p "$RUNNER_TEMP/npm-package"
109+
npm pack --ignore-scripts --pack-destination "$RUNNER_TEMP/npm-package"
110+
111+
- name: Upload npm artifact
112+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
113+
with:
114+
name: npm-package
115+
path: ${{ runner.temp }}/npm-package/*.tgz
116+
if-no-files-found: error
117+
retention-days: 1
118+
119+
publish:
120+
needs: package
121+
runs-on: ubuntu-latest
122+
timeout-minutes: 5
123+
permissions:
124+
id-token: write
125+
steps:
126+
# Keep package code and caches out of the job with publishing authority.
127+
- name: Set up node
128+
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
129+
with:
130+
node-version: "24"
131+
registry-url: https://registry.npmjs.org
132+
package-manager-cache: false
133+
134+
- name: Download npm artifact
135+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
136+
with:
137+
name: npm-package
138+
path: .release
139+
140+
- name: Publish npm artifact
141+
env:
142+
DIST_TAG: ${{ needs.package.outputs.dist-tag }}
143+
run: |
144+
tarballs=(.release/*.tgz)
145+
if [[ ${#tarballs[@]} -ne 1 || ! -f "${tarballs[0]}" ]]; then
146+
echo "::error::Expected exactly one npm package tarball."
147+
exit 1
148+
fi
149+
case "$DIST_TAG" in
150+
latest|alpha|beta|backport) ;;
151+
*) echo "::error::Unexpected npm dist-tag."; exit 1 ;;
152+
esac
153+
npm publish "${tarballs[0]}" --access public --ignore-scripts --tag "$DIST_TAG"

0 commit comments

Comments
 (0)