|
| 1 | +on: |
| 2 | + push: |
| 3 | + branches: |
| 4 | + - master |
| 5 | + tags: |
| 6 | + - v?[0-9]+.[0-9]+.[0-9]+* |
| 7 | + pull_request: |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + discover: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + outputs: |
| 17 | + matrix: ${{ steps.discover.outputs.matrix }} |
| 18 | + manifests: ${{ steps.discover.outputs.manifests }} |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + - uses: DeterminateSystems/nix-installer-action@main |
| 22 | + - id: discover |
| 23 | + run: | |
| 24 | + flake_json=$(nix flake show --all-systems --json 2>/dev/null) |
| 25 | +
|
| 26 | + matrix=$(echo "$flake_json" | jq -c '[.inventory.packages.output.children |
| 27 | + | to_entries[] |
| 28 | + | .key as $sys |
| 29 | + | .value.children |
| 30 | + | keys[] |
| 31 | + | select(endswith("-docker")) |
| 32 | + | {system: $sys, package: .}]') |
| 33 | +
|
| 34 | + manifests=$(echo "$flake_json" | jq -c '[.inventory.packages.output.children |
| 35 | + | to_entries[] |
| 36 | + | .key as $sys |
| 37 | + | .value.children |
| 38 | + | keys[] |
| 39 | + | select(endswith("-docker")) |
| 40 | + | {system: $sys, package: .}] |
| 41 | + | group_by(.package) |
| 42 | + | map({package: .[0].package, systems: [.[].system]})') |
| 43 | +
|
| 44 | + echo "matrix=$matrix" >> "$GITHUB_OUTPUT" |
| 45 | + echo "manifests=$manifests" >> "$GITHUB_OUTPUT" |
| 46 | +
|
| 47 | + build-and-push: |
| 48 | + needs: discover |
| 49 | + runs-on: ${{ matrix.system == 'aarch64-linux' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }} |
| 50 | + permissions: |
| 51 | + contents: read |
| 52 | + packages: write |
| 53 | + strategy: |
| 54 | + fail-fast: false |
| 55 | + matrix: |
| 56 | + include: ${{ fromJson(needs.discover.outputs.matrix) }} |
| 57 | + steps: |
| 58 | + - uses: actions/checkout@v4 |
| 59 | + - uses: DeterminateSystems/nix-installer-action@main |
| 60 | + |
| 61 | + - name: Build Docker image |
| 62 | + run: nix build '.#packages.${{ matrix.system }}.${{ matrix.package }}' -o result |
| 63 | + |
| 64 | + - name: Determine image metadata |
| 65 | + id: meta |
| 66 | + run: | |
| 67 | + name=$(nix eval --raw '.#packages.${{ matrix.system }}.${{ matrix.package }}.imageName') |
| 68 | + tag=$(nix eval --raw '.#packages.${{ matrix.system }}.${{ matrix.package }}.imageTag') |
| 69 | + echo "name=$name" >> "$GITHUB_OUTPUT" |
| 70 | + echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 71 | +
|
| 72 | + - name: Login to GHCR |
| 73 | + uses: docker/login-action@v3 |
| 74 | + with: |
| 75 | + registry: ghcr.io |
| 76 | + username: ${{ github.actor }} |
| 77 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + |
| 79 | + - name: Push per-arch image |
| 80 | + run: | |
| 81 | + repo="ghcr.io/${{ github.repository_owner }}/${{ steps.meta.outputs.name }}" |
| 82 | + repo="${repo,,}" |
| 83 | +
|
| 84 | + docker load < result |
| 85 | + loaded="${{ steps.meta.outputs.name }}:${{ steps.meta.outputs.tag }}" |
| 86 | +
|
| 87 | + docker tag "$loaded" "$repo:${{ matrix.system }}" |
| 88 | + docker push "$repo:${{ matrix.system }}" |
| 89 | +
|
| 90 | + manifest: |
| 91 | + needs: [discover, build-and-push] |
| 92 | + runs-on: ubuntu-latest |
| 93 | + permissions: |
| 94 | + contents: read |
| 95 | + packages: write |
| 96 | + strategy: |
| 97 | + matrix: |
| 98 | + include: ${{ fromJson(needs.discover.outputs.manifests) }} |
| 99 | + steps: |
| 100 | + - uses: actions/checkout@v4 |
| 101 | + - uses: DeterminateSystems/nix-installer-action@main |
| 102 | + |
| 103 | + - name: Determine image name |
| 104 | + id: meta |
| 105 | + run: | |
| 106 | + name=$(nix eval --raw '.#packages.${{ matrix.systems[0] }}.${{ matrix.package }}.imageName') |
| 107 | + repo="ghcr.io/${{ github.repository_owner }}/$name" |
| 108 | + echo "repo=${repo,,}" >> "$GITHUB_OUTPUT" |
| 109 | +
|
| 110 | + - name: Login to GHCR |
| 111 | + uses: docker/login-action@v3 |
| 112 | + with: |
| 113 | + registry: ghcr.io |
| 114 | + username: ${{ github.actor }} |
| 115 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 116 | + |
| 117 | + - name: Create and push multi-arch manifest |
| 118 | + run: | |
| 119 | + repo="${{ steps.meta.outputs.repo }}" |
| 120 | + systems='${{ toJson(matrix.systems) }}' |
| 121 | +
|
| 122 | + args=() |
| 123 | + annotations=() |
| 124 | + for sys in $(echo "$systems" | jq -r '.[]'); do |
| 125 | + args+=("$repo:$sys") |
| 126 | + arch="${sys%%-*}" |
| 127 | + case "$arch" in |
| 128 | + x86_64) docker_arch="amd64" ;; |
| 129 | + aarch64) docker_arch="arm64" ;; |
| 130 | + *) docker_arch="$arch" ;; |
| 131 | + esac |
| 132 | + annotations+=("$repo:$sys --os linux --arch $docker_arch") |
| 133 | + done |
| 134 | +
|
| 135 | + docker manifest create "$repo:latest" "${args[@]}" |
| 136 | + for ann in "${annotations[@]}"; do |
| 137 | + docker manifest annotate "$repo:latest" $ann |
| 138 | + done |
| 139 | + docker manifest push "$repo:latest" |
| 140 | +
|
| 141 | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then |
| 142 | + tag="${GITHUB_REF#refs/tags/}" |
| 143 | + docker manifest create "$repo:$tag" "${args[@]}" |
| 144 | + for ann in "${annotations[@]}"; do |
| 145 | + docker manifest annotate "$repo:$tag" $ann |
| 146 | + done |
| 147 | + docker manifest push "$repo:$tag" |
| 148 | + fi |
0 commit comments