-
Notifications
You must be signed in to change notification settings - Fork 1
566 lines (489 loc) · 18.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Build
on:
push:
branches:
- main
pull_request:
branches:
- main
permissions:
contents: read
checks: write
pull-requests: write
issues: write
packages: write
env:
CARGO_TERM_COLOR: always
# set this to true in GitHub variables to enable building the container
# HAS_CONTAINER: true
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
# just a name, but storing it separately as we're nice people
DOCKER_IMAGE_ARTIFACT_NAME: Docker image
DOCKER_IMAGE_OUTPUT_LOCATION: /tmp
DOCKER_IMAGE_TAR_LOCATION: /tmp/docker-image.tar # notice /tmp same as DOCKER_IMAGE_OUTPUT_LOCATION
concurrency:
# each new commit to a PR runs this workflow
# so we need to avoid a long running older one from overwriting the "pr-<number>-latest"
group: "${{ github.workflow }} @ ${{ github.ref_name }}"
cancel-in-progress: true
jobs:
repo-has-container:
name: Repo has container?
runs-on: ubuntu-latest
outputs:
has_container: ${{ steps.determine.outputs.has_container }}
steps:
- name: Repo has docker container?
id: determine
shell: bash
run: |
HAS_CONTAINER="${{ vars.HAS_CONTAINER }}"
echo "has_container=${HAS_CONTAINER:-false}" >> ${GITHUB_OUTPUT}
changes:
name: Detect changes
runs-on: ubuntu-latest
outputs:
code: ${{ steps.filter.outputs.code }}
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Check if we actually made changes
uses: dorny/paths-filter@4512585405083f25c027a35db413c2b3b9006d50 # tag=v2.11.1
id: filter
with:
token: ${{ secrets.GITHUB_TOKEN }}
filters: .github/file-filters.yml
calculate-version:
name: Calculate version
runs-on: ubuntu-latest
needs:
- changes
- repo-has-container
outputs:
version: ${{ steps.version.outputs.nextversion }}
if: |
github.event_name == 'pull_request' &&
fromJSON(needs.repo-has-container.outputs.has_container) == true &&
fromJSON(needs.changes.outputs.code) == true
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
fetch-depth: 0
- name: Cache dependencies
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
env:
CACHE_NAME: cargo-cache-dependencies
with:
path: |
~/.cargo
./target
key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-cocogitto
restore-keys: |
${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-
${{ runner.os }}-build-${{ env.CACHE_NAME }}-
- name: Set up toolchain
shell: bash
run: |
rm ${HOME}/.cargo/bin/rustfmt
rm ${HOME}/.cargo/bin/cargo-fmt
rustup update
cargo --version
- name: Get binstall
shell: bash
run: |
cd /tmp
archive="cargo-binstall-x86_64-unknown-linux-musl.tgz"
wget "https://github.com/cargo-bins/cargo-binstall/releases/latest/download/${archive}"
tar -xvf "./${archive}"
rm "./${archive}"
mv ./cargo-binstall ~/.cargo/bin/
- name: Install cocogitto to get the next version number
shell: bash
run: |
cargo binstall --no-confirm cocogitto --target x86_64-unknown-linux-musl --pkg-url "{ repo }/releases/download/{ version }/{ name }-{ version }-{ target }.tar.gz" --bin-dir "{ bin }" --pkg-fmt tgz
- name: Calculate next version
id: version
shell: bash
run: |
VERSION="$(cog bump --auto --dry-run || true)"
if [[ -z $VERSION ]]; then
VERSION="$(git tag --points-at "$(git rev-list --tags --max-count=1)" | sort --reverse | head --lines 1)"
echo "No version generated, defaulting to latest git tag: ${VERSION}"
else
echo "New version: ${VERSION}"
fi
# remove v
VERSION="${VERSION//v/}"
# store
echo "nextversion=${VERSION}" >> ${GITHUB_OUTPUT}
cargo-build:
name: Cargo build
runs-on: ubuntu-latest
needs:
- changes
if: |
github.event_name == 'pull_request' &&
fromJSON(needs.changes.outputs.code) == true
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Cache dependencies
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
env:
CACHE_NAME: cargo-cache-dependencies
with:
path: |
~/.cargo
./target
key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-build
restore-keys: |
${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-
${{ runner.os }}-build-${{ env.CACHE_NAME }}-
- name: Set up toolchain
shell: bash
run: |
rm ${HOME}/.cargo/bin/rustfmt
rm ${HOME}/.cargo/bin/cargo-fmt
rustup update
cargo --version
- name: Build
shell: bash
run: |
cargo build --all-targets --workspace --verbose
cargo-fmt:
name: Cargo fmt
runs-on: ubuntu-latest
needs:
- changes
if: |
github.event_name == 'pull_request' &&
fromJSON(needs.changes.outputs.code) == true
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Cache dependencies
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
env:
CACHE_NAME: cargo-cache-dependencies
with:
path: |
~/.cargo
./target
key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-fmt
restore-keys: |
${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-
${{ runner.os }}-build-${{ env.CACHE_NAME }}-
- name: Set up toolchain
shell: bash
run: |
rm ${HOME}/.cargo/bin/rustfmt
rm ${HOME}/.cargo/bin/cargo-fmt
rustup update
cargo --version
- name: Check formatting
shell: bash
run: |
cargo fmt --all -- --check --verbose
cargo-test-and-report:
name: Cargo test (and report)
runs-on: ubuntu-latest
needs:
- changes
if: fromJSON(needs.changes.outputs.code) == true
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Cache dependencies
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
env:
CACHE_NAME: cargo-cache-dependencies
with:
path: |
~/.cargo
./target
key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-test
restore-keys: |
${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-
${{ runner.os }}-build-${{ env.CACHE_NAME }}-
- name: Set up toolchain
shell: bash
run: |
rm ${HOME}/.cargo/bin/rustfmt
rm ${HOME}/.cargo/bin/cargo-fmt
rustup update
cargo --version
- name: Install llvm-tools-preview
shell: bash
run: |
rustup component add llvm-tools-preview
- name: Get binstall
shell: bash
run: |
archive="cargo-binstall-x86_64-unknown-linux-musl.tgz"
wget "https://github.com/ryankurte/cargo-binstall/releases/latest/download/${archive}"
tar -xvf "./${archive}"
rm "./${archive}"
mv ./cargo-binstall ~/.cargo/bin/
- name: Install nextest, custom test runner, with native support for junit
shell: bash
run: |
cargo binstall --no-confirm cargo-nextest;
- name: Install grcov
shell: bash
run: |
cargo binstall --no-confirm grcov --pkg-url "{ repo }/releases/download/v{ version }/{ name }-{ target }.tar.bz2" --pkg-fmt tbz2 --bin-dir "{ bin }";
- name: Build with instrumentation support
shell: bash
env:
RUSTFLAGS: "-C instrument-coverage"
run: |
cargo build --all-targets --workspace --verbose
- name: Run nextest
shell: bash
id: tests
env:
RUSTFLAGS: "-C instrument-coverage"
LLVM_PROFILE_FILE: "profiling/profile-%p-%m.profraw"
run: |
cargo nextest run --profile ci --no-fail-fast --all-targets --workspace
continue-on-error: true
- name: Upload test results
uses: EnricoMi/publish-unit-test-result-action@ca89ad036b5fcd524c1017287fb01b5139908408 # v2.11.0
with:
check_name: Test results
github_token: ${{ secrets.GITHUB_TOKEN }}
junit_files: reports/results.xml
- name: Run grcov
shell: bash
run: |
grcov $(find profiling -name "profile-*.profraw" -print) --source-dir . --binary-path ./target/debug/ --output-type lcov --branch --ignore-not-existing --llvm --keep-only "src/**" --keep-only "tests/**" --output-path ./reports/lcov.info
- name: Upload to Coveralls
uses: coverallsapp/github-action@3dfc5567390f6fa9267c0ee9c251e4c8c3f18949 # v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: reports/lcov.info
- name: Fail if tests failed
shell: bash
if: steps.tests.outcome != 'success'
run: |
# the test reporter we use (or any for that matter)
# all show a report. But we cannot depend on that report because
# we don't know which subsection it belongs in GitHub
# so we explicitly fail this one
# which will fail All Done
exit 1;
cargo-clippy-and-report:
name: Cargo clippy (and report)
runs-on: ubuntu-latest
needs:
- changes
if: |
github.event_name == 'pull_request' &&
fromJSON(needs.changes.outputs.code) == true
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Cache dependencies
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
env:
CACHE_NAME: cargo-cache-dependencies
with:
path: |
~/.cargo
./target
key: ${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-clippy
restore-keys: |
${{ runner.os }}-build-${{ env.CACHE_NAME }}-${{ hashFiles('Cargo.lock') }}-
${{ runner.os }}-build-${{ env.CACHE_NAME }}-
- name: Set up toolchain
shell: bash
run: |
rm ${HOME}/.cargo/bin/rustfmt
rm ${HOME}/.cargo/bin/cargo-fmt
rustup update
cargo --version
- name: Run Clippy for GitHub Actions report
uses: actions-rs-plus/clippy-check@58bb2527088f0a574f000fad0244d021aeebc55a # v2.1.1
with:
args: --workspace --all-targets --all-features
docker-build:
name: Build Docker container
runs-on: ubuntu-latest
needs:
- calculate-version
outputs:
image_id_with_sha256: ${{ steps.build.outputs.imageid }}
# if: ... is not needed because calculate-version will not run if we disable building the docker container
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Set the Cargo.toml version before we copy in the data into the Docker container
shell: bash
run: |
./.github/scripts/update-version.sh ${{ needs.calculate-version.outputs.version }}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
# TODO validate no changes between github.event.pull_request.head.sha and the actual current sha (representing the hypothetical merge)
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=pr,suffix=-latest
type=raw,value=pr-${{ github.event.pull_request.base.sha }}-${{ github.event.pull_request.head.sha }}
labels: |
org.opencontainers.image.version=pr-${{ github.event.number }}
org.opencontainers.image.source=${{ github.event.pull_request.html_url }}
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Lowercase the image name
shell: bash
run: |
echo "IMAGE_NAME=${IMAGE_NAME,,}" >> ${GITHUB_ENV}
- name: Set application name
shell: bash
run: |
APPLICATION_NAME=${{ github.repository }}
echo "APPLICATION_NAME=${APPLICATION_NAME##*/}" >> ${GITHUB_ENV}
- name: Build Docker image
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
id: build
with:
build-args: |
APPLICATION_NAME=${{ env.APPLICATION_NAME }}
context: .
# this container is THE PR's artifact, and we will re-tag it
# once the PR has been accepted
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
outputs: type=docker,dest=${{ env.DOCKER_IMAGE_TAR_LOCATION }}
- name: Upload artifact
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: ${{ env.DOCKER_IMAGE_ARTIFACT_NAME }}
path: ${{ env.DOCKER_IMAGE_TAR_LOCATION }}
docker-integration-test:
name: Docker integration test
runs-on: ubuntu-latest
needs:
- changes
- docker-build
if: |
(needs.changes.outputs.code == 'true')
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
- name: Download artifact
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: ${{ env.DOCKER_IMAGE_ARTIFACT_NAME }}
path: ${{ env.DOCKER_IMAGE_OUTPUT_LOCATION }}
- name: Load image from artifact
shell: bash
run: |
docker load --input ${{ env.DOCKER_IMAGE_TAR_LOCATION }}
- name: Run integration tests
shell: bash
env:
DOCKER_BUILDKIT: "1" # https://github.com/moby/buildkit/issues/1945#issuecomment-763705334
IMAGE_ID_WITH_SHA256: ${{ needs.docker-build.outputs.image_id_with_sha256 }}
run: |
# IMAGE_ID_WITH_SHA256 looks like sha256:0ae0dc0eb5fbada1c6083b078a168422b4d67227bb762340857c095008c3cbb3
# we only need the second part so we're splitting it at the :
export IMAGE_ID=${IMAGE_ID_WITH_SHA256#*:}
cd integration-tests
./tests.sh
docker-publish:
name: Publish Docker container
runs-on: ubuntu-latest
needs:
- cargo-build
- cargo-fmt
- cargo-test-and-report
- cargo-clippy-and-report
- docker-build
- docker-integration-test
# Check if the event is not triggered by a fork
if: github.event.pull_request.head.repo.full_name == github.repository && github.event_name == 'pull_request'
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
- name: Download artifact
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: ${{ env.DOCKER_IMAGE_ARTIFACT_NAME }}
path: ${{ env.DOCKER_IMAGE_OUTPUT_LOCATION }}
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Load image from artifact
shell: bash
run: |
docker load --input ${{ env.DOCKER_IMAGE_TAR_LOCATION }}
- name: Push image with all tags
shell: bash
run: |
docker push ${REGISTRY,,}/${IMAGE_NAME,,} --all-tags
all-done:
name: All done
# this is the job that should be marked as required on GitHub. It's the only one that'll reliably trigger
# when any upstream fails: success
# when all upstream skips: pass
# when all upstream success: success
# combination of upstream skip and success: success
runs-on: ubuntu-latest
needs:
- calculate-version
- cargo-build
- cargo-fmt
- cargo-clippy-and-report
- cargo-test-and-report
- docker-build
- docker-publish
if: always()
steps:
- name: Fail!
shell: bash
if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
run: |
echo "One / more upstream failed or was cancelled. Failing job..."
exit 1
- name: Success!
shell: bash
run: |
echo "Great success!"