-
Notifications
You must be signed in to change notification settings - Fork 231
189 lines (174 loc) · 6.66 KB
/
Copy pathcustom-image.yaml
File metadata and controls
189 lines (174 loc) · 6.66 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
name: Build Custom Docker Image
on:
workflow_dispatch:
inputs:
image:
description: 'Image to build'
required: false
default: 'nativelink'
type: choice
options:
- nativelink
- nativelink-worker-init
- nativelink-worker-lre-cc
skip_signing:
description: 'Skip cosign signing'
required: false
default: true
type: boolean
issue_comment:
types: [created]
permissions:
contents: read
jobs:
check-trigger:
runs-on: ubuntu-24.04
permissions:
issues: write
pull-requests: write
outputs:
should_build: ${{ steps.check.outputs.should_build }}
pr_sha: ${{ steps.check.outputs.pr_sha }}
image: ${{ steps.check.outputs.image }}
multi-arch: ${{ steps.check.outputs.multi-arch }}
components: ${{ steps.check.outputs.components }}
steps:
- name: Checkout
uses: >- # v6.0.2
actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
- name: Check trigger
id: check
uses: >- #v8.0.0
actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd
with:
script: |
const fs = require('fs');
const matrix = JSON.parse(fs.readFileSync('.github/images-matrix.json', 'utf8'));
const validImages = matrix['include'].map((o) => o['image']);
// Never fail the job over a courtesy comment (e.g. a read-only
// token); the build is what matters.
const tryComment = async (body) => {
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body,
});
} catch (e) {
core.warning(`Could not post comment: ${e.message}`);
}
};
let image;
let prSha;
if (context.eventName === 'workflow_dispatch') {
image = '${{ inputs.image }}';
prSha = context.sha;
} else if (context.eventName === 'issue_comment') {
const body = context.payload.comment.body.trim();
const isPR = !!context.payload.issue.pull_request;
// Match /build-image or /build-image <image-name>
const match = body.match(/^\/build-image(?:\s+(\S+))?/i);
if (!isPR || !match) {
core.setOutput('should_build', 'false');
return;
}
image = match[1];
if (!image || !validImages.includes(image)) {
const hint = image
? `Unknown image: \`${image}\``
: 'Specify an image to build.';
await tryComment(
`${hint}\n\nValid options: ${validImages.map((i) => `\`${i}\``).join(', ')}`
);
core.setOutput('should_build', 'false');
return;
}
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number,
});
prSha = pr.head.sha;
try {
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket',
});
} catch (e) {
core.warning(`Could not add reaction: ${e.message}`);
}
} else {
core.setOutput('should_build', 'false');
return;
}
const option = matrix['include'].find((o) => o['image'] === image);
core.setOutput('should_build', 'true');
core.setOutput('pr_sha', prSha);
core.setOutput('image', image);
core.setOutput('multi-arch', option?.['multi-arch'] ?? false);
core.setOutput('components', option?.['components'] ?? '');
build-image:
name: Build and Push Image
needs: check-trigger
if: needs.check-trigger.outputs.should_build == 'true'
runs-on: ubuntu-24.04
permissions:
contents: read
issues: write
pull-requests: write
timeout-minutes: 45
steps:
- name: Checkout
uses: >- # v6.0.2
actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
ref: ${{ needs.check-trigger.outputs.pr_sha }}
- name: Prepare Worker
uses: ./.github/actions/prepare-nix
with:
nativelink_attic_token: ${{ secrets.NATIVELINK_ATTIC_TOKEN }}
- id: version
uses: ./.github/actions/generate-version
- name: Upload image
uses: ./.github/actions/test-and-upload-image
with:
testing: false
image: ${{ needs.check-trigger.outputs.image }}
multi-arch: ${{ needs.check-trigger.outputs.multi-arch }}
components: ${{ needs.check-trigger.outputs.components }}
tag: ${{ steps.version.outputs.version-string }}
GHCR_USERNAME: ${{ vars.GHCR_PUBLISH_USER }}
GHCR_PASSWORD: ${{ secrets.GHCR_PUBLISH_TOKEN }}
force-push: true
- name: Output image info
run: |
echo "### Published Image" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "ghcr.io/${{ github.repository_owner }}/${{ needs.check-trigger.outputs.image }}:${{ steps.version.outputs.version-string }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
- name: Comment on PR
if: github.event_name == 'issue_comment'
uses: >- #v8.0.0
actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd
env:
IMAGE_REF: ghcr.io/${{ github.repository_owner }}/${{ needs.check-trigger.outputs.image }}:${{ steps.version.outputs.version-string }}
with:
script: |
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `Image built and pushed!\n\n\`\`\`\n${process.env.IMAGE_REF}\n\`\`\``,
});
} catch (e) {
core.warning(`Could not post comment: ${e.message}`);
}
- name: Teardown Worker
uses: ./.github/actions/end-nix
if: always()
with:
nativelink_attic_token: ${{ secrets.NATIVELINK_ATTIC_TOKEN }}