From 17a4bd2271a8787a65437f62b559275ee8c62c80 Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 20 Aug 2020 12:58:11 -0500 Subject: [PATCH 1/3] adding some dope code --- .automation/upload-docker.sh | 219 +++++++++++++++++++++++---- .github/workflows/deploy-PROD.yml | 15 ++ .github/workflows/deploy-RELEASE.yml | 2 +- lib/linter.sh | 2 +- 4 files changed, 207 insertions(+), 31 deletions(-) diff --git a/.automation/upload-docker.sh b/.automation/upload-docker.sh index 91ad532a..f4708bb5 100755 --- a/.automation/upload-docker.sh +++ b/.automation/upload-docker.sh @@ -17,17 +17,28 @@ ########### # Globals # ########### -GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace -DOCKER_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub -DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub -GPR_USERNAME="${GPR_USERNAME}" # Username to login to GitHub package registry -GPR_TOKEN="${GPR_TOKEN}" # Password to login to GitHub package registry -REGISTRY="${REGISTRY}" # What registry to upload | or -IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image -IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image -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 +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 | or +IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image +IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image +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='containers.pkg.github.com' # 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 + +############################## +# Get the org and repo names # +############################## +ORG_NAME=$(echo "${GITHUB_REPOSITORY}" |cut -f1 -d'/') # Github Organization Name +REPO_NAME=$(echo "${GITHUB_REPOSITORY}" |cut -f2 -d'/') # GitHub Repository name ######################### # Source Function Files # @@ -79,25 +90,25 @@ ValidateInput() { ##################################################### # See if we need values for GitHub package Registry # ##################################################### - if [[ ${REGISTRY} == "GPR" ]]; then + if [[ ${REGISTRY} == "GCR" ]]; then ######################### - # Validate GPR_USERNAME # + # Validate GCR_USERNAME # ######################### - if [ -z "${GPR_USERNAME}" ]; then - error "Failed to get [GPR_USERNAME]!" - fatal "[${GPR_USERNAME}]" + if [ -z "${GCR_USERNAME}" ]; then + error "Failed to get [GCR_USERNAME]!" + fatal "[${GCR_USERNAME}]" else - info "Successfully found:${F[W]}[GPR_USERNAME]${F[B]}, value:${F[W]}[${GPR_USERNAME}]" + info "Successfully found:${F[W]}[GCR_USERNAME]${F[B]}, value:${F[W]}[${GCR_USERNAME}]" fi ###################### - # Validate GPR_TOKEN # + # Validate GCR_TOKEN # ###################### - if [ -z "${GPR_TOKEN}" ]; then - error "Failed to get [GPR_TOKEN]!" - fatal "[${GPR_TOKEN}]" + if [ -z "${GCR_TOKEN}" ]; then + error "Failed to get [GCR_TOKEN]!" + fatal "[${GCR_TOKEN}]" else - info "Successfully found:${F[W]}[GPR_TOKEN]${F[B]}, value:${F[W]}[********]" + info "Successfully found:${F[W]}[GCR_TOKEN]${F[B]}, value:${F[W]}[********]" fi ######################################## # See if we need values for Ducker hub # @@ -138,13 +149,15 @@ ValidateInput() { fatal "[${IMAGE_REPO}]" else info "Successfully found:${F[W]}[IMAGE_REPO]${F[B]}, value:${F[W]}[${IMAGE_REPO}]" + DOCKER_IMAGE_REPO="${IMAGE_REPO}" ############################################### - # Need to see if GPR registry and update name # + # Need to see if GCR registry and update name # ############################################### - if [[ ${REGISTRY} == "GPR" ]]; then - NAME="containers.pkg.github.com/${IMAGE_REPO}/super-linter" + if [[ ${REGISTRY} == "GCR" ]]; then + NAME="${GCR_URL}/${IMAGE_REPO}/${REPO_NAME}" IMAGE_REPO="${NAME}" - info "Updated [IMAGE_REPO] to:[${IMAGE_REPO}] for GPR" + GCR_IMAGE_REPO="${IMAGE_REPO}" + info "Updated [IMAGE_REPO] to:[${IMAGE_REPO}] for GCR" fi fi @@ -326,6 +339,36 @@ BuildImage() { fi } ################################################################################ +#### Function TagBuiltImage #################################################### +TagBuiltImage() { + #################### + # Pull in the vars # + #################### + ORIGINAL_TAG="$1" + NEW_TAG="$2" + + ################# + # Tag the image # + ################# + info "Re-Tagging image from:[${ORIGINAL_TAG}] to:[${NEW_TAG}]" + docker image tag "${ORIGINAL_TAG}" "${NEW_TAG}" + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + info "Successfully tag of image:[${NEW_TAG}]" + else + fatal "Failed to tag image:[${NEW_TAG}]" + fi + +} +################################################################################ #### Function UploadImage ###################################################### UploadImage() { ################ @@ -422,6 +465,117 @@ UploadImage() { 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 + FOUND_DOCKER_RELASE=0 + FOUND_DOCKER_MAJOR=0 + FOUND_GCR_RELASE=0 + FOUND_GCR_MAJOR=0 + + ####################################### + # Look for Release image in DockerHub # + ####################################### + DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_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 Docker image:[$DOCKER_IMAGE_REPO:$IMAGE_VERSION] already built on instance" + FOUND_DOCKER_RELASE=1 + else + info "Failed to find locally created Docker image:[$DOCKERHUB_FIND_CMD]" + fi + + ##################################### + # Look for Major image in DockerHub # + ##################################### + DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_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 Docker image:[$DOCKER_IMAGE_REPO:$MAJOR_TAG] already built on instance" + FOUND_DOCKER_MAJOR=1 + else + info "Failed to find locally created Docker image:[$DOCKERHUB_FIND_CMD]" + fi + + #################################### + # Look for Release image in GitHub # + #################################### + GCR_FIND_CMD=$(docker images | grep "${GCR_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 Docker image:[$GCR_IMAGE_REPO:$IMAGE_VERSION] already built on instance" + FOUND_GCR_RELASE=1 + else + info "Failed to find locally created Docker image:[$GCR_FIND_CMD]" + fi + + ################################## + # Look for Major image in GitHub # + ################################## + GCR_FIND_CMD=$(docker images | grep "${GCR_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 Docker image:[$GCR_IMAGE_REPO:$MAJOR_TAG] already built on instance" + FOUND_GCR_MAJOR=1 + else + info "Failed to find locally created Docker image:[$GCR_FIND_CMD]" + fi + + #################################### + # Need to see if GCR registry push # + #################################### + if [[ ${REGISTRY} == "GCR" ]]; then + if [ ${FOUND_DOCKER_MAJOR} -eq 1 ] && [ ${FOUND_DOCKER_RELASE} -eq 1 ]; then + # We have already created the images for dockerhub, need to re-tag and push + # Tag the image: TagBuiltImage "OldTag" "NewTag" + TagBuiltImage "${DOCKER_IMAGE_REPO}:${IMAGE_VERSION}" "$GCR_IMAGE_REPO:${IMAGE_VERSION}" + TagBuiltImage "${DOCKER_IMAGE_REPO}:${MAJOR_TAG}" "${GCR_IMAGE_REPO}:${MAJOR_TAG}" + FOUND_IMAGE=1 + fi + elif [[ ${REGISTRY} == "Docker" ]]; then + if [ ${FOUND_GCR_MAJOR} -eq 1 ] && [ ${FOUND_GCR_RELASE} -eq 1 ]; then + # We have already created the images for GCR, need to re-tag and push + TagBuiltImage "$GCR_IMAGE_REPO:${IMAGE_VERSION}" "${DOCKER_IMAGE_REPO}:${IMAGE_VERSION}" + TagBuiltImage "${GCR_IMAGE_REPO}:${MAJOR_TAG}""${DOCKER_IMAGE_REPO}:${MAJOR_TAG}" + FOUND_IMAGE=1 + fi + fi +} +################################################################################ #### Function Footer ########################################################### Footer() { info "-------------------------------------------------------" @@ -442,10 +596,17 @@ Header ################## ValidateInput +############################### +# Find Image if already built # +############################### +FindBuiltImage + ################### # Build the image # ################### -BuildImage +if [ "$FOUND_IMAGE" -ne 0 ]; then + BuildImage +fi ###################### # Login to DockerHub # @@ -457,9 +618,9 @@ if [[ ${REGISTRY} == "Docker" ]]; then #################################### # Login to GitHub Package Registry # #################################### -elif [[ ${REGISTRY} == "GPR" ]]; then +elif [[ ${REGISTRY} == "GCR" ]]; then # Authenticate "Username" "Password" "Url" "Name" - Authenticate "${GPR_USERNAME}" "${GPR_TOKEN}" "https://containers.pkg.github.com" "GitHub Package Registry" + Authenticate "${GCR_USERNAME}" "${GCR_TOKEN}" "https://${GCR_URL}" "GitHub Package Registry" else ######### diff --git a/.github/workflows/deploy-PROD.yml b/.github/workflows/deploy-PROD.yml index ad5c77f7..087de664 100644 --- a/.github/workflows/deploy-PROD.yml +++ b/.github/workflows/deploy-PROD.yml @@ -53,3 +53,18 @@ jobs: REGISTRY: Docker shell: bash run: .automation/upload-docker.sh + + #################### + # Run Deploy script # + ##################### + - name: Deploy latest image to GitHub Container Registry + env: + # Set the Env Vars + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + IMAGE_REPO: github/super-linter + IMAGE_VERSION: latest + DOCKERFILE_PATH: Dockerfile + REGISTRY: GPR + shell: bash + run: .automation/upload-docker.sh diff --git a/.github/workflows/deploy-RELEASE.yml b/.github/workflows/deploy-RELEASE.yml index bd1009e0..84e64f57 100644 --- a/.github/workflows/deploy-RELEASE.yml +++ b/.github/workflows/deploy-RELEASE.yml @@ -57,7 +57,7 @@ jobs: ############################# # Run Deploy script for GPR # ############################# - - name: Deploy Release image to GitHub Package Registry + - name: Deploy Release image to GitHub Container Registry env: # Set the Env Vars GPR_USERNAME: ${{ secrets.GPR_USERNAME }} diff --git a/lib/linter.sh b/lib/linter.sh index fab24b88..469694ed 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -1744,7 +1744,7 @@ if [ "${VALIDATE_R}" == "true" ]; then ########################## # Check for local config # ########################## - if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ]; then + if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ] && [ "${FILE_ARRAY_R[@]}" -ne 0 ]; then info " " info "No .lintr configuration file found, using defaults." cp $R_LINTER_RULES "$GITHUB_WORKSPACE" From 815ff9d520f973d12be052339df251fb51fd88cc Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 20 Aug 2020 13:03:40 -0500 Subject: [PATCH 2/3] Adding more --- .automation/upload-docker.sh | 15 +++++++-------- lib/linter.sh | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.automation/upload-docker.sh b/.automation/upload-docker.sh index f4708bb5..d774305a 100755 --- a/.automation/upload-docker.sh +++ b/.automation/upload-docker.sh @@ -34,10 +34,9 @@ DOCKER_IMAGE_REPO='' # Docker tag for the image when create GCR_IMAGE_REPO='' # Docker tag for the image when created FOUND_IMAGE=0 # Flag for if the image has already been built -############################## -# Get the org and repo names # -############################## -ORG_NAME=$(echo "${GITHUB_REPOSITORY}" |cut -f1 -d'/') # Github Organization Name +##################### +# Get the repo name # +##################### REPO_NAME=$(echo "${GITHUB_REPOSITORY}" |cut -f2 -d'/') # GitHub Repository name ######################### @@ -478,7 +477,7 @@ FindBuiltImage() { ####################################### # Look for Release image in DockerHub # ####################################### - DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_IMAGE_REPO}" | grep ${IMAGE_VERSION} 2>&1) + DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1) ####################### # Load the error code # @@ -498,7 +497,7 @@ FindBuiltImage() { ##################################### # Look for Major image in DockerHub # ##################################### - DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_IMAGE_REPO}" | grep ${MAJOR_TAG} 2>&1) + DOCKERHUB_FIND_CMD=$(docker images | grep "${DOCKER_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1) ####################### # Load the error code # @@ -518,7 +517,7 @@ FindBuiltImage() { #################################### # Look for Release image in GitHub # #################################### - GCR_FIND_CMD=$(docker images | grep "${GCR_IMAGE_REPO}" | grep ${IMAGE_VERSION} 2>&1) + GCR_FIND_CMD=$(docker images | grep "${GCR_IMAGE_REPO}" | grep "${IMAGE_VERSION}" 2>&1) ####################### # Load the error code # @@ -538,7 +537,7 @@ FindBuiltImage() { ################################## # Look for Major image in GitHub # ################################## - GCR_FIND_CMD=$(docker images | grep "${GCR_IMAGE_REPO}" | grep ${MAJOR_TAG} 2>&1) + GCR_FIND_CMD=$(docker images | grep "${GCR_IMAGE_REPO}" | grep "${MAJOR_TAG}" 2>&1) ####################### # Load the error code # diff --git a/lib/linter.sh b/lib/linter.sh index 469694ed..b642d8f1 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -1744,7 +1744,7 @@ if [ "${VALIDATE_R}" == "true" ]; then ########################## # Check for local config # ########################## - if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ] && [ "${FILE_ARRAY_R[@]}" -ne 0 ]; then + if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ] && [ ${#FILE_ARRAY_R[@]} -ne 0 ]; then info " " info "No .lintr configuration file found, using defaults." cp $R_LINTER_RULES "$GITHUB_WORKSPACE" From a59f74a287addea93d85b5b02c8ef9860651a9db Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 20 Aug 2020 13:18:17 -0500 Subject: [PATCH 3/3] small cleanup --- .automation/upload-docker.sh | 27 +++++++++++++++++++-------- .github/workflows/deploy-PROD.yml | 2 +- .github/workflows/deploy-RELEASE.yml | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.automation/upload-docker.sh b/.automation/upload-docker.sh index d774305a..b5b0d941 100755 --- a/.automation/upload-docker.sh +++ b/.automation/upload-docker.sh @@ -148,13 +148,16 @@ ValidateInput() { fatal "[${IMAGE_REPO}]" else info "Successfully found:${F[W]}[IMAGE_REPO]${F[B]}, value:${F[W]}[${IMAGE_REPO}]" + # Set the docker Image repo DOCKER_IMAGE_REPO="${IMAGE_REPO}" ############################################### # Need to see if GCR registry and update name # ############################################### if [[ ${REGISTRY} == "GCR" ]]; then NAME="${GCR_URL}/${IMAGE_REPO}/${REPO_NAME}" + # Set the default image repo IMAGE_REPO="${NAME}" + # Set the GCR image repo GCR_IMAGE_REPO="${IMAGE_REPO}" info "Updated [IMAGE_REPO] to:[${IMAGE_REPO}] for GCR" fi @@ -469,10 +472,14 @@ 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 - FOUND_DOCKER_RELASE=0 - FOUND_DOCKER_MAJOR=0 - FOUND_GCR_RELASE=0 - FOUND_GCR_MAJOR=0 + + ############## + # Local vars # + ############## + FOUND_DOCKER_RELASE=0 # Flag if docker relase image is found + FOUND_DOCKER_MAJOR=0 # Flag if docker major image is found + FOUND_GCR_RELASE=0 # Flag if GCR releasae image is found + FOUND_GCR_MAJOR=0 # Flag if GCR major image is found ####################################### # Look for Release image in DockerHub # @@ -489,6 +496,7 @@ FindBuiltImage() { ############################## if [ $ERROR_CODE -ne 0 ]; then info "Found Docker image:[$DOCKER_IMAGE_REPO:$IMAGE_VERSION] already built on instance" + # Increment flag FOUND_DOCKER_RELASE=1 else info "Failed to find locally created Docker image:[$DOCKERHUB_FIND_CMD]" @@ -509,6 +517,7 @@ FindBuiltImage() { ############################## if [ $ERROR_CODE -ne 0 ]; then info "Found Docker image:[$DOCKER_IMAGE_REPO:$MAJOR_TAG] already built on instance" + # Increment flag FOUND_DOCKER_MAJOR=1 else info "Failed to find locally created Docker image:[$DOCKERHUB_FIND_CMD]" @@ -529,6 +538,7 @@ FindBuiltImage() { ############################## if [ $ERROR_CODE -ne 0 ]; then info "Found Docker image:[$GCR_IMAGE_REPO:$IMAGE_VERSION] already built on instance" + # Increment flag FOUND_GCR_RELASE=1 else info "Failed to find locally created Docker image:[$GCR_FIND_CMD]" @@ -549,6 +559,7 @@ FindBuiltImage() { ############################## if [ $ERROR_CODE -ne 0 ]; then info "Found Docker image:[$GCR_IMAGE_REPO:$MAJOR_TAG] already built on instance" + # Increment flag FOUND_GCR_MAJOR=1 else info "Failed to find locally created Docker image:[$GCR_FIND_CMD]" @@ -614,12 +625,12 @@ if [[ ${REGISTRY} == "Docker" ]]; then # Authenticate "Username" "Password" "Url" "Name" Authenticate "${DOCKER_USERNAME}" "${DOCKER_PASSWORD}" "" "Dockerhub" -#################################### -# Login to GitHub Package Registry # -#################################### +###################################### +# Login to GitHub Container Registry # +###################################### elif [[ ${REGISTRY} == "GCR" ]]; then # Authenticate "Username" "Password" "Url" "Name" - Authenticate "${GCR_USERNAME}" "${GCR_TOKEN}" "https://${GCR_URL}" "GitHub Package Registry" + Authenticate "${GCR_USERNAME}" "${GCR_TOKEN}" "https://${GCR_URL}" "GitHub Container Registry" else ######### diff --git a/.github/workflows/deploy-PROD.yml b/.github/workflows/deploy-PROD.yml index 087de664..0a1bb3a6 100644 --- a/.github/workflows/deploy-PROD.yml +++ b/.github/workflows/deploy-PROD.yml @@ -65,6 +65,6 @@ jobs: IMAGE_REPO: github/super-linter IMAGE_VERSION: latest DOCKERFILE_PATH: Dockerfile - REGISTRY: GPR + REGISTRY: GCR shell: bash run: .automation/upload-docker.sh diff --git a/.github/workflows/deploy-RELEASE.yml b/.github/workflows/deploy-RELEASE.yml index 84e64f57..5eaa32ef 100644 --- a/.github/workflows/deploy-RELEASE.yml +++ b/.github/workflows/deploy-RELEASE.yml @@ -65,6 +65,6 @@ jobs: IMAGE_REPO: github/super-linter IMAGE_VERSION: ${{ github.event.release.tag_name }} DOCKERFILE_PATH: Dockerfile - REGISTRY: GPR + REGISTRY: GCR shell: bash run: .automation/upload-docker.sh