forked from oxsecurity/megalinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-docker.sh
executable file
·663 lines (592 loc) · 23.4 KB
/
upload-docker.sh
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#!/usr/bin/env bash
# shellcheck disable=SC2269
################################################################################
############# Deploy Container to DockerHub @admiralawkbar #####################
################################################################################
# NOTES: This script is used to upload a Dockerfile to DockerHub
# under the GitHub organization
# Its based on being built from a GitHub Action, but could be easily updated
# To be ran in a different medium
#
# PRE-Requirements:
# - Dockerfile
# - System with Docker installed
# - Global variables met
###########
# Globals #
###########
GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace
GITHUB_REPOSITORY="${GITHUB_REPOSITORY}" # GitHub Org/Repo passed from system
DOCKER_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub
DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub
GCR_USERNAME="${GCR_USERNAME}" # Username to login to GitHub package registry
GCR_TOKEN="${GCR_TOKEN}" # Password to login to GitHub package registry
REGISTRY="${REGISTRY}" # What registry to upload | <GCR> or <Docker>
IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image
IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image
ALWAYS_BUILD="${ALWAYS_BUILD}" # Always build image even if another has been found
DOCKERFILE_PATH="${DOCKERFILE_PATH}" # Path to the Dockerfile to be uploaded
MAJOR_TAG='' # Major tag version if we need to update it
UPDATE_MAJOR_TAG=0 # Flag to deploy the major tag version as well
GCR_URL='ghcr.io' # URL to Github Container Registry
DOCKER_IMAGE_REPO='' # Docker tag for the image when created
GCR_IMAGE_REPO='' # Docker tag for the image when created
FOUND_IMAGE=0 # Flag for if the image has already been built
CONTAINER_URL='' # Final URL to upload
SQUASH="${SQUASH}" # if true, calls docker build with --squash
DOCKER_BUILD_PLATFORMS="${DOCKER_BUILD_PLATFORMS}" # docker buildx build --platform value
###########################################################
# Dynamic build variables to pass to container when built #
###########################################################
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') # Current build date EX> "2017-08-28T09:24:41Z"
BUILD_REVISION=$(git rev-parse --short HEAD) # Current git commit EX> "e89faa7"
BUILD_VERSION='' # Current version of the container being built
((LOG_TRACE = LOG_DEBUG = LOG_VERBOSE = LOG_NOTICE = LOG_WARN = LOG_ERROR = "true")) # Enable all loging
export LOG_TRACE LOG_DEBUG LOG_VERBOSE LOG_NOTICE LOG_WARN LOG_ERROR
EXTRA_DOCKER_BUILD_ARGS=""
# Manage --squash parameter
if [[ ${SQUASH} == "true" ]]; then
EXTRA_DOCKER_BUILD_ARGS="--squash"
fi
export EXTRA_DOCKER_BUILD_ARGS
#########################
# Source Function Files #
#########################
# shellcheck source=/dev/null
source "${GITHUB_WORKSPACE}/.automation/log.sh" # Source the function script(s)
################################################################################
############################ FUNCTIONS BELOW ###################################
################################################################################
################################################################################
#### Function Header ###########################################################
Header() {
info "-------------------------------------------------------"
info "---- GitHub Actions Upload image to [${REGISTRY}] ----"
info "-------------------------------------------------------"
}
################################################################################
#### Function ValidateInput ####################################################
ValidateInput() {
# Need to validate we have the basic variables
################
# Print header #
################
info "----------------------------------------------"
info "Gathering variables..."
info "----------------------------------------------"
#############################
# Validate GITHUB_WORKSPACE #
#############################
if [ -z "${GITHUB_WORKSPACE}" ]; then
error "Failed to get [GITHUB_WORKSPACE]!"
fatal "[${GITHUB_WORKSPACE}]"
else
info "Successfully found:${F[W]}[GITHUB_WORKSPACE]${F[B]}, value:${F[W]}[${GITHUB_WORKSPACE}]"
fi
#####################
# Validate REGISTRY #
#####################
if [ -z "${REGISTRY}" ]; then
error "Failed to get [REGISTRY]!"
fatal "[${REGISTRY}]"
else
info "Successfully found:${F[W]}[REGISTRY]${F[B]}, value:${F[W]}[${REGISTRY}]"
fi
#####################################################
# See if we need values for GitHub package Registry #
#####################################################
if [[ ${REGISTRY} == "GCR" ]]; then
#########################
# Validate GCR_USERNAME #
#########################
if [ -z "${GCR_USERNAME}" ]; then
error "Failed to get [GCR_USERNAME]!"
fatal "[${GCR_USERNAME}]"
else
info "Successfully found:${F[W]}[GCR_USERNAME]${F[B]}, value:${F[W]}[${GCR_USERNAME}]"
fi
######################
# Validate GCR_TOKEN #
######################
if [ -z "${GCR_TOKEN}" ]; then
error "Failed to get [GCR_TOKEN]!"
fatal "[${GCR_TOKEN}]"
else
info "Successfully found:${F[W]}[GCR_TOKEN]${F[B]}, value:${F[W]}[********]"
fi
########################################
# See if we need values for Ducker hub #
########################################
elif [[ ${REGISTRY} == "Docker" ]]; then
############################
# Validate DOCKER_USERNAME #
############################
if [ -z "${DOCKER_USERNAME}" ]; then
error "Failed to get [DOCKER_USERNAME]!"
fatal "[${DOCKER_USERNAME}]"
else
info "Successfully found:${F[W]}[DOCKER_USERNAME]${F[B]}, value:${F[W]}[${DOCKER_USERNAME}]"
fi
############################
# Validate DOCKER_PASSWORD #
############################
if [ -z "${DOCKER_PASSWORD}" ]; then
error "Failed to get [DOCKER_PASSWORD]!"
fatal "[${DOCKER_PASSWORD}]"
else
info "Successfully found:${F[W]}[DOCKER_PASSWORD]${F[B]}, value:${F[B]}[********]"
fi
###########################################
# We were not passed a registry to update #
###########################################
else
error "Failed to find a valid registry!"
fatal "Registry:[${REGISTRY}]"
fi
#######################
# Validate IMAGE_REPO #
#######################
if [ -z "${IMAGE_REPO}" ]; then
error "Failed to get [IMAGE_REPO]!"
fatal "[${IMAGE_REPO}]"
else
info "Successfully found:${F[W]}[IMAGE_REPO]${F[B]}, value:${F[W]}[${IMAGE_REPO}]"
# Set the docker Image repo and GCR image repo
DOCKER_IMAGE_REPO="${IMAGE_REPO}"
GCR_IMAGE_REPO="${GCR_URL}/${IMAGE_REPO}"
#########################
# Set the container URL #
#########################
if [[ ${REGISTRY} == "Docker" ]]; then
CONTAINER_URL="${DOCKER_IMAGE_REPO}"
elif [[ ${REGISTRY} == "GCR" ]]; then
CONTAINER_URL="${GCR_IMAGE_REPO}"
fi
fi
##########################
# Validate IMAGE_VERSION #
##########################
if [ -z "${IMAGE_VERSION}" ]; then
warn "Failed to get [IMAGE_VERSION]!"
info "Pulling from Branch Name..."
##############################
# Get the name of the branch #
##############################
BRANCH_NAME=$(git -C "${GITHUB_WORKSPACE}" branch --contains "${GITHUB_SHA}" | awk '{print $2}' 2>&1)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
error "Failed to get branch name!"
fatal "[${BRANCH_NAME}]"
fi
##################################
# Remove non alpha-numeric chars #
##################################
BRANCH_NAME=$(echo "${BRANCH_NAME}" | tr -cd '[:alnum:]')
############################################
# Set the IMAGE_VERSION to the BRANCH_NAME #
############################################
IMAGE_VERSION="${BRANCH_NAME}"
BUILD_VERSION="${IMAGE_VERSION}"
info "Tag:[${IMAGE_VERSION}]"
else
info "Successfully found:${F[W]}[IMAGE_VERSION]${F[B]}, value:${F[W]}[${IMAGE_VERSION}]"
#########################
# Set the build version #
#########################
BUILD_VERSION="${IMAGE_VERSION}"
fi
##################################
# Set regex for getting tag info #
##################################
REGEX='(v[0-9]+\.[0-9]+\.[0-9]+)' # Matches 'v1.2.3'
######################################################################
# Check if this is a latest to a versioned release at create new tag #
######################################################################
if [[ ${IMAGE_VERSION} =~ ${REGEX} ]]; then
# Need to get the major version, and set flag to update
#####################
# Set the major tag #
#####################
MAJOR_TAG=$(echo "${IMAGE_VERSION}" | cut -d '.' -f1)
###################################
# Set flag for updating major tag #
###################################
UPDATE_MAJOR_TAG=1
info "- Also deploying a major tag of:[${MAJOR_TAG}]"
fi
############################
# Validate DOCKERFILE_PATH #
############################
if [ -z "${DOCKERFILE_PATH}" ]; then
error "Failed to get [DOCKERFILE_PATH]!"
fatal "[${DOCKERFILE_PATH}]"
else
info "Successfully found:${F[W]}[DOCKERFILE_PATH]${F[B]}, value:${F[W]}[${DOCKERFILE_PATH}]"
fi
}
################################################################################
#### Function Authenticate #####################################################
Authenticate() {
################
# Pull in Vars #
################
USERNAME="${1}" # Name to auth with
PASSWORD="${2}" # Password to auth with
URL="${3}" # Url to auth towards
NAME="${4}" # name of the service
################
# Print header #
################
info "----------------------------------------------"
info "Login to ${NAME}..."
info "----------------------------------------------"
###################
# Auth to service #
###################
LOGIN_CMD=$(docker login "${URL}" --username "${USERNAME}" --password "${PASSWORD}" 2>&1)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
error "Failed to authenticate to ${NAME}!"
fatal "[${LOGIN_CMD}]"
else
# SUCCESS
info "Successfully authenticated to ${F[C]}${NAME}${F[B]}!"
fi
}
################################################################################
#### Function BuildImage #######################################################
BuildImage() {
################
# Print header #
################
info "----------------------------------------------"
info "Building the Dockerfile image..."
info "----------------------------------------------"
################################
# Validate the DOCKERFILE_PATH #
################################
if [ ! -f "${DOCKERFILE_PATH}" ]; then
# No file found
error "failed to find Dockerfile at:[${DOCKERFILE_PATH}]"
error "Please make sure you give full path!"
fatal "Example:[/configs/Dockerfile] or [Dockerfile] if at root directory"
fi
###################
# Build the image #
###################
docker buildx build --platform "${DOCKER_BUILD_PLATFORMS}" $EXTRA_DOCKER_BUILD_ARGS --no-cache --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${BUILD_VERSION}" -t "${CONTAINER_URL}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" --load . 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [build] Dockerfile!"
else
# SUCCESS
info "Successfully Built image!"
fi
########################################################
# Need to see if we need to tag a major update as well #
########################################################
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
# docker tag "${CONTAINER_URL}:${IMAGE_VERSION}" "${CONTAINER_URL}:latest"
# Tag the image with the major tag & latest tag as well
docker buildx build --platform "${DOCKER_BUILD_PLATFORMS}" $EXTRA_DOCKER_BUILD_ARGS --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${MAJOR_TAG}" -t "${CONTAINER_URL}:latest" -f "${DOCKERFILE_PATH}" --load . 2>&1
docker buildx build --platform "${DOCKER_BUILD_PLATFORMS}" $EXTRA_DOCKER_BUILD_ARGS --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${MAJOR_TAG}" -t "${CONTAINER_URL}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" --load . 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [tag] Dockerfile!"
else
# SUCCESS
info "Successfully tagged image!"
fi
fi
#########################
# Set var to be updated #
#########################
ADDITIONAL_URL=''
####################################
# Set the additional container URL #
####################################
if [[ ${REGISTRY} == "Docker" ]]; then
ADDITIONAL_URL="${GCR_IMAGE_REPO}"
elif [[ ${REGISTRY} == "GCR" ]]; then
ADDITIONAL_URL="${DOCKER_IMAGE_REPO}"
fi
###################
# Build the image #
###################
docker buildx build --platform "${DOCKER_BUILD_PLATFORMS}" $EXTRA_DOCKER_BUILD_ARGS --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${BUILD_VERSION}" -t "${ADDITIONAL_URL}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" --load . 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [tag] Version:[${IMAGE_VERSION}] Additional location Dockerfile!"
else
# SUCCESS
info "Successful [tag] Version:[${IMAGE_VERSION}] of additional image!"
fi
########################################################
# Need to see if we need to tag a major update as well #
########################################################
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
###################
# Build the image with latest tags#
###################
docker buildx build --platform "${DOCKER_BUILD_PLATFORMS}" $EXTRA_DOCKER_BUILD_ARGS --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${MAJOR_TAG}" -t "${ADDITIONAL_URL}:latest" -f "${DOCKERFILE_PATH}" . --load 2>&1
docker buildx build --platform "${DOCKER_BUILD_PLATFORMS}" $EXTRA_DOCKER_BUILD_ARGS --build-arg "BUILD_DATE=${BUILD_DATE}" --build-arg "BUILD_REVISION=${BUILD_REVISION}" --build-arg "BUILD_VERSION=${MAJOR_TAG}" -t "${ADDITIONAL_URL}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" . --load 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [tag] Version:[${MAJOR_TAG}]Additional location Dockerfile!"
else
# SUCCESS
info "Successful [tag] Version:[${MAJOR_TAG}] of additional image!"
fi
fi
}
################################################################################
#### Function UploadImage ######################################################
UploadImage() {
################
# Print header #
################
info "----------------------------------------------"
info "Uploading the DockerFile image to ${REGISTRY}..."
info "----------------------------------------------"
############################################
# Upload the docker image that was created #
############################################
docker push "${CONTAINER_URL}:${IMAGE_VERSION}" 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [upload] Dockerfile!"
else
# SUCCESS
info "Successfully Uploaded Docker image:${F[W]}[${IMAGE_VERSION}]${F[B]} to ${F[C]}${REGISTRY}${F[B]}!"
fi
#########################
# Get Image information #
#########################
IFS=$'\n' # Set the delimit to newline
GET_INFO_CMD=$(docker images | grep "${CONTAINER_URL}" | grep "${IMAGE_VERSION}" 2>&1)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
error "Failed to get information about built Image!"
fatal "[${GET_INFO_CMD}]"
else
################
# Get the data #
################
REPO=$(echo "${GET_INFO_CMD}" | awk '{print $1}')
TAG=$(echo "${GET_INFO_CMD}" | awk '{print $2}')
IMAGE_ID=$(echo "${GET_INFO_CMD}" | awk '{print $3}')
SIZE="${GET_INFO_CMD##* }"
###################
# Print the goods #
###################
info "----------------------------------------------"
info "Docker Image Details:"
info "Repository:[${REPO}]"
info "Tag:[${TAG}]"
info "Image_ID:[${IMAGE_ID}]"
info "Size:[${SIZE}]"
info "----------------------------------------------"
fi
###############################################################
# Check if we need to upload the major tagged version as well #
###############################################################
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
############################################
# Upload the docker image that was created #
############################################
docker push "${CONTAINER_URL}:latest" 2>&1
docker push "${CONTAINER_URL}:${MAJOR_TAG}" 2>&1
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
# ERROR
fatal "failed to [upload] MAJOR_TAG:[${MAJOR_TAG}] Dockerfile!"
else
# SUCCESS
info "Successfully Uploaded TAG:${F[W]}[${MAJOR_TAG}]${F[B]} of Docker image to ${F[C]}${REGISTRY}${F[B]}!"
fi
fi
}
################################################################################
#### Function FindBuiltImage ###################################################
FindBuiltImage() {
# Check the local system to see if an image has already been built
# if so, we only need to update tags and push
# Set FOUND_IMAGE=1 when found
##############
# Local vars #
##############
CHECK_IMAGE_REPO='' # Repo to look for
####################################
# Set the additional container URL #
####################################
if [[ ${REGISTRY} == "GCR" ]]; then
CHECK_IMAGE_REPO="${GCR_IMAGE_REPO}"
elif [[ ${REGISTRY} == "Docker" ]]; then
CHECK_IMAGE_REPO="${DOCKER_IMAGE_REPO}"
fi
#######################################
# Look for Release image in DockerHub #
#######################################
FIND_VERSION_CMD=$(docker images | grep "${CHECK_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
info "Found ${REGISTRY} image:[${CHECK_IMAGE_REPO}:${IMAGE_VERSION}] already built on instance"
# Increment flag
FOUND_RELASE=1
else
info "Failed to find locally created Docker image:[${CHECK_IMAGE_REPO}]"
info "${FIND_VERSION_CMD}"
fi
#####################################
# Look for Major image in DockerHub #
#####################################
FIND_MAJOR_CMD=$(docker images | grep "${CHECK_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
info "Found ${REGISTRY} image:[${CHECK_IMAGE_REPO}:${MAJOR_TAG}] already built on instance"
# Increment flag
FOUND_MAJOR=1
else
info "Failed to find locally created Docker image:[${FIND_MAJOR_CMD}]"
info "${FIND_MAJOR_CMD}"
fi
###############################
# Check if we found the image #
###############################
if [ "${FOUND_MAJOR}" -eq 1 ] && [ "${FOUND_RELASE}" -eq 1 ]; then
FOUND_IMAGE=1
fi
}
################################################################################
#### Function Footer ###########################################################
Footer() {
info "-------------------------------------------------------"
info "The step has completed"
info "-------------------------------------------------------"
}
################################################################################
################################## MAIN ########################################
################################################################################
##########
# Header #
##########
Header
##################
# Validate Input #
##################
ValidateInput
###############################
# Find Image if already built #
###############################
if [ "$ALWAYS_BUILD" != "force" ]; then
FindBuiltImage
else
FOUND_IMAGE=1
echo "skip find build image as ALWAYS_BUILD is force"
fi
###################
# Build the image #
###################
if [ "$FOUND_IMAGE" -ne 0 ]; then
BuildImage
fi
######################
# Login to DockerHub #
######################
if [[ ${REGISTRY} == "Docker" ]]; then
# Authenticate "Username" "Password" "Url" "Name"
Authenticate "${DOCKER_USERNAME}" "${DOCKER_PASSWORD}" "" "Dockerhub"
######################################
# Login to GitHub Container Registry #
######################################
elif [[ ${REGISTRY} == "GCR" ]]; then
# Authenticate "Username" "Password" "Url" "Name"
Authenticate "${GCR_USERNAME}" "${GCR_TOKEN}" "https://${GCR_URL}" "GitHub Container Registry"
else
#########
# ERROR #
#########
error "Registry not set correctly!"
fatal "Registry:[${REGISTRY}]"
fi
####################
# Upload the image #
####################
UploadImage
##########
# Footer #
##########
Footer