summaryrefslogtreecommitdiff
path: root/.github/workflows/publish-linux-ci-images.yml
blob: 8a38c5c42192c1ffd6f72ae3b7ab82f8a1582549 (plain)
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
name: Publish Linux CI Images

on:
  workflow_call:
    inputs:
      publication_target:
        description: "Image destination: internal or public-release"
        required: false
        type: string
        default: "internal"
      mode:
        description: "Refresh or rollback"
        required: false
        type: string
        default: "refresh"
      promote:
        description: "Promote candidates after every validation succeeds"
        required: false
        type: boolean
        default: true
      rollback_tag:
        description: "Retained validated-build-* tag to promote when mode is rollback"
        required: false
        type: string
        default: ""
      include_armhf:
        description: "Build and validate the ARMHF cross-builder/runtime pair"
        required: false
        type: boolean
        default: false
      image_set:
        description: "Image cohort: normal, tsan, or normal-and-tsan"
        required: false
        type: string
        default: "normal"
    outputs:
      image_tag:
        description: "Immutable tag shared by the published image cohort"
        value: ${{ jobs.metadata.outputs.build_tag }}
      x86_64_digest:
        description: "Digest of the linux-noble release image"
        value: ${{ jobs.build-normal.outputs.digest }}
      aarch64_digest:
        description: "Digest of the linux-arm64-bookworm release image"
        value: ${{ jobs.build-arm64.outputs.digest }}
      armhf_digest:
        description: "Digest of the linux-armv7-bookworm release image"
        value: ${{ jobs.build-armhf-runtime.outputs.digest }}
      armhf_cross_digest:
        description: "Digest of the linux-armhf-cross-bookworm release image"
        value: ${{ jobs.build-armhf-cross.outputs.digest }}
  workflow_dispatch:
    inputs:
      mode:
        description: "Build a new generation or roll stable back to a retained tag"
        required: true
        type: choice
        options:
          - refresh
          - rollback
        default: refresh
      promote:
        description: "Promote candidates after every validation succeeds"
        required: false
        type: boolean
        default: true
      rollback_tag:
        description: "Retained validated-build-* tag to promote when mode is rollback"
        required: false
        type: string
        default: ""
      include_armhf:
        description: "Build and validate the ARMHF cross-builder/runtime pair"
        required: false
        type: boolean
        default: false
      image_set:
        description: "Images to publish or roll back"
        required: true
        type: choice
        options:
          - normal
          - tsan
          - normal-and-tsan
        default: normal
      publication_target:
        description: "Image destination"
        required: true
        type: choice
        options:
          - internal
          - public-release
        default: internal

permissions:
  contents: read
  packages: write

concurrency:
  group: linux-ci-image-publishing
  cancel-in-progress: false

