Skip to content

Commit 7570a78

Browse files
committed
Harden CI and publish workflow
- Scope workflow permissions to contents: read; move npm release and GitHub Packages publishing into separate jobs that need the build and get id-token/packages write only where required. - Compute the version once and pass it to every job via job outputs. - Replace the global replace-in-files-cli install with scripts/set-version.mjs (Node built-ins only). - Pin actions to commit SHAs. - Only tolerate "already published" on GitHub Packages pushes. - Use setup-node npm caching, add a concurrency group, lint once on ubuntu, and test Node 22 on ubuntu. - Group example-app Dependabot updates separately from SDK updates.
1 parent 54ac83f commit 7570a78

4 files changed

Lines changed: 218 additions & 43 deletions

File tree

‎.github/dependabot.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ updates:
77
cooldown:
88
default-days: 7
99
groups:
10+
# Dependencies used only by the example apps, so a framework bump that
11+
# breaks an example can't hold up SDK dependency updates. Dependencies
12+
# shared with the SDK packages (react, react-native, typescript, ...)
13+
# stay in npm-dependencies. The first matching group wins.
14+
example-dependencies:
15+
patterns:
16+
- "@expo/*"
17+
- "expo"
18+
- "expo-*"
19+
- "@react-native/*"
20+
- "react-native-*"
21+
- "@sveltejs/*"
22+
- "svelte"
23+
- "svelte-*"
24+
- "@testing-library/*"
25+
- "@vitejs/*"
26+
- "vite"
27+
- "@vue/*"
28+
- "vue"
29+
- "express"
30+
- "jquery"
31+
- "next"
1032
npm-dependencies:
1133
patterns:
1234
- "*"

‎.github/workflows/build.yml‎

Lines changed: 130 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@ on: [push, pull_request]
33

44
permissions:
55
contents: read
6-
id-token: write
7-
packages: write
6+
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.ref }}
9+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
810

