-
Notifications
You must be signed in to change notification settings - Fork 3
369 lines (321 loc) · 12.9 KB
/
pr-build.yml
File metadata and controls
369 lines (321 loc) · 12.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
name: PR Build
on:
workflow_run:
workflows: ["CI"]
types:
- completed
pull_request_target:
branches: [ main ]
types: [labeled, closed]
push:
branches: [ prbuild ]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to build'
required: false
type: number
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
defaults:
run:
working-directory: cli
env:
GO_VERSION: '1.26.1'
jobs:
check-permission:
name: Check Build Permission
runs-on: ubuntu-latest
if: |
github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request_target' ||
github.event_name == 'push' ||
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
outputs:
allowed: ${{ steps.check.outputs.allowed }}
pr_number: ${{ steps.check.outputs.pr_number }}
pr_sha: ${{ steps.check.outputs.pr_sha }}
steps:
- name: Check if build is allowed
id: check
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
let allowed = false;
let prNumber = null;
let prSha = null;
if (context.eventName === 'workflow_dispatch') {
allowed = true;
prNumber = context.payload.inputs.pr_number;
if (prNumber) {
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
prSha = pr.data.head.sha;
}
core.setOutput('allowed', 'true');
core.setOutput('pr_number', prNumber || '');
core.setOutput('pr_sha', prSha || '');
return;
}
if (context.eventName === 'push') {
allowed = true;
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${context.repo.owner}:${context.ref.replace('refs/heads/', '')}`
});
if (prs.length > 0) {
prNumber = prs[0].number;
prSha = prs[0].head.sha;
}
core.setOutput('allowed', 'true');
core.setOutput('pr_number', prNumber || '');
core.setOutput('pr_sha', prSha || '');
return;
}
if (context.eventName === 'workflow_run') {
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${context.repo.owner}:${context.payload.workflow_run.head_branch}`
});
if (prs.length > 0) {
const pr = prs[0];
const association = pr.author_association;
const trustedAssociations = ['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'];
if (trustedAssociations.includes(association)) {
allowed = true;
prNumber = pr.number;
prSha = pr.head.sha;
} else {
const hasLabel = pr.labels.some(label => label.name === 'safe-to-build');
if (hasLabel) {
allowed = true;
prNumber = pr.number;
prSha = pr.head.sha;
}
}
}
}
if (context.eventName === 'pull_request_target') {
const hasLabel = context.payload.pull_request.labels.some(
label => label.name === 'safe-to-build'
);
if (hasLabel) {
allowed = true;
prNumber = context.payload.pull_request.number;
prSha = context.payload.pull_request.head.sha;
}
}
core.setOutput('allowed', allowed ? 'true' : 'false');
core.setOutput('pr_number', prNumber || '');
core.setOutput('pr_sha', prSha || '');
if (!allowed) {
core.notice('Build skipped - requires maintainer approval. Add "safe-to-build" label to proceed.');
}
build-pr:
name: Build PR Preview
runs-on: ubuntu-latest
timeout-minutes: 20
needs: check-permission
if: needs.check-permission.outputs.allowed == 'true'
permissions:
contents: write
pull-requests: write
steps:
- name: Get PR details
id: pr
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
let prNumber = '${{ needs.check-permission.outputs.pr_number }}';
if (!prNumber) {
const ref = context.ref.replace('refs/heads/', '');
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${context.repo.owner}:${ref}`
});
if (prs.length > 0) {
prNumber = prs[0].number;
} else {
core.setFailed('No PR found for this branch.');
return;
}
}
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
core.setOutput('number', prNumber);
core.setOutput('sha', pr.data.head.sha);
core.setOutput('ref', pr.data.head.ref);
core.setOutput('title', pr.data.title);
- name: Checkout code
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
with:
ref: ${{ steps.pr.outputs.sha }}
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: '${{ env.GO_VERSION }}'
cache: true
cache-dependency-path: cli/go.sum
- name: Calculate PR version
id: version
working-directory: cli
run: |
BASE_VERSION=$(grep '^version:' extension.yaml | awk '{print $2}')
PR_NUMBER="${{ steps.pr.outputs.number }}"
PR_VERSION="${BASE_VERSION}-pr${PR_NUMBER}"
STANDARD_TAG="azd-ext-jongio-azd-copilot_${PR_VERSION}"
echo "version=$PR_VERSION" >> $GITHUB_OUTPUT
echo "base_version=$BASE_VERSION" >> $GITHUB_OUTPUT
echo "tag=$STANDARD_TAG" >> $GITHUB_OUTPUT
echo "Building version: $PR_VERSION with tag: $STANDARD_TAG"
- name: Install azd
run: curl -fsSL https://aka.ms/install-azd.sh | bash
- name: Install azd extensions
run: |
azd extension source add azd --location https://aka.ms/azd/extensions/registry --type url 2>/dev/null || true
azd extension install microsoft.azd.extensions --source azd
- name: Update extension.yaml with PR version
working-directory: cli
run: |
VERSION="${{ steps.version.outputs.version }}"
sed -i "s/^version: .*/version: ${VERSION}/" extension.yaml
- name: Build binaries
working-directory: cli
run: |
export EXTENSION_ID="jongio.azd.copilot"
export EXTENSION_VERSION="${{ steps.version.outputs.version }}"
azd x build --all
- name: Package
working-directory: cli
run: azd x pack
- name: Create PR pre-release
working-directory: cli
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION="${{ steps.version.outputs.version }}"
TAG="azd-ext-jongio-azd-copilot_${VERSION}"
PR_NUM="${{ steps.pr.outputs.number }}"
gh release delete "$TAG" --repo "${{ github.repository }}" --yes 2>/dev/null || true
gh release create "$TAG" \
--repo "${{ github.repository }}" \
--title "PR #${PR_NUM} Preview (v${VERSION})" \
--notes "PR #${PR_NUM} preview build. Test using the one-line installer from the PR comment." \
--prerelease \
~/.azd/registry/jongio.azd.copilot/${VERSION}/*
- name: Update registry
working-directory: cli
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION="${{ steps.version.outputs.version }}"
echo '{"extensions":[]}' > /tmp/pr-registry.json
azd x publish \
--registry /tmp/pr-registry.json \
--version "$VERSION" \
--repo "${{ github.repository }}"
TAG="azd-ext-jongio-azd-copilot_${VERSION}"
gh release upload "$TAG" --repo "${{ github.repository }}" /tmp/pr-registry.json --clobber
- name: Generate installation instructions
id: instructions
run: |
VERSION="${{ steps.version.outputs.version }}"
PR_NUM="${{ steps.pr.outputs.number }}"
TAG="${{ steps.version.outputs.tag }}"
REPO="${{ github.repository }}"
COMMIT="${{ steps.pr.outputs.sha }}"
SHORT_COMMIT=$(echo $COMMIT | cut -c1-7)
BRANCH="${{ github.ref_name }}"
cat > ../instructions.md <<EOF
## 🚀 Test This PR
A preview build (\`$VERSION\`) is ready for testing!
### One-Line Install (Recommended)
**PowerShell (Windows):**
\`\`\`powershell
iex "& { \$(irm https://raw.githubusercontent.com/$REPO/$BRANCH/cli/scripts/install-pr.ps1) } -PrNumber $PR_NUM -Version $VERSION"
\`\`\`
**Bash (macOS/Linux):**
\`\`\`bash
curl -fsSL https://raw.githubusercontent.com/$REPO/$BRANCH/cli/scripts/install-pr.sh | bash -s $PR_NUM $VERSION
\`\`\`
### Uninstall
**PowerShell (Windows):**
\`\`\`powershell
iex "& { \$(irm https://raw.githubusercontent.com/$REPO/$BRANCH/cli/scripts/uninstall-pr.ps1) } -PrNumber $PR_NUM"
\`\`\`
**Bash (macOS/Linux):**
\`\`\`bash
curl -fsSL https://raw.githubusercontent.com/$REPO/$BRANCH/cli/scripts/uninstall-pr.sh | bash -s $PR_NUM
\`\`\`
---
**Build Info:**
- **Version:** \`$VERSION\`
- **Commit:** \`$SHORT_COMMIT\`
- **Release:** [View pre-release](https://github.com/$REPO/releases/tag/$TAG)
EOF
- name: Comment on PR
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const fs = require('fs');
const instructions = fs.readFileSync('instructions.md', 'utf8');
const prNumber = '${{ steps.pr.outputs.number }}';
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('🚀 Test This PR')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: instructions
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: instructions
});
}
cleanup:
name: Cleanup PR Registry
runs-on: ubuntu-latest
if: github.event.action == 'closed' && github.event_name == 'pull_request_target'
permissions:
contents: write
defaults:
run:
working-directory: .
steps:
- name: Delete PR registry release
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUM=${{ github.event.pull_request.number }}
gh release list --repo ${{ github.repository }} --json tagName,name --limit 100 | \
jq -r ".[] | select(.tagName | contains(\"-pr${PR_NUM}\")) | .tagName" | \
while read tag; do
echo "Deleting release: $tag"
gh release delete "$tag" --repo ${{ github.repository }} --yes || true
git push --delete origin "$tag" 2>/dev/null || true
done