jobs:
  validate-inputs:
    runs-on: ubuntu-24.04
    timeout-minutes: 10
    steps:
      - name: Validate publication request
        env:
          MODE: ${{ inputs.mode }}
          ROLLBACK_TAG: ${{ inputs.rollback_tag }}
          IMAGE_SET: ${{ inputs.image_set }}
          INCLUDE_ARMHF: ${{ inputs.include_armhf }}
          PROMOTE: ${{ inputs.promote }}
          REQUEST_REF: ${{ github.ref }}
          REQUEST_REPOSITORY: ${{ github.repository }}
          PUBLICATION_TARGET: ${{ inputs.publication_target }}
        shell: bash
        run: |
          case "$MODE" in
            refresh)
              if [ -n "$ROLLBACK_TAG" ]; then
                echo "::error::rollback_tag must be empty in refresh mode"
                exit 2
              fi
              ;;
            rollback)
              if [[ ! "$ROLLBACK_TAG" =~ ^validated-build-[0-9]{8}-[0-9]+-[0-9]+$ ]]; then
                echo "::error::rollback_tag must identify a validated retained generation"
                exit 2
              fi
              ;;
            *)
              echo "::error::Unsupported publication mode: $MODE"
              exit 2
              ;;
          esac

          case "$IMAGE_SET" in
            normal|tsan|normal-and-tsan) ;;
            *)
              echo "::error::Unsupported image_set: $IMAGE_SET"
              exit 2
              ;;
          esac

          case "$PUBLICATION_TARGET" in
            internal) ;;
            public-release)
              if [ "$REQUEST_REPOSITORY" != WSJTX/wsjtx ]; then
                echo "::error::Public release images may only be published by WSJTX/wsjtx"
                exit 2
              fi
              if [[ ! "$REQUEST_REF" =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+(-(beta|rc)[1-9][0-9]*)?$ ]]; then
                echo "::error::Public release images require an exact vX.Y.Z, vX.Y.Z-betaN, or vX.Y.Z-rcN tag"
                exit 2
              fi
              if [ "$MODE" != refresh ] || [ "$PROMOTE" != false ] || \
                 [ "$IMAGE_SET" != normal ] || [ "$INCLUDE_ARMHF" != true ]; then
                echo "::error::Public release images require refresh, promote=false, image_set=normal, and include_armhf=true"
                exit 2
              fi
              ;;
            *)
              echo "::error::Unsupported publication_target: $PUBLICATION_TARGET"
              exit 2
              ;;
          esac

          if [ "$IMAGE_SET" = tsan ] && [ "$INCLUDE_ARMHF" = true ]; then
            echo "::error::include_armhf is valid only when normal images are selected"
            exit 2
          fi

          if { [ "$MODE" = rollback ] || [ "$PROMOTE" = true ]; } && \
            [ "$REQUEST_REF" != refs/heads/develop ]; then
            echo "::error::Promotion and rollback must run from refs/heads/develop"
            exit 2
          fi

  metadata:
    if: inputs.mode != 'rollback'
    needs: validate-inputs
    runs-on: ubuntu-24.04
    timeout-minutes: 10
    outputs:
      build_tag: ${{ steps.metadata.outputs.build_tag }}
      candidate_tag: ${{ steps.metadata.outputs.candidate_tag }}
      created: ${{ steps.metadata.outputs.created }}
      version: ${{ steps.metadata.outputs.version }}
      normal_hamlib_ref: ${{ steps.metadata.outputs.normal_hamlib_ref }}
      tsan_hamlib_ref: ${{ steps.metadata.outputs.tsan_hamlib_ref }}
      normal_noble_recipe: ${{ steps.metadata.outputs.normal_noble_recipe }}
      normal_bookworm_recipe: ${{ steps.metadata.outputs.normal_bookworm_recipe }}
      armhf_cross_recipe: ${{ steps.metadata.outputs.armhf_cross_recipe }}
      armhf_runtime_recipe: ${{ steps.metadata.outputs.armhf_runtime_recipe }}
      tsan_noble_recipe: ${{ steps.metadata.outputs.tsan_noble_recipe }}
      refresh_epoch: ${{ steps.metadata.outputs.refresh_epoch }}
      package_root: ${{ steps.metadata.outputs.package_root }}
      normal_cache_from: ${{ steps.metadata.outputs.normal_cache_from }}
      arm64_cache_from: ${{ steps.metadata.outputs.arm64_cache_from }}
      armhf_cross_cache_from: ${{ steps.metadata.outputs.armhf_cross_cache_from }}
      armhf_cache_from: ${{ steps.metadata.outputs.armhf_cache_from }}
    steps:
      - uses: actions/checkout@v7
      - id: metadata
        env:
          IMAGE_SET: ${{ inputs.image_set }}
          PUBLICATION_TARGET: ${{ inputs.publication_target }}
        shell: bash
        run: |
          created="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
          date_tag="$(date -u +%Y%m%d)"
          version="$(awk -F= '$1 == "version" {print $2; exit}' release-state.txt)"
          if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            echo "::error::Could not derive a numeric version from release-state.txt"
            exit 1
          fi
          {
            if [ "$PUBLICATION_TARGET" = public-release ]; then
              release_tag="release-${GITHUB_REF_NAME}-${GITHUB_SHA}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
              echo "build_tag=$release_tag"
              echo "candidate_tag=candidate-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
              echo "package_root=ghcr.io/wsjtx/wsjtx"
              echo "normal_cache_from=type=gha,scope=public-release-linux-noble"
              echo "arm64_cache_from=type=gha,scope=public-release-linux-arm64-bookworm"
              echo "armhf_cross_cache_from=type=registry,ref=ghcr.io/wsjtx/wsjtx/linux-armhf-cross-bookworm:buildcache"
              echo "armhf_cache_from=type=gha,scope=public-release-linux-armv7-bookworm"
            else
              echo "build_tag=build-${date_tag}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
              echo "candidate_tag=candidate-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
              echo "package_root=ghcr.io/wsjtx/wsjtx-internal"
              echo "normal_cache_from=type=registry,ref=ghcr.io/wsjtx/wsjtx-internal/linux-noble:stable"
              echo "arm64_cache_from=type=registry,ref=ghcr.io/wsjtx/wsjtx-internal/linux-arm64-bookworm:stable"
              echo "armhf_cross_cache_from=type=registry,ref=ghcr.io/wsjtx/wsjtx-internal/linux-armhf-cross-bookworm:buildcache"
              echo "armhf_cache_from=type=registry,ref=ghcr.io/wsjtx/wsjtx-internal/linux-armv7-bookworm:stable"
            fi
            echo "created=$created"
            echo "version=$version"
            echo "refresh_epoch=$(date -u +%Y-%m)"
            if [ "$IMAGE_SET" != tsan ]; then
              . .github/scripts/linux-ci-image-config.sh
              echo "normal_hamlib_ref=$LINUX_HAMLIB_REF"
              echo "normal_noble_recipe=$(.github/scripts/linux-ci-image-fingerprint.sh normal-noble)"
              echo "normal_bookworm_recipe=$(.github/scripts/linux-ci-image-fingerprint.sh normal-bookworm)"
              echo "armhf_cross_recipe=$(.github/scripts/linux-ci-image-fingerprint.sh armhf-cross-bookworm)"
              echo "armhf_runtime_recipe=$(.github/scripts/linux-ci-image-fingerprint.sh armhf-runtime-bookworm)"
            fi
            if [ "$IMAGE_SET" != normal ]; then
              . .github/scripts/tsan-linux-deps-config.sh
              echo "tsan_hamlib_ref=$TSAN_HAMLIB_REF"
              echo "tsan_noble_recipe=$(.github/scripts/linux-ci-image-fingerprint.sh tsan-noble)"
            fi
          } >> "$GITHUB_OUTPUT"

  build-normal:
    if: inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan'
    needs: metadata
    runs-on: ubuntu-24.04
    timeout-minutes: 120
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@v7
        with:
          context: .
          file: .github/images/linux-ci/Dockerfile.noble
          target: normal
          platforms: linux/amd64
          push: true
          provenance: false
          sbom: false
          labels: ${{ inputs.publication_target == 'public-release' && 'org.opencontainers.image.source=https://github.com/WSJTX/wsjtx' || '' }}
          tags: |
            ${{ needs.metadata.outputs.package_root }}/linux-noble:${{ needs.metadata.outputs.candidate_tag }}
            ${{ needs.metadata.outputs.package_root }}/linux-noble:${{ needs.metadata.outputs.build_tag }}
          build-args: |
            IMAGE_CREATED=${{ needs.metadata.outputs.created }}
            IMAGE_GENERATION=${{ needs.metadata.outputs.build_tag }}
            IMAGE_REVISION=${{ github.sha }}
            IMAGE_RECIPE_SHA256=${{ needs.metadata.outputs.normal_noble_recipe }}
            IMAGE_REFRESH_EPOCH=${{ needs.metadata.outputs.refresh_epoch }}
          cache-from: ${{ needs.metadata.outputs.normal_cache_from }}
          cache-to: type=inline

  build-tsan:
    if: inputs.image_set == 'tsan' || inputs.image_set == 'normal-and-tsan'
    needs: metadata
    runs-on: ubuntu-24.04
    timeout-minutes: 240
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@v7
        with:
          context: .
          file: .github/images/linux-ci/Dockerfile.noble
          target: tsan
          platforms: linux/amd64
          push: true
          provenance: false
          sbom: false
          labels: ${{ inputs.publication_target == 'public-release' && 'org.opencontainers.image.source=https://github.com/WSJTX/wsjtx' || '' }}
          tags: |
            ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble:${{ needs.metadata.outputs.candidate_tag }}
            ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble:${{ needs.metadata.outputs.build_tag }}
          build-args: |
            IMAGE_CREATED=${{ needs.metadata.outputs.created }}
            IMAGE_GENERATION=${{ needs.metadata.outputs.build_tag }}
            IMAGE_REVISION=${{ github.sha }}
            IMAGE_RECIPE_SHA256=${{ needs.metadata.outputs.tsan_noble_recipe }}
            IMAGE_REFRESH_EPOCH=${{ needs.metadata.outputs.refresh_epoch }}
          cache-from: type=registry,ref=ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble:stable
          cache-to: type=inline

  build-arm64:
    if: inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan'
    needs: metadata
    runs-on: ubuntu-24.04-arm
    timeout-minutes: 120
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@v7
        with:
          context: .
          file: .github/images/linux-ci/Dockerfile.bookworm
          platforms: linux/arm64
          push: true
          provenance: false
          sbom: false
          labels: ${{ inputs.publication_target == 'public-release' && 'org.opencontainers.image.source=https://github.com/WSJTX/wsjtx' || '' }}
          tags: |
            ${{ needs.metadata.outputs.package_root }}/linux-arm64-bookworm:${{ needs.metadata.outputs.candidate_tag }}
            ${{ needs.metadata.outputs.package_root }}/linux-arm64-bookworm:${{ needs.metadata.outputs.build_tag }}
          build-args: |
            IMAGE_ARCH=aarch64
            IMAGE_CREATED=${{ needs.metadata.outputs.created }}
            IMAGE_GENERATION=${{ needs.metadata.outputs.build_tag }}
            IMAGE_REVISION=${{ github.sha }}
            IMAGE_RECIPE_SHA256=${{ needs.metadata.outputs.normal_bookworm_recipe }}
            IMAGE_REFRESH_EPOCH=${{ needs.metadata.outputs.refresh_epoch }}
          cache-from: ${{ needs.metadata.outputs.arm64_cache_from }}
          cache-to: type=inline

  build-armhf-cross:
    if: >-
      inputs.include_armhf &&
      (inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan')
    needs: metadata
    runs-on: ubuntu-24.04
    timeout-minutes: 180
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - name: Reclaim disk for the ARMHF cross-toolchain
        run: |
          # crosstool-NG needs about 8 GiB of transient build storage. Remove
          # unused hosted-runner SDKs so the Actions worker retains log space.
          df -h /
          sudo rm -rf -- /usr/local/lib/android /usr/share/dotnet /opt/ghc
          df -h /
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@v7
        with:
          context: .
          file: .github/images/linux-ci/Dockerfile.armhf-cross
          platforms: linux/amd64
          push: true
          provenance: false
          sbom: false
          labels: ${{ inputs.publication_target == 'public-release' && 'org.opencontainers.image.source=https://github.com/WSJTX/wsjtx' || '' }}
          tags: |
            ${{ needs.metadata.outputs.package_root }}/linux-armhf-cross-bookworm:${{ needs.metadata.outputs.candidate_tag }}
            ${{ needs.metadata.outputs.package_root }}/linux-armhf-cross-bookworm:${{ needs.metadata.outputs.build_tag }}
          build-args: |
            IMAGE_CREATED=${{ needs.metadata.outputs.created }}
            IMAGE_GENERATION=${{ needs.metadata.outputs.build_tag }}
            IMAGE_REVISION=${{ github.sha }}
            IMAGE_RECIPE_SHA256=${{ needs.metadata.outputs.armhf_cross_recipe }}
            IMAGE_REFRESH_EPOCH=${{ needs.metadata.outputs.refresh_epoch }}
          cache-from: ${{ needs.metadata.outputs.armhf_cross_cache_from }}
          cache-to: type=registry,ref=${{ needs.metadata.outputs.package_root }}/linux-armhf-cross-bookworm:buildcache,mode=max,oci-mediatypes=true,image-manifest=true

  build-armhf-runtime:
    if: >-
      inputs.include_armhf &&
      (inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan')
    needs:
      - metadata
      - build-armhf-cross
    runs-on: ubuntu-24.04-arm
    timeout-minutes: 180
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - uses: docker/setup-qemu-action@v4
        with:
          image: tonistiigi/binfmt:qemu-v10.2.3-68@sha256:400a4873b838d1b89194d982c45e5fb3cda4593fbfd7e08a02e76b03b21166f0
          platforms: linux/arm/v7
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: build
        uses: docker/build-push-action@v7
        with:
          context: .
          file: .github/images/linux-ci/Dockerfile.armhf-runtime
          platforms: linux/arm/v7
          push: true
          provenance: false
          sbom: false
          labels: ${{ inputs.publication_target == 'public-release' && 'org.opencontainers.image.source=https://github.com/WSJTX/wsjtx' || '' }}
          tags: |
            ${{ needs.metadata.outputs.package_root }}/linux-armv7-bookworm:${{ needs.metadata.outputs.candidate_tag }}
            ${{ needs.metadata.outputs.package_root }}/linux-armv7-bookworm:${{ needs.metadata.outputs.build_tag }}
          build-args: |
            CROSS_BUILDER_IMAGE=${{ needs.metadata.outputs.package_root }}/linux-armhf-cross-bookworm:${{ needs.metadata.outputs.build_tag }}
            IMAGE_CREATED=${{ needs.metadata.outputs.created }}
            IMAGE_GENERATION=${{ needs.metadata.outputs.build_tag }}
            IMAGE_REVISION=${{ github.sha }}
            IMAGE_RECIPE_SHA256=${{ needs.metadata.outputs.armhf_runtime_recipe }}
            IMAGE_REFRESH_EPOCH=${{ needs.metadata.outputs.refresh_epoch }}
          cache-from: ${{ needs.metadata.outputs.armhf_cache_from }}
          cache-to: type=inline

  warm-normal-ccache:
    if: >-
      inputs.promote &&
      (inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan')
    needs:
      - metadata
      - build-normal
    uses: ./.github/workflows/build-linux.yml
    permissions:
      contents: read
      packages: read
    with:
      version: ${{ needs.metadata.outputs.version }}
      hamlib_branch: ${{ needs.metadata.outputs.normal_hamlib_ref }}
      arch: x86_64
      image_tag: ${{ needs.metadata.outputs.build_tag }}
      allow_stale_image: false
      recache: false
      save_ccache: true

  warm-arm64-ccache:
    if: >-
      inputs.promote &&
      (inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan')
    needs:
      - metadata
      - build-arm64
    uses: ./.github/workflows/build-linux.yml
    permissions:
      contents: read
      packages: read
    with:
      version: ${{ needs.metadata.outputs.version }}
      hamlib_branch: ${{ needs.metadata.outputs.normal_hamlib_ref }}
      arch: aarch64
      image_tag: ${{ needs.metadata.outputs.build_tag }}
      allow_stale_image: false
      recache: false
      save_ccache: true

  promote:
    if: >-
      always() && inputs.mode == 'refresh' && inputs.promote &&
      (inputs.image_set == 'tsan' ||
       (needs.build-normal.result == 'success' &&
        needs.build-arm64.result == 'success' &&
        needs.warm-normal-ccache.result == 'success' &&
        needs.warm-arm64-ccache.result == 'success' &&
        (inputs.include_armhf == false ||
         (needs.build-armhf-cross.result == 'success' && needs.build-armhf-runtime.result == 'success')))) &&
      (inputs.image_set == 'normal' || needs.build-tsan.result == 'success')
    needs:
      - metadata
      - build-normal
      - build-tsan
      - build-arm64
      - build-armhf-cross
      - build-armhf-runtime
      - warm-normal-ccache
      - warm-arm64-ccache
    runs-on: ubuntu-24.04
    timeout-minutes: 15
    steps:
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Promote validated image digests
        env:
          VALIDATED_TAG: validated-${{ needs.metadata.outputs.build_tag }}
          INCLUDE_ARMHF: ${{ inputs.include_armhf }}
          IMAGE_SET: ${{ inputs.image_set }}
        shell: bash
        run: |
          if [ "$IMAGE_SET" != tsan ]; then
            docker buildx imagetools create --prefer-index=false \
              --tag "ghcr.io/wsjtx/wsjtx-internal/linux-noble:${VALIDATED_TAG}" \
              ghcr.io/wsjtx/wsjtx-internal/linux-noble@${{ needs.build-normal.outputs.digest }}
            docker buildx imagetools create --prefer-index=false \
              --tag "ghcr.io/wsjtx/wsjtx-internal/linux-arm64-bookworm:${VALIDATED_TAG}" \
              ghcr.io/wsjtx/wsjtx-internal/linux-arm64-bookworm@${{ needs.build-arm64.outputs.digest }}
            if [ "$INCLUDE_ARMHF" = true ]; then
              docker buildx imagetools create --prefer-index=false \
                --tag "ghcr.io/wsjtx/wsjtx-internal/linux-armhf-cross-bookworm:${VALIDATED_TAG}" \
                ghcr.io/wsjtx/wsjtx-internal/linux-armhf-cross-bookworm@${{ needs.build-armhf-cross.outputs.digest }}
              docker buildx imagetools create --prefer-index=false \
                --tag "ghcr.io/wsjtx/wsjtx-internal/linux-armv7-bookworm:${VALIDATED_TAG}" \
                ghcr.io/wsjtx/wsjtx-internal/linux-armv7-bookworm@${{ needs.build-armhf-runtime.outputs.digest }}
            fi
          fi
          if [ "$IMAGE_SET" != normal ]; then
            docker buildx imagetools create --prefer-index=false \
              --tag "ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble:${VALIDATED_TAG}" \
              ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble@${{ needs.build-tsan.outputs.digest }}
          fi

          if [ "$IMAGE_SET" != normal ]; then
            docker buildx imagetools create --prefer-index=false \
              --tag ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble:stable \
              ghcr.io/wsjtx/wsjtx-internal/linux-tsan-noble@${{ needs.build-tsan.outputs.digest }}
          fi
          if [ "$IMAGE_SET" != tsan ]; then
            docker buildx imagetools create --prefer-index=false \
              --tag ghcr.io/wsjtx/wsjtx-internal/linux-arm64-bookworm:stable \
              ghcr.io/wsjtx/wsjtx-internal/linux-arm64-bookworm@${{ needs.build-arm64.outputs.digest }}
            if [ "$INCLUDE_ARMHF" = true ]; then
              docker buildx imagetools create --prefer-index=false \
                --tag ghcr.io/wsjtx/wsjtx-internal/linux-armhf-cross-bookworm:stable \
                ghcr.io/wsjtx/wsjtx-internal/linux-armhf-cross-bookworm@${{ needs.build-armhf-cross.outputs.digest }}
              docker buildx imagetools create --prefer-index=false \
                --tag ghcr.io/wsjtx/wsjtx-internal/linux-armv7-bookworm:stable \
                ghcr.io/wsjtx/wsjtx-internal/linux-armv7-bookworm@${{ needs.build-armhf-runtime.outputs.digest }}
            fi
            # linux-noble:stable moves last within the normal image cohort.
            docker buildx imagetools create --prefer-index=false \
              --tag ghcr.io/wsjtx/wsjtx-internal/linux-noble:stable \
              ghcr.io/wsjtx/wsjtx-internal/linux-noble@${{ needs.build-normal.outputs.digest }}
          fi

  warm-asan-ubsan-ccache:
    if: >-
      !cancelled() && needs.promote.result == 'success' &&
      github.ref == 'refs/heads/develop' &&
      inputs.publication_target == 'internal' &&
      inputs.mode == 'refresh' && inputs.promote &&
      (inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan')
    needs:
      - metadata
      - promote
    uses: ./.github/workflows/build-linux-sanitizers.yml
    permissions:
      contents: read
      packages: read
    concurrency:
      group: asan-ubsan-ccache-develop
      cancel-in-progress: false
    with:
      hamlib_branch: ${{ needs.metadata.outputs.normal_hamlib_ref }}
      sanitizer: asan-ubsan
      image_tag: ${{ needs.metadata.outputs.build_tag }}
      run_tests: false
      allow_stale_image: false
      recache: false
      save_ccache: true

  warm-armhf-ccache:
    if: >-
      !cancelled() && needs.promote.result == 'success' &&
      github.ref == 'refs/heads/develop' &&
      inputs.publication_target == 'internal' &&
      inputs.mode == 'refresh' && inputs.promote && inputs.include_armhf &&
      (inputs.image_set == 'normal' || inputs.image_set == 'normal-and-tsan')
    needs:
      - metadata
      - promote
    uses: ./.github/workflows/build-linux.yml
    permissions:
      contents: read
      packages: read
    concurrency:
      group: armhf-ccache-develop
      cancel-in-progress: false
    with:
      version: ${{ needs.metadata.outputs.version }}
      hamlib_branch: ${{ needs.metadata.outputs.normal_hamlib_ref }}
      arch: armhf
      image_tag: ${{ needs.metadata.outputs.build_tag }}
      armhf_build_only: true
      allow_stale_image: false
      recache: false
      save_ccache: true

  rollback:
    if: inputs.mode == 'rollback'
    needs: validate-inputs
    runs-on: ubuntu-24.04
    timeout-minutes: 15
    steps:
      - uses: docker/setup-buildx-action@v4
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Restore retained generation
        env:
          ROLLBACK_TAG: ${{ inputs.rollback_tag }}
          INCLUDE_ARMHF: ${{ inputs.include_armhf }}
          IMAGE_SET: ${{ inputs.image_set }}
        shell: bash
        run: |
          packages=()
          stable_packages=()
          if [ "$IMAGE_SET" != tsan ]; then
            packages+=(linux-noble linux-arm64-bookworm)
            stable_packages+=(linux-arm64-bookworm)
            if [ "$INCLUDE_ARMHF" = true ]; then
              packages+=(linux-armhf-cross-bookworm linux-armv7-bookworm)
              stable_packages+=(linux-armhf-cross-bookworm linux-armv7-bookworm)
            fi
            # linux-noble:stable moves last within the normal image cohort.
            stable_packages+=(linux-noble)
          fi
          if [ "$IMAGE_SET" != normal ]; then
            packages+=(linux-tsan-noble)
            if [ "$IMAGE_SET" = tsan ]; then
              stable_packages=(linux-tsan-noble)
            else
              stable_packages=(linux-tsan-noble "${stable_packages[@]}")
            fi
          fi

          for package in "${packages[@]}"; do
            docker buildx imagetools inspect \
              "ghcr.io/wsjtx/wsjtx-internal/${package}:${ROLLBACK_TAG}" >/dev/null
          done

          for package in "${stable_packages[@]}"; do
            docker buildx imagetools create --prefer-index=false \
              --tag "ghcr.io/wsjtx/wsjtx-internal/${package}:stable" \
              "ghcr.io/wsjtx/wsjtx-internal/${package}:${ROLLBACK_TAG}"
          done