-
Notifications
You must be signed in to change notification settings - Fork 18
184 lines (162 loc) · 6.05 KB
/
Copy pathci-docker.yml
File metadata and controls
184 lines (162 loc) · 6.05 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
on:
push:
branches:
- master
tags:
- v?[0-9]+.[0-9]+.[0-9]+*
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
discover:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
outputs:
matrix: ${{ steps.discover.outputs.matrix }}
manifests: ${{ steps.discover.outputs.manifests }}
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/flakehub-cache-action@main
- id: discover
run: |
flake_json=$(nix flake show --all-systems --json 2>/dev/null)
matrix=$(echo "$flake_json" | jq -c '[.inventory.packages.output.children
| to_entries[]
| .key as $sys
| select($sys | endswith("-linux"))
| .value.children
| keys[]
| select(endswith("-docker"))
| {system: $sys, package: .}]')
manifests=$(echo "$flake_json" | jq -c '[.inventory.packages.output.children
| to_entries[]
| .key as $sys
| select($sys | endswith("-linux"))
| .value.children
| keys[]
| select(endswith("-docker"))
| {system: $sys, package: .}]
| group_by(.package)
| map({package: .[0].package, systems: [.[].system]})')
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
echo "manifests=$manifests" >> "$GITHUB_OUTPUT"
build-and-push:
needs: discover
runs-on: ${{ matrix.system == 'aarch64-linux' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }}
permissions:
id-token: write
contents: read
packages: write
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.discover.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/flakehub-cache-action@main
- name: Build Docker image
run: nix build '.#packages.${{ matrix.system }}.${{ matrix.package }}' -o result
- name: Determine image metadata
id: meta
run: |
name=$(nix eval --raw '.#packages.${{ matrix.system }}.${{ matrix.package }}.imageName')
tag=$(nix eval --raw '.#packages.${{ matrix.system }}.${{ matrix.package }}.imageTag')
echo "name=$name" >> "$GITHUB_OUTPUT"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
- name: Login to GHCR
if: github.event_name == 'push'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push image
if: github.event_name == 'push'
run: |
repo="ghcr.io/${{ github.repository_owner }}/${{ steps.meta.outputs.name }}"
repo="${repo,,}"
sha="${GITHUB_SHA::7}"
docker load < result
loaded="${{ steps.meta.outputs.name }}:${{ steps.meta.outputs.tag }}"
# Always push the system-specific tag (used by manifest job)
docker tag "$loaded" "$repo:${{ matrix.system }}"
docker push "$repo:${{ matrix.system }}"
# Push SHA-tagged per-arch image
docker tag "$loaded" "$repo:${{ matrix.system }}-$sha"
docker push "$repo:${{ matrix.system }}-$sha"
# Push version-tagged per-arch image on tag events
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
vtag="${GITHUB_REF#refs/tags/}"
docker tag "$loaded" "$repo:${{ matrix.system }}-$vtag"
docker push "$repo:${{ matrix.system }}-$vtag"
fi
manifest:
if: github.event_name == 'push'
needs: [discover, build-and-push]
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
packages: write
strategy:
matrix:
include: ${{ fromJson(needs.discover.outputs.manifests) }}
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/flakehub-cache-action@main
- name: Determine image name
id: meta
run: |
name=$(nix eval --raw '.#packages.${{ matrix.systems[0] }}.${{ matrix.package }}.imageName')
repo="ghcr.io/${{ github.repository_owner }}/$name"
echo "repo=${repo,,}" >> "$GITHUB_OUTPUT"
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create and push multi-arch manifests
run: |
repo="${{ steps.meta.outputs.repo }}"
systems='${{ toJson(matrix.systems) }}'
sha="${GITHUB_SHA::7}"
args=()
annotations=()
for sys in $(echo "$systems" | jq -r '.[]'); do
args+=("$repo:$sys")
arch="${sys%%-*}"
case "$arch" in
x86_64) docker_arch="amd64" ;;
aarch64) docker_arch="arm64" ;;
*) docker_arch="$arch" ;;
esac
annotations+=("$repo:$sys --os linux --arch $docker_arch")
done
# latest manifest
docker manifest create "$repo:latest" "${args[@]}"
for ann in "${annotations[@]}"; do
docker manifest annotate "$repo:latest" $ann
done
docker manifest push "$repo:latest"
# SHA manifest
docker manifest create "$repo:$sha" "${args[@]}"
for ann in "${annotations[@]}"; do
docker manifest annotate "$repo:$sha" $ann
done
docker manifest push "$repo:$sha"
# Version tag manifest
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
vtag="${GITHUB_REF#refs/tags/}"
docker manifest create "$repo:$vtag" "${args[@]}"
for ann in "${annotations[@]}"; do
docker manifest annotate "$repo:$vtag" $ann
done
docker manifest push "$repo:$vtag"
fi