911
jobs:
10-
build:
11-
runs-on: ${{ matrix.os }}
12-
strategy:
13-
matrix:
14-
os:
15-
- ubuntu-latest
16-
- macos-latest
17-
- windows-latest
18-
node_version:
19-
- 24
20-
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
12+
version:
13+
name: Version
14+
runs-on: ubuntu-latest
15+
outputs:
16+
version: ${{ steps.version.outputs.version }}
17+
ci_tag: ${{ steps.version.outputs.ci_tag }}
2118
steps:
2219
- name: Checkout
23-
uses: actions/checkout@v7
20+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2421
with:
2522
fetch-depth: 0
23+
persist-credentials: false
2624
- name: Build Reason
2725
shell: bash
2826
run: |
@@ -36,20 +34,10 @@ jobs:
3634
elif [[ "${GITHUB_REF}" = refs/pull* ]]; then
3735
branch=""
3836
fi
39-
echo "GIT_BRANCH_SUFFIX=$branch" >> $GITHUB_ENV
37+
echo "GIT_BRANCH_SUFFIX=$branch" >> "$GITHUB_ENV"
4038
echo "ref: $GITHUB_REF event: $GITHUB_EVENT_NAME branch_suffix: $branch"
41-
- name: Setup Node.js environment
42-
uses: actions/setup-node@v7
43-
with:
44-
node-version: ${{ matrix.node_version }}
45-
registry-url: "https://registry.npmjs.org"
46-
- name: Cache node_modules
47-
uses: actions/cache@v6
48-
with:
49-
path: node_modules
50-
key: ${{ matrix.node_version }}-${{ runner.os }}-node-modules-${{ hashFiles('package-lock.json') }}
5139
- name: Setup .NET SDK for MinVer
52-
uses: actions/setup-dotnet@v6
40+
uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0
5341
with:
5442
dotnet-version: "10.0.x"
5543
- name: Build Version
@@ -67,37 +55,137 @@ jobs:
6755
fi
6856
fi
6957
70-
echo "version=$version" >> $GITHUB_OUTPUT
58+
ci_tag="${GIT_BRANCH_SUFFIX%.}"
59+
ci_tag="${ci_tag:-main}"
60+
ci_tag="ci-${ci_tag//\//-}"
61+
62+
echo "version=$version" >> "$GITHUB_OUTPUT"
63+
echo "ci_tag=$ci_tag" >> "$GITHUB_OUTPUT"
7164
echo "Version: $version"
72-
echo "### Version: $version" >> $GITHUB_STEP_SUMMARY
65+
echo "### Version: $version" >> "$GITHUB_STEP_SUMMARY"
7366
74-
npm install --global replace-in-files-cli
75-
replace-in-files --string="3.0.0-dev" --replacement=$version packages/core/src/configuration/Configuration.ts
76-
replace-in-files --string="3.0.0-dev" --replacement=$version **/package*.json
77-
npm ci
67+
build:
68+
needs: version
69+
runs-on: ${{ matrix.os }}
70+
strategy:
71+
matrix:
72+
os:
73+
- ubuntu-latest
74+
- macos-latest
75+
- windows-latest
76+
node_version:
77+
- 24
78+
include:
79+
- os: ubuntu-latest
80+
node_version: 22
81+
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
82+
steps:
83+
- name: Checkout
84+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
85+
with:
86+
persist-credentials: false
87+
- name: Setup Node.js environment
88+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
89+
with:
90+
node-version: ${{ matrix.node_version }}
91+
cache: npm
92+
- name: Set Version
93+
shell: bash
94+
run: node scripts/set-version.mjs "$VERSION"
95+
env:
96+
VERSION: ${{ needs.version.outputs.version }}
97+
- name: Install Dependencies
98+
run: npm ci
7899
- name: Build
79100
run: npm run build
80101
- name: Lint
102+
if: matrix.os == 'ubuntu-latest' && matrix.node_version == 24
81103
run: npm run lint
82104
- name: Run Tests
83105
run: npm test
106+
107+
# Publish jobs rebuild from a clean checkout so elevated permissions are only
108+
# granted to the job that needs them. They skip dependency caching on purpose.
109+
publish-npm:
110+
name: Publish Release Packages
111+
if: startsWith(github.ref, 'refs/tags/v')
112+
needs: [version, build]
113+
runs-on: ubuntu-latest
114+
environment: npm-release
115+
permissions:
116+
contents: read
117+
id-token: write
118+
steps:
119+
- name: Checkout
120+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
121+
with:
122+
persist-credentials: false
123+
- name: Setup Node.js environment
124+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
125+
with:
126+
node-version: 24
127+
registry-url: "https://registry.npmjs.org"
128+
package-manager-cache: false
129+
- name: Set Version
130+
run: node scripts/set-version.mjs "$VERSION"
131+
env:
132+
VERSION: ${{ needs.version.outputs.version }}
133+
- name: Install Dependencies
134+
run: npm ci
135+
- name: Build
136+
run: npm run build
84137
- name: Publish Release Packages
85-
if: startsWith(github.ref, 'refs/tags/v') && matrix.os == 'ubuntu-latest'
86138
run: npm publish --workspaces --access public
139+
140+
publish-github:
141+
name: Push GitHub CI Packages
142+
if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/heads/') && contains(needs.version.outputs.version, '-')
143+
needs: [version, build]
144+
runs-on: ubuntu-latest
145+
permissions:
146+
contents: read
147+
packages: write
148+
steps:
149+
- name: Checkout
150+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
151+
with:
152+
persist-credentials: false
153+
- name: Setup Node.js environment
154+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
155+
with:
156+
node-version: 24
157+
package-manager-cache: false
158+
- name: Set Version
159+
run: node scripts/set-version.mjs "$VERSION"
160+
env:
161+
VERSION: ${{ needs.version.outputs.version }}
162+
- name: Install Dependencies
163+
run: npm ci
164+
- name: Build
165+
run: npm run build
87166
- name: Setup GitHub CI Node.js environment
88-
if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/heads/') && matrix.os == 'ubuntu-latest' && contains(steps.version.outputs.version, '-')
89-
uses: actions/setup-node@v7
167+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
90168
with:
91-
node-version: ${{ matrix.node_version }}
169+
node-version: 24
92170
registry-url: "https://npm.pkg.github.com"
93171
scope: "@exceptionless"
172+
package-manager-cache: false
94173
- name: Push GitHub CI Packages
95-
if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/heads/') && matrix.os == 'ubuntu-latest' && contains(steps.version.outputs.version, '-')
96174
shell: bash
97175
run: |
98-
TAG_BRANCH="${GIT_BRANCH_SUFFIX%.}"
99-
TAG_BRANCH="${TAG_BRANCH:-main}"
100-
TAG_BRANCH="${TAG_BRANCH//\//-}"
101-
npm publish --workspaces --access public --tag "ci-${TAG_BRANCH}" || true
176+
# Re-runs of the same commit produce the same version; skip packages
177+
# that already exist and let every other publish failure fail the job.
178+
for dir in packages/*/; do
179+
dir="${dir%/}"
180+
name=$(node -p "require('./$dir/package.json').name")
181+
existing=$(npm view "$name@$VERSION" version 2>/dev/null || true)
182+
if [ "$existing" = "$VERSION" ]; then
183+
echo "::notice::$name@$VERSION already exists in GitHub Packages; skipping."
184+
continue
185+
fi
186+
npm publish --workspace "$dir" --access public --tag "$CI_TAG"
187+
done
102188
env:
103-
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
189+
VERSION: ${{ needs.version.outputs.version }}
190+
CI_TAG: ${{ needs.version.outputs.ci_tag }}
191+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎eslint.config.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import tseslint from "typescript-eslint";
77

88
export default defineConfig(
99
{
10-
ignores: ["**/dist/", "**/node_modules/", ".agents/", "example/", "**/expo-plugin/", "**/react-native.config.*"]
10+
ignores: ["**/dist/", "**/node_modules/", ".agents/", "example/", "**/expo-plugin/", "**/react-native.config.*", "scripts/"]
1111
},
1212
eslint.configs.recommended,
1313
{

‎scripts/set-version.mjs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env node
2+
// Stamps the computed build version over the "3.0.0-dev" placeholder in the
3+
// core configuration, the root and workspace package.json files, and
4+
// package-lock.json. Used by CI before `npm ci`; uses only Node built-ins.
5+
//
6+
// Usage: node scripts/set-version.mjs <version>
7+
8+
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
9+
import { dirname, join, resolve } from "node:path";
10+
import { fileURLToPath } from "node:url";
11+
12+
const PLACEHOLDER = "3.0.0-dev";
13+
const VERSION_PATTERN = /^\d+\.\d+\.\d+\S*$/;
14+
15+
const version = process.argv[2];
16+
if (!version || !VERSION_PATTERN.test(version)) {
17+
console.error(`Usage: node scripts/set-version.mjs <version> (got: ${version ?? "nothing"})`);
18+
process.exit(1);
19+
}
20+
21+
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
22+
const rootPackage = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
23+
24+
function workspaceDirs(patterns) {
25+
return patterns.flatMap((pattern) => {
26+
if (!pattern.endsWith("/*")) {
27+
return [pattern];
28+
}
29+
30+
const parent = pattern.slice(0, -2);
31+
return readdirSync(join(root, parent), { withFileTypes: true })
32+
.filter((entry) => entry.isDirectory())
33+
.map((entry) => `${parent}/${entry.name}`);
34+
});
35+
}
36+
37+
const files = [
38+
"packages/core/src/configuration/Configuration.ts",
39+
"package.json",
40+
"package-lock.json",
41+
...workspaceDirs(rootPackage.workspaces ?? [])
42+
.map((dir) => `${dir}/package.json`)
43+
.filter((file) => existsSync(join(root, file)))
44+
];
45+
46+
let total = 0;
47+
for (const file of files) {
48+
const path = join(root, file);
49+
const content = readFileSync(path, "utf8");
50+
const parts = content.split(PLACEHOLDER);
51+
if (parts.length === 1) {
52+
continue;
53+
}
54+
55+
writeFileSync(path, parts.join(version));
56+
total += parts.length - 1;
57+
console.log(`${file}: ${parts.length - 1} replacement(s)`);
58+
}
59+
60+
if (total === 0) {
61+
console.error(`No "${PLACEHOLDER}" placeholders found; nothing was versioned.`);
62+
process.exit(1);
63+
}
64+
65+
console.log(`Set version ${version} (${total} replacement(s) in total).`);

0 commit comments

Comments
 (0)