@@ -30,23 +30,26 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
30
30
echo " When \$ {BRANCH}=master: latest"
31
31
echo " When \$ {BRANCH}=develop: snapshot"
32
32
echo " Else: same as \$ {BRANCH}"
33
- echo " DOCKER_ORG The Docker registry (i.e. hub.docker.com/r/\$ {DOCKER_ORG}/\$ {DOCKER_REPO})"
34
- echo " Also used for tagging the image."
33
+ echo " DOCKER_REGISTRY The Docker repository's registry (i.e. '\$ {DOCKER_REGISTRY}/\$ {DOCKER_ORG}/\$ {DOCKER_REPO}'')"
34
+ echo " Used for tagging the image."
35
+ echo " Default: docker.io"
36
+ echo " DOCKER_ORG The Docker repository's organisation (i.e. '\$ {DOCKER_REGISTRY}/\$ {DOCKER_ORG}/\$ {DOCKER_REPO}'')"
37
+ echo " Used for tagging the image."
35
38
echo " Default: netboxcommunity"
36
- echo " DOCKER_REPO The Docker registry (i.e. hub.docker.com/r/ \$ {DOCKER_ORG}/\$ {DOCKER_REPO})"
37
- echo " Also used for tagging the image."
39
+ echo " DOCKER_REPO The Docker repository's name (i.e. ' \$ {DOCKER_REGISTRY}/ \$ {DOCKER_ORG}/\$ {DOCKER_REPO}'' )"
40
+ echo " Used for tagging the image."
38
41
echo " Default: netbox"
39
- echo " DOCKER_FROM The base image to use."
40
- echo " Default: Whatever is defined as default in the Dockerfile."
41
42
echo " DOCKER_TAG The name of the tag which is applied to the image."
42
43
echo " Useful for pushing into another registry than hub.docker.com."
43
- echo " Default: \$ {DOCKER_ORG}/\$ {DOCKER_REPO}:\$ {TAG}"
44
+ echo " Default: \$ {DOCKER_REGISTRY}/ \$ { DOCKER_ORG}/\$ {DOCKER_REPO}:\$ {TAG}"
44
45
echo " DOCKER_SHORT_TAG The name of the short tag which is applied to the"
45
46
echo " image. This is used to tag all patch releases to their"
46
47
echo " containing version e.g. v2.5.1 -> v2.5"
47
- echo " Default: \$ {DOCKER_ORG}/\$ {DOCKER_REPO}:<MAJOR>.<MINOR>"
48
+ echo " Default: \$ {DOCKER_REGISTRY}/ \$ { DOCKER_ORG}/\$ {DOCKER_REPO}:<MAJOR>.<MINOR>"
48
49
echo " DOCKERFILE The name of Dockerfile to use."
49
50
echo " Default: Dockerfile"
51
+ echo " DOCKER_FROM The base image to use."
52
+ echo " Default: Whatever is defined as default in the Dockerfile."
50
53
echo " DOCKER_TARGET A specific target to build."
51
54
echo " It's currently not possible to pass multiple targets."
52
55
echo " Default: main ldap"
98
101
DRY=" echo"
99
102
fi
100
103
101
- # ##
102
- # read the project version from the `VERSION` file and trim it
103
- # see https://stackoverflow.com/a/3232433/172132
104
- # ##
105
- NETBOX_DOCKER_PROJECT_VERSION=" ${NETBOX_DOCKER_PROJECT_VERSION-$(sed -e ' s/^[[:space:]]*//' -e ' s/[[:space:]]*$//' VERSION)} "
106
-
107
104
# ##
108
105
# variables for fetching the source
109
106
# ##
@@ -152,9 +149,29 @@ if [ ! -f "${DOCKERFILE}" ]; then
152
149
fi
153
150
fi
154
151
152
+ # ##
153
+ # variables for labelling the docker image
154
+ # ##
155
+ BUILD_DATE=" $( date --utc --iso-8601=minutes) "
156
+
157
+ if [ -d " .git" ]; then
158
+ GIT_REF=" $( git rev-parse HEAD) "
159
+ fi
160
+
161
+ # read the project version from the `VERSION` file and trim it, see https://stackoverflow.com/a/3232433/172132
162
+ PROJECT_VERSION=" ${PROJECT_VERSION-$(sed -e ' s/^[[:space:]]*//' -e ' s/[[:space:]]*$//' VERSION)} "
163
+
164
+ # Get the Git information from the netbox directory
165
+ if [ -d " ${NETBOX_PATH} /.git" ]; then
166
+ NETBOX_GIT_REF=$( cd ${NETBOX_PATH} ; git rev-parse HEAD)
167
+ NETBOX_GIT_BRANCH=$( cd ${NETBOX_PATH} ; git rev-parse --abbrev-ref HEAD)
168
+ NETBOX_GIT_URL=$( cd ${NETBOX_PATH} ; git remote get-url origin)
169
+ fi
170
+
155
171
# ##
156
172
# variables for tagging the docker image
157
173
# ##
174
+ DOCKER_REGISTRY=" ${DOCKER_REGISTRY-docker.io} "
158
175
DOCKER_ORG=" ${DOCKER_ORG-netboxcommunity} "
159
176
DOCKER_REPO=" ${DOCKER_REPO-netbox} "
160
177
case " ${BRANCH} " in
@@ -182,7 +199,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
182
199
# ##
183
200
# composing the final TARGET_DOCKER_TAG
184
201
# ##
185
- TARGET_DOCKER_TAG=" ${DOCKER_TAG-${DOCKER_ORG} / ${DOCKER_REPO} : ${TAG} } "
202
+ TARGET_DOCKER_TAG=" ${DOCKER_TAG-${DOCKER_REGISTRY} / ${ DOCKER_ORG}/ ${DOCKER_REPO} : ${TAG} } "
186
203
if [ " ${DOCKER_TARGET} " != " main" ]; then
187
204
TARGET_DOCKER_TAG=" ${TARGET_DOCKER_TAG} -${DOCKER_TARGET} "
188
205
fi
@@ -196,10 +213,10 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
196
213
MAJOR=${BASH_REMATCH[1]}
197
214
MINOR=${BASH_REMATCH[2]}
198
215
199
- DOCKER_SHORT_TAG =" ${DOCKER_SHORT_TAG-${DOCKER_ORG} / ${DOCKER_REPO} : v${MAJOR} .${MINOR} } "
216
+ TARGET_DOCKER_SHORT_TAG =" ${DOCKER_SHORT_TAG-${DOCKER_REGISTRY} / ${DOCKER_ORG} / ${DOCKER_REPO} : v${MAJOR} .${MINOR} } "
200
217
201
218
if [ " ${DOCKER_TARGET} " != " main" ]; then
202
- DOCKER_SHORT_TAG =" ${DOCKER_SHORT_TAG } -${DOCKER_TARGET} "
219
+ TARGET_DOCKER_SHORT_TAG =" ${TARGET_DOCKER_SHORT_TAG } -${DOCKER_TARGET} "
203
220
fi
204
221
fi
205
222
@@ -216,25 +233,39 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
216
233
-f " ${DOCKERFILE} "
217
234
-t " ${TARGET_DOCKER_TAG} "
218
235
)
219
- if [ -n " ${DOCKER_SHORT_TAG } " ]; then
220
- DOCKER_BUILD_ARGS+=( -t " ${DOCKER_SHORT_TAG } " )
236
+ if [ -n " ${TARGET_DOCKER_SHORT_TAG } " ]; then
237
+ DOCKER_BUILD_ARGS+=( -t " ${TARGET_DOCKER_SHORT_TAG } " )
221
238
fi
222
239
223
240
# --label
224
- DOCKER_BUILD_ARGS+=(
225
- --label " NETBOX_DOCKER_PROJECT_VERSION=${NETBOX_DOCKER_PROJECT_VERSION} "
226
- --label " NETBOX_BRANCH=${BRANCH} "
227
- --label " ORIGINAL_DOCKER_TAG=${TARGET_DOCKER_TAG} "
228
- )
229
- if [ -d " ${NETBOX_PATH} /.git" ]; then
241
+ if [ " ${DOCKER_TARGET} " == " main" ]; then
230
242
DOCKER_BUILD_ARGS+=(
231
- --label " NETBOX_GIT_COMMIT=$( $DRY cd " ${NETBOX_PATH} " ; $DRY git rev-parse HEAD) "
232
- --label " NETBOX_GIT_URL=$( $DRY cd " ${NETBOX_PATH} " ; $DRY git remote get-url origin) "
243
+ --label " ORIGINAL_TAG=${TARGET_DOCKER_TAG} "
244
+
245
+ --label " org.label-schema.build-date=${BUILD_DATE} "
246
+ --label " org.opencontainers.image.created=${BUILD_DATE} "
247
+
248
+ --label " org.label-schema.version=${PROJECT_VERSION} "
249
+ --label " org.opencontainers.image.version=${PROJECT_VERSION} "
233
250
)
251
+ if [ -d " .git" ]; then
252
+ DOCKER_BUILD_ARGS+=(
253
+ --label " org.label-schema.vcs-ref=${GIT_REF} "
254
+ --label " org.opencontainers.image.revision=${GIT_REF} "
255
+ )
256
+ fi
257
+ if [ -d " ${NETBOX_PATH} /.git" ]; then
258
+ DOCKER_BUILD_ARGS+=(
259
+ --label " NETBOX_GIT_BRANCH=${NETBOX_GIT_BRANCH} "
260
+ --label " NETBOX_GIT_REF=${NETBOX_GIT_REF} "
261
+ --label " NETBOX_GIT_URL=${NETBOX_GIT_URL} "
262
+ )
263
+ fi
234
264
fi
235
265
236
266
# --build-arg
237
- DOCKER_BUILD_ARGS+=( --build-arg " NETBOX_PATH=${NETBOX_PATH} " )
267
+ DOCKER_BUILD_ARGS+=( --build-arg " NETBOX_PATH=${NETBOX_PATH} " )
268
+
238
269
if [ -n " ${DOCKER_FROM} " ]; then
239
270
DOCKER_BUILD_ARGS+=( --build-arg " FROM=${DOCKER_FROM} " )
240
271
fi
@@ -262,10 +293,10 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
262
293
$DRY docker push " ${TARGET_DOCKER_TAG} "
263
294
echo " ✅ Finished pushing the Docker image '${TARGET_DOCKER_TAG} '."
264
295
265
- if [ -n " $DOCKER_SHORT_TAG " ]; then
266
- echo " ⏫ Pushing '${DOCKER_SHORT_TAG } '"
267
- $DRY docker push " ${DOCKER_SHORT_TAG } "
268
- echo " ✅ Finished pushing the Docker image '${DOCKER_SHORT_TAG } '."
296
+ if [ -n " ${TARGET_DOCKER_SHORT_TAG} " ]; then
297
+ echo " ⏫ Pushing '${TARGET_DOCKER_SHORT_TAG } '"
298
+ $DRY docker push " ${TARGET_DOCKER_SHORT_TAG } "
299
+ echo " ✅ Finished pushing the Docker image '${TARGET_DOCKER_SHORT_TAG } '."
269
300
fi
270
301
fi
271
302
done
0 commit comments