-
Notifications
You must be signed in to change notification settings - Fork 1
202 lines (182 loc) · 6.74 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
name: Build
on:
workflow_call:
inputs:
REGISTRY:
required: true
type: string
NAMESPACE:
required: true
type: string
MULTI_ARCH:
required: true
type: boolean
TAG:
required: true
type: string
MAJOR_TAG:
required: false
type: string
MINOR_TAG:
required: false
type: string
PATCH_TAG:
required: false
type: string
USE_QEMU:
required: true
type: boolean
workflow_dispatch:
inputs:
REGISTRY:
description: Target registry to push images
required: true
type: string
default: ghcr.io
NAMESPACE:
description: Target namespace to the given registry
required: true
type: string
default: this-is-tobi/template-monorepo-ts
MULTI_ARCH:
description: Build for both amd64 and arm64
required: true
type: boolean
default: false
USE_QEMU:
description: Use QEMU emulator for arm64
required: true
type: boolean
default: false
permissions:
packages: write
jobs:
infos:
name: Generate infos
runs-on: ubuntu-latest
outputs:
build-matrix: ${{ steps.build-matrix.outputs.BUILD_MATRIX }}
lower-branch: ${{ steps.infos.outputs.LOWER_BRANCH }}
short-sha: ${{ steps.infos.outputs.SHORT_SHA }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
- name: Generate matrix
id: build-matrix
run: |
echo "BUILD_MATRIX=$(jq -c . < ./ci/matrix/docker.json)" >> $GITHUB_OUTPUT
- name: Get variables
id: infos
run: |
echo "LOWER_BRANCH=$(echo '${{ github.head_ref }}' | sed 's/\//-/g' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
echo "SHORT_SHA=sha-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
build:
name: Build application
runs-on: ${{ matrix.runners }}
needs:
- infos
strategy:
matrix:
runners: ${{ inputs.MULTI_ARCH && !inputs.USE_QEMU && fromJson('["ubuntu-latest", "ARM64"]') || fromJson('["ubuntu-latest"]') }}
images: ${{ fromJSON(needs.infos.outputs.build-matrix) }}
steps:
- name: Checks-out repository
uses: actions/checkout@v4
- name: Set up Docker buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: buildx-${{ runner.os }}-${{ runner.arch }}-${{ matrix.images.name }}-${{ hashFiles(format('apps/{0}/**', matrix.images.name)) }}
restore-keys: |
buildx-${{ runner.os }}-${{ runner.arch }}-${{ matrix.images.name }}-
- name: Set up QEMU (for multi platform build)
uses: docker/setup-qemu-action@v3
if: ${{ inputs.USE_QEMU }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
logout: true
- name: Build and push docker image
id: build
uses: docker/build-push-action@v6
with:
context: ${{ matrix.images.build.context }}
file: ${{ matrix.images.build.dockerfile }}
provenance: false
platforms: ${{ inputs.MULTI_ARCH && inputs.USE_QEMU && 'linux/amd64,linux/arm64' || (contains(runner.arch, 'ARM') && 'linux/arm64' || 'linux/amd64') }}
outputs: type=image,name=${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }},push-by-digest=true,name-canonical=true,push=true
build-args: |
API__VERSION=${{ inputs.TAG || needs.infos.outputs.short-sha }}
# cache-from: type=gha
# cache-to: type=gha,mode=max
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
# Necessary to avoid forever growing cache
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Export digest
run: |
mkdir -p /tmp/digests/${{ matrix.images.name }}
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${{ matrix.images.name }}/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.images.name }}-${{ inputs.MULTI_ARCH && inputs.USE_QEMU && 'linux-amd64_linux-arm64' || (contains(runner.arch, 'ARM') && 'linux-arm64' || 'linux-amd64') }}
path: /tmp/digests/${{ matrix.images.name }}/*
if-no-files-found: error
retention-days: 1
merge:
name: Merge digest
runs-on: ubuntu-latest
needs:
- infos
- build
strategy:
matrix:
images: ${{ fromJSON(needs.infos.outputs.build-matrix) }}
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
pattern: digests-${{ matrix.images.name }}-*
path: /tmp/digests/${{ matrix.images.name }}
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }}
tags: |
type=raw,value=${{ needs.infos.outputs.lower-branch }},enable=${{ github.head_ref != 'main' }}
type=raw,value=${{ needs.infos.outputs.short-sha }},enable=${{ inputs.TAG == '' }}
type=raw,value=${{ inputs.TAG }},enable=${{ inputs.TAG != '' }}
type=raw,value=${{ inputs.MAJOR_TAG }}.${{ inputs.MINOR_TAG }},enable=${{ inputs.MAJOR_TAG != '' && inputs.MINOR_TAG != '' }}
type=raw,value=${{ inputs.MAJOR_TAG }},enable=${{ inputs.MAJOR_TAG != '' }}
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ inputs.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
logout: true
- name: Create manifest list and push
working-directory: /tmp/digests/${{ matrix.images.name }}
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ inputs.REGISTRY }}/${{ inputs.NAMESPACE }}/${{ matrix.images.name }}:${{ steps.meta.outputs.version }}