Repository navigation
220 lines (196 loc) · 7.13 KB
/
Copy pathdocker-build.yml
File metadata and controls
220 lines (196 loc) · 7.13 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
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
name: "Docker build & push"
on:
workflow_call:
inputs:
sha:
required: true
type: string
branch_name:
required: true
type: string
outputs:
image_version:
description: "Docker tag"
value: ${{ jobs.calculate_version.outputs.build_version }}
pretty_branch_name:
description: "Pretty Branch Name"
value: ${{ jobs.calculate_version.outputs.pretty_branch_name }}
env:
DOCKER_CLI_EXPERIMENTAL: enabled
IMAGE_NAME: "${{ vars.DOCKERHUB_ORG }}/adala"
DOCKER_IMAGE_TAG_CHECK_NAME: "Docker image tag"
POETRY_VERSION: 2.1.4
jobs:
calculate_version:
name: "Calculate version"
runs-on: ubuntu-latest
outputs:
image_version: ${{ steps.version.outputs.image_version }}
build_version: ${{ steps.version.outputs.build_version }}
pretty_branch_name: ${{ steps.version.outputs.pretty_branch_name }}
sha: ${{ steps.version.outputs.sha }}
steps:
- uses: hmarr/debug-action@v3.0.0
- name: Checkout
uses: actions/checkout@v7
with:
ref: ${{ inputs.sha }}
- name: Calculate version
id: version
env:
BRANCH_NAME: ${{ inputs.branch_name }}
run: |
set -x
sha="$(git rev-parse HEAD)"
echo "sha=$sha" >> $GITHUB_OUTPUT
pretty_branch_name="$(echo -n "${BRANCH_NAME#refs/heads/}" | sed -E 's#[/_\.-]+#-#g' | tr '[:upper:]' '[:lower:]' | cut -c1-25 | sed -E 's#-$##g')"
echo "pretty_branch_name=${pretty_branch_name}" >> "${GITHUB_OUTPUT}"
regexp='^ls-release\/(.*)$';
if [[ "$BRANCH_NAME" =~ $regexp ]]; then
image_version="${BASH_REMATCH[1]}rc${sha}"
else
image_version="${pretty_branch_name}"
fi
echo "image_version=${image_version}" >> $GITHUB_OUTPUT
current_time="$(date +'%Y%m%d.%H%M%S')"
branch="-${pretty_branch_name}"
short_sha="$(git rev-parse --short HEAD)"
long_sha="$(git rev-parse HEAD)"
echo "sha=$long_sha" >> $GITHUB_OUTPUT
short_sha_length="$(echo $short_sha | awk '{print length}')"
current_time_length="$(echo $current_time | awk '{print length}')"
version="${current_time}$(echo $branch | cut -c1-$((50 - short_sha_length - current_time_length)))-${short_sha}"
echo "build_version=$version" >> $GITHUB_OUTPUT
docker_build:
name: "Docker image (${{ matrix.platform }})"
timeout-minutes: 15
runs-on: ${{ matrix.runner }}
needs: calculate_version
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
- platform: linux/arm64
runner: ubuntu-24.04-arm
steps:
- uses: hmarr/debug-action@v3.0.0
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v7
with:
ref: ${{ inputs.sha }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to DockerHub
uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.IMAGE_NAME }}
- name: Push Docker image (${{ matrix.platform }})
uses: docker/build-push-action@v7.4.0
id: docker_build_and_push
with:
context: .
file: Dockerfile.app
platforms: ${{ matrix.platform }}
sbom: true
provenance: true
tags: ${{ env.IMAGE_NAME }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=min
outputs: type=image,push-by-digest=true,name-canonical=true,push=true
build-args: |
POETRY_VERSION=${{ env.POETRY_VERSION }}
- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.docker_build_and_push.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v7
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
merge_docker_manifest:
runs-on: ubuntu-latest
needs:
- docker_build
- calculate_version
steps:
- name: Download digests
uses: actions/download-artifact@v8
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Login to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ needs.calculate_version.outputs.image_version }}
type=raw,value=${{ needs.calculate_version.outputs.build_version }}
- name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
create_check:
name: "Create Docker image tag Check"
runs-on: ubuntu-latest
needs: [calculate_version, docker_build, merge_docker_manifest]
if: success()
steps:
- name: Create Docker image tag Check
uses: actions/github-script@v9
with:
script: |
const { repo, owner } = context.repo;
const details = {
"branch": "${{ inputs.branch_name }}",
"pretty_branch_name": "${{ needs.calculate_version.outputs.pretty_branch_name }}",
"image_version": "${{ needs.calculate_version.outputs.build_version }}",
"sha": "${{ needs.calculate_version.outputs.sha }}"
}
const { data: check } = await github.rest.checks.create({
owner,
repo,
name: '${{ env.DOCKER_IMAGE_TAG_CHECK_NAME }}',
head_sha: '${{ needs.calculate_version.outputs.sha }}',
status: 'in_progress',
output: {
title: '${{ env.DOCKER_IMAGE_TAG_CHECK_NAME }}',
summary: JSON.stringify(details)
}
});
await github.rest.checks.update({
owner,
repo,
check_run_id: check.id,
status: 'completed',
conclusion: 'success'
});