-
-
Notifications
You must be signed in to change notification settings - Fork 71
381 lines (359 loc) · 16.3 KB
/
Build.yml
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
370
371
372
373
374
375
376
377
378
379
380
381
on:
push:
branches:
# for bors r+
- staging
# for bors try
- trying
workflow_dispatch:
inputs:
no_cache:
description: Disable Docker cache, yes/no
default: 'no'
required: false
toolchain:
description: Rust toolchain to install
default: 'stable'
required: false
arch:
description: Architectures to build for
default: ''
required: false
schedule:
- cron: '0 0 1 */2 *'
name: Build
jobs:
setup:
name: Setup
runs-on: ubuntu-latest
outputs:
arch: ${{ steps.generate-matrix.outputs.arch }}
target: ${{ steps.generate-matrix.outputs.target }}
run-build: ${{ steps.generate-matrix.outputs.run-build }}
fail-fast: ${{ steps.generate-matrix.outputs.fail-fast }}
steps:
- uses: actions/setup-node@v4
with:
node-version: 16
- run: npm install js-yaml
- name: Generate matrix
id: generate-matrix
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const yaml = require('js-yaml')
const archMatrix = yaml.load(process.env.ARCH_MATRIX)
const targetMatrix = yaml.load(process.env.TARGET_MATRIX)
if (context.eventName == 'schedule') {
// Always run on schedule
core.setOutput('arch', JSON.stringify(archMatrix))
core.setOutput('target', JSON.stringify(targetMatrix))
} else if (context.eventName == 'workflow_dispatch') {
let outputArchMatrix = []
let outputTargetMatrix = []
const dockerArch = context.payload.inputs.arch.split(',')
if (dockerArch.length > 0) {
for (const arch of dockerArch) {
outputArchMatrix.push(...archMatrix.filter(item => item.arch == arch))
}
} else {
outputArchMatrix = archMatrix
}
const toolchain = context.payload.inputs.toolchain
for (const target of targetMatrix) {
if (target.TOOLCHAIN == 'nightly' && toolchain != 'nightly') {
outputTargetMatrix.push(target)
} else {
outputTargetMatrix.push({...target, TOOLCHAIN: toolchain})
}
}
core.setOutput('arch', JSON.stringify(outputArchMatrix))
core.setOutput('target', JSON.stringify(outputTargetMatrix))
core.setOutput('fail-fast', 'false')
} else if (context.eventName == 'push') {
const commitMessage = process.env.COMMIT_MESSAGE.trim()
if (commitMessage.length > 0) {
let outputArchMatrix = []
let outputTargetMatrix = []
let borsArgs
if (commitMessage.startsWith('Try #')) {
borsArgs = commitMessage.replace(/Try #[0-9]+:/i, '').trim()
} else {
// Merge example commit message:
// Merge #64
//
// 64: Refine bors command r=messense a=messense
//
// bors: amd64 --target aarch64
//
// Co-authored-by: messense <[email protected]>
//
borsArgs = commitMessage
.split('\n')
.filter(item => item.trim().startsWith('bors:'))
.join('\n')
.trim()
}
const targetIndex = borsArgs.indexOf('--target')
let dockerArch = []
let targets = []
if (targetIndex === -1) {
dockerArch = borsArgs.trim().split(' ').filter(x => x.length > 0)
targets = []
} else {
dockerArch = borsArgs.substring(0, targetIndex).trim().split(' ').filter(x => x.length > 0)
targets = borsArgs.substring(targetIndex + '--target'.length).trim().split(' ')
}
if (dockerArch.length === 0) {
// Defaults to all arches
outputArchMatrix = archMatrix
} else {
for (const arch of dockerArch) {
outputArchMatrix.push(...archMatrix.filter(item => item.arch == arch))
}
}
if (targets.length === 0) {
// Defaults to all targets
outputTargetMatrix = targetMatrix
} else {
for (const target of targets) {
outputTargetMatrix.push(...targetMatrix.filter(item => item.TARGET.startsWith(target)))
}
}
core.setOutput('arch', JSON.stringify(outputArchMatrix))
core.setOutput('target', JSON.stringify(outputTargetMatrix))
} else {
core.setOutput('arch', JSON.stringify(archMatrix))
core.setOutput('target', JSON.stringify(targetMatrix))
core.setOutput('run-build', 'false')
}
const matches = commitMessage.match(/(Try|Merge) #([0-9]+):/)
if (matches) {
const prNumber = matches[2]
const { data: { labels: labels } } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
})
const labelNames = labels.map(label => label.name)
if (labelNames.includes('CI-no-fail-fast')) {
core.setOutput('fail-fast', 'false')
}
}
} else {
core.setOutput('arch', JSON.stringify(archMatrix))
core.setOutput('target', JSON.stringify(targetMatrix))
core.setOutput('run-build', 'false')
}
- name: Show build matrix
run: |
echo '${{ toJson(steps.generate-matrix.outputs.arch) }}'
echo '${{ toJson(steps.generate-matrix.outputs.target) }}'
echo run build: ${{ steps.generate-matrix.outputs.run-build || 'true' }}
echo fail fast: ${{ steps.generate-matrix.outputs.fail-fast || 'true' }}
env:
COMMIT_MESSAGE: >
${{
((
(startsWith(github.event.head_commit.message, 'Try #') || startsWith(github.event.head_commit.message, 'Merge #')) &&
github.event.head_commit.author.username == 'bors[bot]'
) && github.event.head_commit.message) || ''
}}
ARCH_MATRIX: |
- arch: amd64
runner: [ubuntu-latest]
- arch: arm64
runner: [self-hosted, ARM64]
- arch: armv7
runner: [self-hosted, ARM64]
docker_arch: arm/v7
TARGET_MATRIX: |
- IMAGE_TAG: aarch64-musl
TARGET: aarch64-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak.aarch64
TOOLCHAIN: stable
- IMAGE_TAG: arm-musleabi
TARGET: arm-unknown-linux-musleabi
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: arm-musleabihf
TARGET: arm-unknown-linux-musleabihf
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: armv5te-musleabi
TARGET: armv5te-unknown-linux-musleabi
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: armv7-musleabi
TARGET: armv7-unknown-linux-musleabi
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: armv7-musleabihf
TARGET: armv7-unknown-linux-musleabihf
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: i586-musl
TARGET: i586-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: i686-musl
TARGET: i686-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
- IMAGE_TAG: mips-musl
TARGET: mips-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: nightly
- IMAGE_TAG: mips64-muslabi64
TARGET: mips64-unknown-linux-muslabi64
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: nightly
- IMAGE_TAG: mips64el-muslabi64
TARGET: mips64el-unknown-linux-muslabi64
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: nightly
- IMAGE_TAG: mipsel-musl
TARGET: mipsel-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: nightly
- IMAGE_TAG: powerpc64le-musl
TARGET: powerpc64le-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: nightly
- IMAGE_TAG: s390x-musl
TARGET: s390x-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: nightly
- IMAGE_TAG: x86_64-musl
TARGET: x86_64-unknown-linux-musl
RUST_MUSL_MAKE_CONFIG: config.mak
TOOLCHAIN: stable
build:
name: Build - ${{ matrix.os.arch }} - ${{ matrix.env.IMAGE_TAG }}
runs-on: ${{ matrix.os.runner }}
if: ${{ needs.setup.outputs.run-build != 'false' }}
needs: setup
strategy:
fail-fast: ${{ needs.setup.outputs.fail-fast != 'false' }}
matrix:
os: ${{ fromJson(needs.setup.outputs.arch) }}
env: ${{ fromJson(needs.setup.outputs.target) }}
env: ${{ matrix.env }}
steps:
- uses: actions/checkout@v4
- name: Setup QEMU
uses: dbhi/qus/action@main
if: ${{ contains(matrix.os.runner, 'ubuntu-latest') }}
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
if: ${{ github.repository_owner == 'rust-cross' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: docker build
uses: docker/build-push-action@v6
with:
platforms: linux/${{ matrix.os.docker_arch || matrix.os.arch }}
build-args: |
TARGET=${{ matrix.env.TARGET }}
RUST_MUSL_MAKE_CONFIG=${{ matrix.env.RUST_MUSL_MAKE_CONFIG }}
TOOLCHAIN=${{ matrix.env.TOOLCHAIN }}
tags: ghcr.io/${{ github.repository_owner }}/rust-musl-cross:${{ matrix.env.IMAGE_TAG }}-${{ matrix.os.arch }}
no-cache: ${{ github.event.inputs.no_cache == 'yes' }}
context: .
load: true
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/rust-musl-cross:buildcache-${{ matrix.env.IMAGE_TAG }}-${{ matrix.os.arch }}
cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/rust-musl-cross:buildcache-${{ matrix.env.IMAGE_TAG }}-${{ matrix.os.arch }},mode=max
- name: Test Docker cargo build
if: ${{ matrix.os.arch == 'amd64' && !startsWith(matrix.env.TARGET, 'armv5te') && !startsWith(matrix.env.TARGET, 's390x') }}
run: |
docker run --rm \
-v "$(pwd)/tests":/home/rust/src \
ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-${{ matrix.os.arch }} bash -c "cargo build"
- name: Build and push multiarch image
env:
DOCKER_CLI_EXPERIMENTAL: enabled
run: |
set -e
docker push ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-${{ matrix.os.arch }}
curl -sqL -o manifest-tool https://github.com/estesp/manifest-tool/releases/download/v1.0.3/manifest-tool-linux-${{ matrix.os.arch }}
chmod +x manifest-tool
echo "image: ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG
manifests:" > ghcr-manifest.yaml
if docker manifest inspect ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-amd64 > /dev/null; then
echo " - image: ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-amd64
platform:
architecture: amd64
os: linux" >> ghcr-manifest.yaml
fi
if docker manifest inspect ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-arm64 > /dev/null; then
echo " - image: ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-arm64
platform:
architecture: arm64
os: linux" >> ghcr-manifest.yaml
fi
if docker manifest inspect ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-armv7 > /dev/null; then
echo " - image: ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-armv7
platform:
architecture: arm
variant: v7
os: linux" >> ghcr-manifest.yaml
fi
cat ghcr-manifest.yaml
./manifest-tool push from-spec ghcr-manifest.yaml
echo "image: ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$TARGET" > ghcr-manifest-target.yaml
sed -n '1d;p' ghcr-manifest.yaml >> ghcr-manifest-target.yaml
./manifest-tool push from-spec ghcr-manifest-target.yaml
- name: Sync images to Docker Hub
if: ${{ github.repository_owner == 'rust-cross' }}
env:
DOCKER_CLI_EXPERIMENTAL: enabled
run: |
set -e
echo "image: ${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG
manifests:" > dockerhub-manifest.yaml
if docker manifest inspect ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-amd64 > /dev/null; then
docker run --rm quay.io/skopeo/stable:latest copy --src-creds ${{ github.repository_owner }}:${{ secrets.GITHUB_TOKEN }} --dest-creds ${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }} --retry-times 3 docker://ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-amd64 docker://${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG-amd64
echo " - image: ${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG-amd64
platform:
architecture: amd64
os: linux" >> dockerhub-manifest.yaml
fi
if docker manifest inspect ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-arm64 > /dev/null; then
docker run --rm quay.io/skopeo/stable:latest copy --src-creds ${{ github.repository_owner }}:${{ secrets.GITHUB_TOKEN }} --dest-creds ${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }} --retry-times 3 docker://ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-arm64 docker://${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG-arm64
echo " - image: ${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG-arm64
platform:
architecture: arm64
os: linux" >> dockerhub-manifest.yaml
fi
if docker manifest inspect ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-armv7 > /dev/null; then
docker run --rm quay.io/skopeo/stable:latest copy --src-creds ${{ github.repository_owner }}:${{ secrets.GITHUB_TOKEN }} --dest-creds ${{ secrets.DOCKER_USERNAME }}:${{ secrets.DOCKER_PASSWORD }} --retry-times 3 docker://ghcr.io/${{ github.repository_owner }}/rust-musl-cross:$IMAGE_TAG-armv7 docker://${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG-armv7
echo " - image: ${{ secrets.DOCKER_USERNAME }}/rust-musl-cross:$IMAGE_TAG-armv7
platform:
architecture: arm
variant: v7
os: linux" >> dockerhub-manifest.yaml
fi
cat dockerhub-manifest.yaml
./manifest-tool push from-spec dockerhub-manifest.yaml
conclusion:
needs: [setup, build]
if: always()
runs-on: ubuntu-latest
steps:
- name: Result
run: |
jq -C <<< "${needs}"
# Check if all needs were successful or skipped.
"$(jq -r 'all(.result as $result | (["success", "skipped"] | contains([$result])))' <<< "${needs}")"
env:
needs: ${{ toJson(needs) }}