Skip to content

Commit d4c65c0

Browse files
committed
better code
1 parent b48ac71 commit d4c65c0

File tree

1 file changed

+83
-103
lines changed

1 file changed

+83
-103
lines changed

.automation/upload-docker.sh

Lines changed: 83 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ GCR_URL='containers.pkg.github.com' # URL to Github Container Registry
3333
DOCKER_IMAGE_REPO='' # Docker tag for the image when created
3434
GCR_IMAGE_REPO='' # Docker tag for the image when created
3535
FOUND_IMAGE=0 # Flag for if the image has already been built
36+
CONTAINER_URL='' # Final URL to upload
3637

3738
#####################
3839
# Get the repo name #
@@ -148,18 +149,16 @@ ValidateInput() {
148149
fatal "[${IMAGE_REPO}]"
149150
else
150151
info "Successfully found:${F[W]}[IMAGE_REPO]${F[B]}, value:${F[W]}[${IMAGE_REPO}]"
151-
# Set the docker Image repo
152+
# Set the docker Image repo and GCR image repo
152153
DOCKER_IMAGE_REPO="${IMAGE_REPO}"
153-
###############################################
154-
# Need to see if GCR registry and update name #
155-
###############################################
156-
if [[ ${REGISTRY} == "GCR" ]]; then
157-
NAME="${GCR_URL}/${IMAGE_REPO}/${REPO_NAME}"
158-
# Set the default image repo
159-
IMAGE_REPO="${NAME}"
160-
# Set the GCR image repo
161-
GCR_IMAGE_REPO="${IMAGE_REPO}"
162-
info "Updated [IMAGE_REPO] to:[${IMAGE_REPO}] for GCR"
154+
GCR_IMAGE_REPO="${GCR_URL}/${IMAGE_REPO}/${REPO_NAME}"
155+
#########################
156+
# Set the container URL #
157+
#########################
158+
if [[ ${REGISTRY} == "Docker" ]]; then
159+
CONTAINER_URL="${DOCKER_IMAGE_REPO}"
160+
elif [[ ${REGISTRY} == "GCR" ]]; then
161+
CONTAINER_URL="${GCR_IMAGE_REPO}"
163162
fi
164163
fi
165164

@@ -282,7 +281,7 @@ BuildImage() {
282281
# Print header #
283282
################
284283
info "----------------------------------------------"
285-
info "Building the DockerFile image..."
284+
info "Building the Dockerfile image..."
286285
info "----------------------------------------------"
287286

288287
################################
@@ -298,7 +297,7 @@ BuildImage() {
298297
###################
299298
# Build the image #
300299
###################
301-
docker build --no-cache -t "${IMAGE_REPO}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" . 2>&1
300+
docker build --no-cache -t "${CONTAINER_URL}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" . 2>&1
302301

303302
#######################
304303
# Load the error code #
@@ -321,7 +320,7 @@ BuildImage() {
321320
########################################################
322321
if [ ${UPDATE_MAJOR_TAG} -eq 1 ]; then
323322
# Tag the image with the major tag as well
324-
docker build -t "${IMAGE_REPO}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" . 2>&1
323+
docker build -t "${CONTAINER_URL}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" . 2>&1
325324

326325
#######################
327326
# Load the error code #
@@ -339,21 +338,25 @@ BuildImage() {
339338
info "Successfully tagged image!"
340339
fi
341340
fi
342-
}
343-
################################################################################
344-
#### Function TagBuiltImage ####################################################
345-
TagBuiltImage() {
346-
####################
347-
# Pull in the vars #
348-
####################
349-
ORIGINAL_TAG="$1"
350-
NEW_TAG="$2"
351-
352-
#################
353-
# Tag the image #
354-
#################
355-
info "Re-Tagging image from:[${ORIGINAL_TAG}] to:[${NEW_TAG}]"
356-
docker image tag "${ORIGINAL_TAG}" "${NEW_TAG}"
341+
342+
#########################
343+
# Set var to be updated #
344+
#########################
345+
ADDITONAL_URL=''
346+
347+
####################################
348+
# Set the additional container URL #
349+
####################################
350+
if [[ ${REGISTRY} == "Docker" ]]; then
351+
ADDITONAL_URL="${GCR_IMAGE_REPO}"
352+
elif [[ ${REGISTRY} == "GCR" ]]; then
353+
ADDITONAL_URL="${DOCKER_IMAGE_REPO}"
354+
fi
355+
356+
###################
357+
# Build the image #
358+
###################
359+
docker build -t "${ADDITONAL_URL}:${IMAGE_VERSION}" -f "${DOCKERFILE_PATH}" . 2>&1
357360

358361
#######################
359362
# Load the error code #
@@ -363,12 +366,34 @@ TagBuiltImage() {
363366
##############################
364367
# Check the shell for errors #
365368
##############################
366-
if [ $ERROR_CODE -ne 0 ]; then
367-
info "Successfully tag of image:[${NEW_TAG}]"
369+
if [ ${ERROR_CODE} -ne 0 ]; then
370+
# ERROR
371+
fatal "failed to [tag] Version:[${IMAGE_VERSION}] Additonal location Dockerfile!"
368372
else
369-
fatal "Failed to tag image:[${NEW_TAG}]"
373+
# SUCCESS
374+
info "Successfull [tag] Version:[${IMAGE_VERSION}] of additonal image!"
370375
fi
371376

377+
###################
378+
# Build the image #
379+
###################
380+
docker build -t "${ADDITONAL_URL}:${MAJOR_TAG}" -f "${DOCKERFILE_PATH}" . 2>&1
381+
382+
#######################
383+
# Load the error code #
384+
#######################
385+
ERROR_CODE=$?
386+
387+
##############################
388+
# Check the shell for errors #
389+
##############################
390+
if [ ${ERROR_CODE} -ne 0 ]; then
391+
# ERROR
392+
fatal "failed to [tag] Version:[${MAJOR_TAG}]Additonal location Dockerfile!"
393+
else
394+
# SUCCESS
395+
info "Successfull [tag] Version:[${MAJOR_TAG}] of additonal image!"
396+
fi
372397
}
373398
################################################################################
374399
#### Function UploadImage ######################################################
@@ -383,7 +408,7 @@ UploadImage() {
383408
############################################
384409
# Upload the docker image that was created #
385410
############################################
386-
docker push "${IMAGE_REPO}:${IMAGE_VERSION}" 2>&1
411+
docker push "${CONTAINER_URL}:${IMAGE_VERSION}" 2>&1
387412

388413
#######################
389414
# Load the error code #
@@ -405,7 +430,7 @@ UploadImage() {
405430
# Get Image information #
406431
#########################
407432
IFS=$'\n' # Set the delimit to newline
408-
GET_INFO_CMD=$(docker images | grep "${IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1)
433+
GET_INFO_CMD=$(docker images | grep "${CONTAINER_URL}" | grep "${IMAGE_VERSION}" 2>&1)
409434

410435
#######################
411436
# Load the error code #
@@ -447,7 +472,7 @@ UploadImage() {
447472
############################################
448473
# Upload the docker image that was created #
449474
############################################
450-
docker push "${IMAGE_REPO}:${MAJOR_TAG}" 2>&1
475+
docker push "${CONTAINER_URL}:${MAJOR_TAG}" 2>&1
451476

452477
#######################
453478
# Load the error code #
@@ -480,11 +505,21 @@ FindBuiltImage() {
480505
FOUND_DOCKER_MAJOR=0 # Flag if docker major image is found
481506
FOUND_GCR_RELASE=0 # Flag if GCR releasae image is found
482507
FOUND_GCR_MAJOR=0 # Flag if GCR major image is found
508+
CHECK_IMAGE_REPO='' # Repo to look for
509+
510+
####################################
511+
# Set the additional container URL #
512+
####################################
513+
if [[ ${REGISTRY} == "GCR" ]]; then
514+
CHECK_IMAGE_REPO="${GCR_IMAGE_REPO}"
515+
elif [[ ${REGISTRY} == "Docker" ]]; then
516+
CHECK_IMAGE_REPO="${DOCKER_IMAGE_REPO}"
517+
fi
483518

484519
#######################################
485520
# Look for Release image in DockerHub #
486521
#######################################
487-
DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1)
522+
FIND_VERSION_CMD=$(docker images | grep "${CHECK_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1)
488523

489524
#######################
490525
# Load the error code #
@@ -495,59 +530,17 @@ FindBuiltImage() {
495530
# Check the shell for errors #
496531
##############################
497532
if [ $ERROR_CODE -ne 0 ]; then
498-
info "Found Docker image:[$DOCKER_IMAGE_REPO:$IMAGE_VERSION] already built on instance"
533+
info "Found ${REGISTRY} image:[${CHECK_IMAGE_REPO}:${IMAGE_VERSION}] already built on instance"
499534
# Increment flag
500-
FOUND_DOCKER_RELASE=1
535+
FOUND_RELASE=1
501536
else
502-
info "Failed to find locally created Docker image:[$DOCKERHUB_FIND_CMD]"
537+
info "Failed to find locally created Docker image:[${CHECK_IMAGE_REPO}]"
503538
fi
504539

505540
#####################################
506541
# Look for Major image in DockerHub #
507542
#####################################
508-
DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1)
509-
510-
#######################
511-
# Load the error code #
512-
#######################
513-
ERROR_CODE=$?
514-
515-
##############################
516-
# Check the shell for errors #
517-
##############################
518-
if [ $ERROR_CODE -ne 0 ]; then
519-
info "Found Docker image:[$DOCKER_IMAGE_REPO:$MAJOR_TAG] already built on instance"
520-
# Increment flag
521-
FOUND_DOCKER_MAJOR=1
522-
else
523-
info "Failed to find locally created Docker image:[$DOCKERHUB_FIND_CMD]"
524-
fi
525-
526-
####################################
527-
# Look for Release image in GitHub #
528-
####################################
529-
GCR_FIND_CMD=$(docker images | grep "${GCR_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1)
530-
531-
#######################
532-
# Load the error code #
533-
#######################
534-
ERROR_CODE=$?
535-
536-
##############################
537-
# Check the shell for errors #
538-
##############################
539-
if [ $ERROR_CODE -ne 0 ]; then
540-
info "Found Docker image:[$GCR_IMAGE_REPO:$IMAGE_VERSION] already built on instance"
541-
# Increment flag
542-
FOUND_GCR_RELASE=1
543-
else
544-
info "Failed to find locally created Docker image:[$GCR_FIND_CMD]"
545-
fi
546-
547-
##################################
548-
# Look for Major image in GitHub #
549-
##################################
550-
GCR_FIND_CMD=$(docker images | grep "${GCR_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1)
543+
FIND_MAJOR_CMD=$(docker images | grep "${CHECK_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1)
551544

552545
#######################
553546
# Load the error code #
@@ -558,31 +551,18 @@ FindBuiltImage() {
558551
# Check the shell for errors #
559552
##############################
560553
if [ $ERROR_CODE -ne 0 ]; then
561-
info "Found Docker image:[$GCR_IMAGE_REPO:$MAJOR_TAG] already built on instance"
554+
info "Found ${REGISTRY} image:[${CHECK_IMAGE_REPO}:${MAJOR_TAG}] already built on instance"
562555
# Increment flag
563-
FOUND_GCR_MAJOR=1
556+
FOUND_MAJOR=1
564557
else
565-
info "Failed to find locally created Docker image:[$GCR_FIND_CMD]"
558+
info "Failed to find locally created Docker image:[${FIND_MAJOR_CMD}]"
566559
fi
567560

568-
####################################
569-
# Need to see if GCR registry push #
570-
####################################
571-
if [[ ${REGISTRY} == "GCR" ]]; then
572-
if [ ${FOUND_DOCKER_MAJOR} -eq 1 ] && [ ${FOUND_DOCKER_RELASE} -eq 1 ]; then
573-
# We have already created the images for dockerhub, need to re-tag and push
574-
# Tag the image: TagBuiltImage "OldTag" "NewTag"
575-
TagBuiltImage "${DOCKER_IMAGE_REPO}:${IMAGE_VERSION}" "$GCR_IMAGE_REPO:${IMAGE_VERSION}"
576-
TagBuiltImage "${DOCKER_IMAGE_REPO}:${MAJOR_TAG}" "${GCR_IMAGE_REPO}:${MAJOR_TAG}"
577-
FOUND_IMAGE=1
578-
fi
579-
elif [[ ${REGISTRY} == "Docker" ]]; then
580-
if [ ${FOUND_GCR_MAJOR} -eq 1 ] && [ ${FOUND_GCR_RELASE} -eq 1 ]; then
581-
# We have already created the images for GCR, need to re-tag and push
582-
TagBuiltImage "$GCR_IMAGE_REPO:${IMAGE_VERSION}" "${DOCKER_IMAGE_REPO}:${IMAGE_VERSION}"
583-
TagBuiltImage "${GCR_IMAGE_REPO}:${MAJOR_TAG}""${DOCKER_IMAGE_REPO}:${MAJOR_TAG}"
584-
FOUND_IMAGE=1
585-
fi
561+
###############################
562+
# Check if we found the image #
563+
###############################
564+
if [ ${FOUND_MAJOR} -eq 1 ] && [ ${FOUND_RELASE} -eq 1 ]; then
565+
FOUND_IMAGE=1
586566
fi
587567
}
588568
################################################################################

0 commit comments

Comments
 (0)