Skip to content

Commit 466daf5

Browse files
Merge pull request #274 from BitGo/BTC-0.detect-ci-changes
ci: add dynamic package detection for pull requests
2 parents 6802d86 + 6ef827c commit 466daf5

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

.github/scripts/detect-changes.mjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
import { execSync } from 'child_process';
3+
import { appendFileSync } from 'fs';
4+
5+
const ALL_PACKAGES = ['wasm-bip32', 'wasm-mps', 'wasm-utxo', 'wasm-solana', 'wasm-dot', 'wasm-ton'];
6+
7+
function setOutput(packages) {
8+
const value = JSON.stringify(packages);
9+
appendFileSync(process.env.GITHUB_OUTPUT, `packages=${value}\n`);
10+
console.log(`Packages to test: ${value}`);
11+
}
12+
13+
// Non-PR events (push to master, workflow_dispatch): run everything
14+
if (process.env.GITHUB_EVENT_NAME !== 'pull_request') {
15+
setOutput(ALL_PACKAGES);
16+
process.exit(0);
17+
}
18+
19+
const base = process.env.BASE_SHA;
20+
const head = process.env.HEAD_SHA;
21+
22+
const changedFiles = execSync(`git diff --name-only ${base} ${head}`)
23+
.toString().trim().split('\n').filter(Boolean);
24+
25+
// Shared infrastructure changes → run all packages
26+
const sharedChanged = changedFiles.some(f =>
27+
/^(\.github\/|package\.json$|lerna\.json$|package-lock\.json$)/.test(f)
28+
);
29+
if (sharedChanged) {
30+
setOutput(ALL_PACKAGES);
31+
process.exit(0);
32+
}
33+
34+
// Per-package detection; fall back to all if nothing package-specific changed
35+
const changed = ALL_PACKAGES.filter(pkg =>
36+
changedFiles.some(f => f.startsWith(`packages/${pkg}/`))
37+
);
38+
setOutput(changed.length > 0 ? changed : ALL_PACKAGES);

.github/workflows/build-and-test.yaml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ env:
2020
WASM_OPT_VERSION: 0.116.1
2121

2222
jobs:
23+
detect-changes:
24+
name: "Detect Changes"
25+
runs-on: ubuntu-latest
26+
outputs:
27+
packages: ${{ steps.set-matrix.outputs.packages }}
28+
steps:
29+
- uses: actions/checkout@v4
30+
with:
31+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
32+
fetch-depth: 0
33+
34+
- name: Determine changed packages
35+
id: set-matrix
36+
env:
37+
GITHUB_EVENT_NAME: ${{ github.event_name }}
38+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
39+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
40+
run: node .github/scripts/detect-changes.mjs
41+
2342
build:
2443
name: "Build"
2544
runs-on: ubuntu-latest
@@ -112,12 +131,12 @@ jobs:
112131

113132
test:
114133
name: "Test ${{ matrix.package }}"
115-
needs: build
134+
needs: [detect-changes, build]
116135
runs-on: ubuntu-latest
117136
strategy:
118137
fail-fast: false
119138
matrix:
120-
package: [wasm-bip32, wasm-mps, wasm-utxo, wasm-solana, wasm-dot, wasm-ton]
139+
package: ${{ fromJson(needs.detect-changes.outputs.packages) }}
121140
include:
122141
- package: wasm-utxo
123142
needs-wasm-pack: true

0 commit comments

Comments
 (0)