-
Notifications
You must be signed in to change notification settings - Fork 0
302 lines (268 loc) · 10.7 KB
/
build.yml
File metadata and controls
302 lines (268 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
name: Build plugin
on:
push:
branches: ["**"]
workflow_dispatch:
inputs:
update:
description: "Run bun update on dependencies before building"
type: boolean
default: false
pull_request:
description: "Create a PR instead of committing directly to the branch"
type: boolean
default: false
tag_latest:
description: "Trigger tag-latest workflow after successful build"
type: boolean
default: false
workflow_call:
inputs:
update:
description: "Run bun update on dependencies before building"
type: boolean
default: false
pull_request:
description: "Create a PR instead of committing directly to the branch"
type: boolean
default: false
tag_latest:
description: "Trigger tag-latest workflow after successful build"
type: boolean
default: false
permissions:
contents: write
pull-requests: write
actions: write
# Only one build at a time per branch
concurrency:
group: build-${{ github.ref }}
cancel-in-progress: false
jobs:
build:
runs-on: ubuntu-latest
env:
# Prefer GH_TOKEN secret (allows triggering downstream workflows like tag-latest)
# Fall back to GITHUB_TOKEN (won't trigger other workflows)
TOKEN: ${{ secrets.GH_TOKEN || github.token }}
COMMIT_PREFIX: "ci-build: "
steps:
# ── 1. Skip check: bail if the triggering commit is from this workflow ──
- name: Check if previous commit is from this workflow
id: skip_check
env:
GH_TOKEN: ${{ secrets.GH_TOKEN || github.token }}
run: |
COMMIT_MSG=$(gh api repos/${{ github.repository }}/commits/${{ github.sha }} --jq '.commit.message' 2>/dev/null || echo "")
if [[ "$COMMIT_MSG" == "${COMMIT_PREFIX}"* ]]; then
echo "skip=true" >> "$GITHUB_OUTPUT"
echo "::notice::Skipping build — previous commit was from this workflow: ${COMMIT_MSG%%$'\n'*}"
else
echo "skip=false" >> "$GITHUB_OUTPUT"
fi
- name: Exit early if skip
if: steps.skip_check.outputs.skip == 'true'
run: |
echo "## Skipped" >> "$GITHUB_STEP_SUMMARY"
echo "Previous commit was from this workflow. Nothing to do." >> "$GITHUB_STEP_SUMMARY"
# ── 2. Checkout and setup ──
- uses: actions/checkout@v4
if: steps.skip_check.outputs.skip != 'true'
with:
token: ${{ env.TOKEN }}
fetch-depth: 0
- uses: oven-sh/setup-bun@v2
if: steps.skip_check.outputs.skip != 'true'
with:
bun-version: latest
# ── 3. Install dependencies ──
- name: Install dependencies
if: steps.skip_check.outputs.skip != 'true'
run: bun install --frozen-lockfile 2>/dev/null || bun install
# ── 4. Optional: upgrade dependencies ──
- name: Upgrade dependencies
if: steps.skip_check.outputs.skip != 'true' && inputs.update == 'true'
run: |
echo "Upgrading dependencies..."
bun update
echo "upgrade_ran=true" >> "$GITHUB_ENV"
# ── 5. Build ──
- name: Build
if: steps.skip_check.outputs.skip != 'true'
run: bunx tsc
# ── 6. Detect changes ──
- name: Detect changes
if: steps.skip_check.outputs.skip != 'true'
id: changes
run: |
git add -A
if git diff --cached --quiet; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
echo "diff_summary=No changes detected." >> "$GITHUB_OUTPUT"
else
echo "has_changes=true" >> "$GITHUB_OUTPUT"
# Build a human-readable summary of changed files
STAT=$(git diff --cached --stat)
SHORTSTAT=$(git diff --cached --shortstat)
FILES=$(git diff --cached --name-only)
DIFF_NAMES=$(git diff --cached --diff-filter=M --name-only | tr '\n' ', ' | sed 's/,$//')
echo "diff_stat<<DIFFEOF" >> "$GITHUB_OUTPUT"
echo "$STAT" >> "$GITHUB_OUTPUT"
echo "DIFFEOF" >> "$GITHUB_OUTPUT"
echo "diff_shortstat=$SHORTSTAT" >> "$GITHUB_OUTPUT"
echo "diff_names=$DIFF_NAMES" >> "$GITHUB_OUTPUT"
# Build commit message body
{
echo "commit_body<<BODYEOF"
if [ "${upgrade_ran:-}" = "true" ]; then
echo ""
echo "Dependencies upgraded via bun update."
fi
echo ""
echo "Changed files:"
echo "$FILES" | sed 's/^/ /'
echo ""
echo "$SHORTSTAT"
echo "BODYEOF"
} >> "$GITHUB_OUTPUT"
fi
# ── 7a. Commit directly (default) ──
- name: Commit and push
if: >-
steps.skip_check.outputs.skip != 'true'
&& steps.changes.outputs.has_changes == 'true'
&& inputs.pull_request != 'true'
id: push
env:
GH_TOKEN: ${{ env.TOKEN }}
COMMIT_BODY: ${{ steps.changes.outputs.commit_body }}
DIFF_SHORTSTAT: ${{ steps.changes.outputs.diff_shortstat }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
BRANCH="${GITHUB_REF#refs/heads/}"
# Write commit message to file to avoid quoting issues with multiline content
{
printf '%s\n' "${COMMIT_PREFIX}rebuild dist (${DIFF_SHORTSTAT})"
printf '%s\n' "${COMMIT_BODY}"
} > /tmp/commit-msg.txt
git commit -F /tmp/commit-msg.txt
# Push once — if it fails, another commit landed and a new workflow
# run will handle building that. No rebase: that would change the
# commit and require regenerating the build, causing a loop.
if git push origin "HEAD:${BRANCH}" 2>&1; then
echo "push_result=success" >> "$GITHUB_OUTPUT"
else
echo "push_result=failed" >> "$GITHUB_OUTPUT"
echo "::warning::Push failed (branch moved) — a new workflow run will rebuild"
# Try to cancel this workflow run so it shows as cancelled, not failed
RUN_ID="${{ github.run_id }}"
gh api repos/${{ github.repository }}/actions/runs/${RUN_ID}/cancel -X POST 2>/dev/null || true
# Sleep up to 30s waiting for cancellation to take effect
WAITED=0
while [ "$WAITED" -lt 30 ]; do
sleep 2
WAITED=$((WAITED + 2))
STATUS=$(gh api repos/${{ github.repository }}/actions/runs/${RUN_ID} --jq '.status' 2>/dev/null || echo "unknown")
if [ "$STATUS" = "completed" ] || [ "$STATUS" = "cancelled" ]; then
echo "Workflow cancelled after ${WAITED}s"
exit 0
fi
done
echo "::error::Cancel did not take effect within 30s"
exit 1
fi
# ── 7b. Optionally trigger tag-latest ──
- name: Trigger tag-latest workflow
if: >-
steps.push.outputs.push_result == 'success'
&& inputs.tag_latest == 'true'
env:
GH_TOKEN: ${{ env.TOKEN }}
run: |
BRANCH="${GITHUB_REF#refs/heads/}"
echo "Triggering tag-latest workflow on ${BRANCH}..."
gh workflow run tag-latest.yml --ref "$BRANCH"
echo "tag_latest_triggered=true" >> "$GITHUB_ENV"
# ── 7c. Create PR instead (workflow_dispatch/workflow_call + pull_request flag) ──
- name: Create pull request
if: >-
steps.skip_check.outputs.skip != 'true'
&& steps.changes.outputs.has_changes == 'true'
&& inputs.pull_request == 'true'
id: pr
env:
GH_TOKEN: ${{ env.TOKEN }}
COMMIT_BODY: ${{ steps.changes.outputs.commit_body }}
DIFF_SHORTSTAT: ${{ steps.changes.outputs.diff_shortstat }}
DIFF_STAT: ${{ steps.changes.outputs.diff_stat }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
BRANCH="${GITHUB_REF#refs/heads/}"
PR_BRANCH="build/${BRANCH}-$(date -u +%Y%m%dT%H%M%SZ)"
SUBJECT="${COMMIT_PREFIX}rebuild dist (${DIFF_SHORTSTAT})"
# Write commit message to file
{
printf '%s\n' "$SUBJECT"
printf '%s\n' "${COMMIT_BODY}"
} > /tmp/commit-msg.txt
git checkout -b "$PR_BRANCH"
git commit -F /tmp/commit-msg.txt
git push -u origin "$PR_BRANCH"
# Write PR body to file
{
echo "## Build Output Changes"
echo ""
echo '```'
echo "${DIFF_STAT}"
echo '```'
echo ""
echo "---"
echo "Triggered by workflow_dispatch on \`${BRANCH}\`."
} > /tmp/pr-body.md
PR_URL=$(gh pr create \
--base "$BRANCH" \
--head "$PR_BRANCH" \
--title "$SUBJECT" \
--body-file /tmp/pr-body.md)
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
echo "Created PR: $PR_URL"
# ── 8. Job summary ──
- name: Write job summary
if: steps.skip_check.outputs.skip != 'true'
env:
HAS_CHANGES: ${{ steps.changes.outputs.has_changes }}
DIFF_STAT: ${{ steps.changes.outputs.diff_stat }}
PR_URL: ${{ steps.pr.outputs.pr_url }}
PUSH_RESULT: ${{ steps.push.outputs.push_result }}
run: |
{
echo "## Build Plugin"
echo ""
if [ "$HAS_CHANGES" != "true" ]; then
echo "**No changes.** Build output is already up to date."
elif [ -n "$PR_URL" ]; then
echo "**PR created:** ${PR_URL}"
echo ""
echo '```'
echo "${DIFF_STAT}"
echo '```'
elif [ "$PUSH_RESULT" = "success" ]; then
echo "**Committed and pushed** to \`${GITHUB_REF#refs/heads/}\`."
echo ""
echo '```'
echo "${DIFF_STAT}"
echo '```'
elif [ "$PUSH_RESULT" = "failed" ]; then
echo ":x: **Push failed** — conflict could not be resolved."
fi
if [ "${upgrade_ran:-}" = "true" ]; then
echo ""
echo "> Dependencies were upgraded via \`bun update\`."
fi
if [ "${tag_latest_triggered:-}" = "true" ]; then
echo ""
echo "> Tag-latest workflow was triggered."
fi
} >> "$GITHUB_STEP_SUMMARY"