-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-build.yml
218 lines (192 loc) · 7.26 KB
/
docker-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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
---
variables:
# You should overide this variable in the subsequent gitlab-ci file
# Where to the branch and tagged images will be pushed
# DOCKER_IMAGE_NAME: lansible/ubuntu
DOCKER_IMAGE_NAME: ${CI_REGISTRY_IMAGE}
# build-args to add to the build
# BUILD_ARGS: --build-arg VERSION=2.9 --build-arg TEST=true
BUILD_ARGS: ""
# Added here to have lower predence then included variables
# Use to override when --help exists with a non zero code
DOCKER_TEST_COMMAND: --help
.setup_buildx:
script:
# Setup buildx not default bundled in docker/docker image
# https://github.com/docker/buildx/#binary-release
- mkdir -p /root/.docker/cli-plugins/
- wget -O ~/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.8.2/buildx-v0.8.2.linux-amd64
- chmod a+x ~/.docker/cli-plugins/docker-buildx
# Get last part of job name as architecture
- export ARCHITECTURE=$(echo ${CI_JOB_NAME} | grep -o '[^_]*$')
# Register qemu cross compile for other then host arch (amd64)
- if [ "${ARCHITECTURE}" != "amd64" ]; then
docker run --rm --privileged tonistiigi/binfmt --install ${ARCHITECTURE};
fi
# Create buildx builder (instead of default docker), needed for the --cache-to option
- docker context create gitlab
- docker buildx create --use gitlab
.build:
stage: build
variables:
DOCKER_IMAGE_NAME: ${CI_REGISTRY_IMAGE}
DOCKER_IMAGE_TAG: ${CI_COMMIT_SHA}
# no need to run for tags, image commit should be built already
except:
- tags
cache:
key: ${CI_JOB_NAME} # one cache per job to avoid architectures overwriting eachothers cache
paths:
- .docker/cache
script:
- !reference [.setup_buildx, script]
# Show command to run
- echo docker buildx build
--platform=linux/${ARCHITECTURE}
--progress=plain
--cache-from=type=local,src=.docker/cache
--cache-to=type=local,dest=.docker/cache,mode=max
${BUILD_ARGS}
--label "org.opencontainers.image.created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
--label "org.opencontainers.image.authors='Wilmar den Ouden <[email protected]>'"
--label "org.opencontainers.image.documentation='https://github.com/LANsible/${CI_PROJECT_NAME}'"
--label "org.opencontainers.image.source='https://github.com/LANsible/${CI_PROJECT_NAME}'"
--label "org.opencontainers.image.version='${CI_COMMIT_REF_SLUG}'"
--label "org.opencontainers.image.vendor=LANsible"
--label "org.opencontainers.image.title='${CI_PROJECT_TITLE}'"
--tag ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}-${ARCHITECTURE}
--push .
# Build and push container
- docker buildx build
--platform=linux/${ARCHITECTURE}
--progress=plain
--cache-from=type=local,src=.docker/cache
--cache-to=type=local,dest=.docker/cache,mode=max
${BUILD_ARGS}
--label "org.opencontainers.image.created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
--label "org.opencontainers.image.authors='Wilmar den Ouden <[email protected]>'"
--label "org.opencontainers.image.documentation='https://github.com/LANsible/${CI_PROJECT_NAME}'"
--label "org.opencontainers.image.source='https://github.com/LANsible/${CI_PROJECT_NAME}'"
--label "org.opencontainers.image.version='${CI_COMMIT_REF_SLUG}'"
--label "org.opencontainers.image.vendor=LANsible"
--label "org.opencontainers.image.title='${CI_PROJECT_TITLE}'"
--tag ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}-${ARCHITECTURE}
--push .
# Implement this in your CI file like
# test_amd64:
# extends: .test
# variables:
# DOCKER_TEST_COMMAND: --version
.test:
stage: test
variables:
DOCKER_IMAGE_NAME: ${CI_REGISTRY_IMAGE}
DOCKER_IMAGE_TAG: ${CI_COMMIT_SHA}
script:
- !reference [.setup_buildx, script]
- docker run --platform=linux/${ARCHITECTURE} ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}-${ARCHITECTURE} ${DOCKER_TEST_COMMAND}
# no need to run for tags, image commit should be built already
except:
- tags
.retag:
stage: push
variables:
# Always use commit sha as source
SOURCE_DOCKER_IMAGE_NAME: ${CI_REGISTRY_IMAGE}
SOURCE_DOCKER_IMAGE_TAG: ${CI_COMMIT_SHA}
script:
# Get last part of job name as architecture
- export ARCHITECTURE=$(echo ${CI_JOB_NAME} | grep -o '[^_]*$')
# Pull previous build image for retagging
- for i in 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; do
docker pull ${SOURCE_DOCKER_IMAGE_NAME}:${SOURCE_DOCKER_IMAGE_TAG}-${ARCHITECTURE} && break || sleep 5m;
done
# Retag source to target image
- echo "Tagging ${SOURCE_DOCKER_IMAGE_NAME}:${SOURCE_DOCKER_IMAGE_TAG}-${ARCHITECTURE} to
${TARGET_DOCKER_IMAGE_NAME}:${TARGET_DOCKER_IMAGE_TAG}-${ARCHITECTURE}"
- docker tag
${SOURCE_DOCKER_IMAGE_NAME}:${SOURCE_DOCKER_IMAGE_TAG}-${ARCHITECTURE}
${TARGET_DOCKER_IMAGE_NAME}:${TARGET_DOCKER_IMAGE_TAG}-${ARCHITECTURE}
- docker push ${TARGET_DOCKER_IMAGE_NAME}:${TARGET_DOCKER_IMAGE_TAG}-${ARCHITECTURE}
.manifest:
stage: manifest
image: lansible/manifest-tool:master
script:
# Push manifest
- manifest-tool push from-args
--ignore-missing
--platforms linux/amd64,linux/arm64,linux/arm
--template "${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}-ARCH"
--target "${DOCKER_IMAGE_NAME}:${DOCKER_MANIFEST_TAG}"
# Build stage
build_amd64:
extends: .build
build_arm64:
extends: .build
# Push branch image
.retag_branch:
extends: .retag
variables:
TARGET_DOCKER_IMAGE_NAME: ${DOCKER_IMAGE_NAME}
TARGET_DOCKER_IMAGE_TAG: ${CI_COMMIT_REF_SLUG}
only:
- branches
retag_branch_amd64:
extends: .retag_branch
retag_branch_arm64:
extends: .retag_branch
# Push tagged release
.retag_tag:
extends: .retag
variables:
TARGET_DOCKER_IMAGE_NAME: ${DOCKER_IMAGE_NAME}
TARGET_DOCKER_IMAGE_TAG: ${CI_COMMIT_TAG}
only:
- tags
push_tag_amd64:
extends: .retag_tag
push_tag_arm64:
extends: .retag_tag
# Setup manifests
push_branch_manifest:
extends: .manifest
variables:
DOCKER_MANIFEST_TAG: ${CI_COMMIT_REF_SLUG}
DOCKER_IMAGE_TAG: ${CI_COMMIT_REF_SLUG}
only:
- branches
push_tag_manifest:
extends: .manifest
variables:
DOCKER_MANIFEST_TAG: ${CI_COMMIT_TAG}
DOCKER_IMAGE_TAG: ${CI_COMMIT_TAG}
only:
- tags
push_latest_manifest:
extends: .manifest
variables:
DOCKER_MANIFEST_TAG: latest
DOCKER_IMAGE_TAG: ${CI_COMMIT_TAG}
only:
- tags
# Push readme to Docker Hub
# https://www.sanisimov.com/2019/03/building-docker-images-for-multiple-architectures/
push_readme:
stage: manifest
script:
- apk add --no-cache jq curl
- 'token=$(
curl -s -X POST
-H "Content-Type: application/json"
-d "{\"username\": \"$DOCKER_USERNAME\", \"password\": \"$DOCKER_PASSWORD\"}"
https://hub.docker.com/v2/users/login/
| jq -r .token
)'
- 'jq -n --arg msg "$(<README.md)" "{\"registry\":\"registry-1.docker.io\",\"full_description\": \"$msg\"}"
| curl -s -o /dev/null -L -w "%{http_code}"
https://cloud.docker.com/v2/repositories/"${DOCKER_IMAGE_NAME}"/
-d @- -X PATCH
-H "Content-Type: application/json"
-H "Authorization: JWT ${token}"'
only:
- tags