Skip to content

Commit ac13b7e

Browse files
committed
Create reusable build-dist workflow
- Add build-dist-reusable.yml with configurable inputs - Update build-dist.yml to use reusable workflow - Make script respect DIST_BRANCH env var - This can now be extracted to a separate repo for reuse
1 parent 86303cf commit ac13b7e

File tree

3 files changed

+88
-29
lines changed

3 files changed

+88
-29
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Build dist branch (reusable)
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
source_ref:
7+
description: 'Source ref to build from (commit SHA, branch, or tag)'
8+
required: false
9+
type: string
10+
default: 'main'
11+
node_version:
12+
description: 'Node.js version to use'
13+
required: false
14+
type: string
15+
default: '20'
16+
pnpm_version:
17+
description: 'pnpm version to use'
18+
required: false
19+
type: string
20+
default: '9'
21+
build_command:
22+
description: 'Build command to run'
23+
required: false
24+
type: string
25+
default: 'pnpm run build'
26+
dist_branch:
27+
description: 'Name of dist branch'
28+
required: false
29+
type: string
30+
default: 'dist'
31+
32+
jobs:
33+
build-dist:
34+
runs-on: ubuntu-latest
35+
permissions:
36+
contents: write
37+
steps:
38+
- uses: actions/checkout@v4
39+
with:
40+
ref: ${{ inputs.source_ref }}
41+
42+
- name: Get source commit SHA
43+
id: source
44+
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
45+
46+
- name: Fetch build script from dist branch
47+
run: |
48+
git fetch origin ${{ inputs.dist_branch }}
49+
git checkout origin/${{ inputs.dist_branch }} -- scripts/build-dist.sh
50+
51+
- uses: pnpm/action-setup@v4
52+
with:
53+
version: ${{ inputs.pnpm_version }}
54+
55+
- uses: actions/setup-node@v4
56+
with:
57+
node-version: ${{ inputs.node_version }}
58+
cache: 'pnpm'
59+
60+
- name: Install dependencies
61+
run: pnpm install --frozen-lockfile
62+
63+
- name: Build
64+
run: ${{ inputs.build_command }}
65+
66+
- name: Build dist branch
67+
run: ./scripts/build-dist.sh ${{ steps.source.outputs.sha }}
68+
env:
69+
DIST_BRANCH: ${{ inputs.dist_branch }}
70+
71+
- name: Push dist branch
72+
run: git push origin ${{ inputs.dist_branch }} --force-with-lease

.github/workflows/build-dist.yml

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,7 @@ on:
88
default: 'main'
99
jobs:
1010
build-dist:
11-
runs-on: ubuntu-latest
12-
permissions:
13-
contents: write
14-
steps:
15-
- uses: actions/checkout@v4
16-
with:
17-
ref: ${{ inputs.source_ref }}
18-
- uses: pnpm/action-setup@v4
19-
with:
20-
version: 10
21-
- uses: actions/setup-node@v4
22-
with:
23-
node-version: '20'
24-
cache: 'pnpm'
25-
- run: pnpm install --frozen-lockfile
26-
- run: pnpm run build
27-
- run: ./scripts/build-dist.sh
28-
- run: git push origin dist --force-with-lease
11+
uses: ./.github/workflows/build-dist-reusable.yml
12+
with:
13+
source_ref: ${{ inputs.source_ref }}
14+
pnpm_version: '10'

scripts/build-dist.sh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
set -e
33

44
SOURCE_SHA="${1:-$(git rev-parse HEAD)}"
5+
DIST_BRANCH="${DIST_BRANCH:-dist}"
56

6-
echo "Building dist from source commit: $SOURCE_SHA"
7+
echo "Building $DIST_BRANCH from source commit: $SOURCE_SHA"
78

89
# Checkout or create dist branch
9-
if git fetch origin dist:dist 2>/dev/null; then
10-
git checkout dist
10+
if git fetch origin "$DIST_BRANCH:$DIST_BRANCH" 2>/dev/null; then
11+
git checkout "$DIST_BRANCH"
1112
else
12-
git checkout --orphan dist
13+
git checkout --orphan "$DIST_BRANCH"
1314
fi
1415

1516
# Configure git
@@ -33,9 +34,9 @@ rmdir dist 2>/dev/null || true
3334
if [ -f package.json.dist ]; then
3435
mv package.json.dist package.json
3536
else
36-
echo "ERROR: No package.json found on dist branch"
37-
echo "This script requires an existing dist branch with a package.json"
38-
echo "For initial dist branch setup, manually create package.json with correct paths:"
37+
echo "ERROR: No package.json found on $DIST_BRANCH branch"
38+
echo "This script requires an existing $DIST_BRANCH branch with a package.json"
39+
echo "For initial $DIST_BRANCH branch setup, manually create package.json with correct paths:"
3940
echo " main: ./index.cjs (not ./dist/index.cjs)"
4041
echo " module: ./index.js (not ./dist/index.js)"
4142
exit 1
@@ -47,11 +48,11 @@ git add -A
4748
# Create commit
4849
if git rev-parse --verify HEAD~0 >/dev/null 2>&1; then
4950
# dist branch exists, create merge commit
50-
git commit -m "Build dist from $SOURCE_SHA" || true
51-
git merge --no-ff -m "Merge built dist from main@$SOURCE_SHA" "$SOURCE_SHA" -s ours || true
51+
git commit -m "Build $DIST_BRANCH from $SOURCE_SHA" || true
52+
git merge --no-ff -m "Merge built $DIST_BRANCH from main@$SOURCE_SHA" "$SOURCE_SHA" -s ours || true
5253
else
5354
# First dist commit
54-
git commit -m "Initial dist build from $SOURCE_SHA"
55+
git commit -m "Initial $DIST_BRANCH build from $SOURCE_SHA"
5556
fi
5657

57-
echo "Dist branch built successfully"
58+
echo "$DIST_BRANCH branch built successfully"

0 commit comments

Comments
 (0)