Merge pull request #594 from github/updateDeployment

Adding better deployment code
This commit is contained in:
Lukas Gravley 2020-08-20 14:08:58 -05:00 committed by GitHub
commit 6168e86ae6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 221 additions and 35 deletions

View file

@ -18,16 +18,26 @@
# Globals # # Globals #
########### ###########
GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # GitHub Workspace 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_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub
DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub
GPR_USERNAME="${GPR_USERNAME}" # Username to login to GitHub package registry GCR_USERNAME="${GCR_USERNAME}" # Username to login to GitHub package registry
GPR_TOKEN="${GPR_TOKEN}" # Password to login to GitHub package registry GCR_TOKEN="${GCR_TOKEN}" # Password to login to GitHub package registry
REGISTRY="${REGISTRY}" # What registry to upload | <GPR> or <Docker> REGISTRY="${REGISTRY}" # What registry to upload | <GCR> or <Docker>
IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image
IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image
DOCKERFILE_PATH="${DOCKERFILE_PATH}" # Path to the Dockerfile to be uploaded DOCKERFILE_PATH="${DOCKERFILE_PATH}" # Path to the Dockerfile to be uploaded
MAJOR_TAG='' # Major tag version if we need to update it MAJOR_TAG='' # Major tag version if we need to update it
UPDATE_MAJOR_TAG=0 # Flag to deploy the major tag version as well 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 repo name #
#####################
REPO_NAME=$(echo "${GITHUB_REPOSITORY}" |cut -f2 -d'/') # GitHub Repository name
######################### #########################
# Source Function Files # # Source Function Files #
@ -79,25 +89,25 @@ ValidateInput() {
##################################################### #####################################################
# See if we need values for GitHub package Registry # # 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 if [ -z "${GCR_USERNAME}" ]; then
error "Failed to get [GPR_USERNAME]!" error "Failed to get [GCR_USERNAME]!"
fatal "[${GPR_USERNAME}]" fatal "[${GCR_USERNAME}]"
else 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 fi
###################### ######################
# Validate GPR_TOKEN # # Validate GCR_TOKEN #
###################### ######################
if [ -z "${GPR_TOKEN}" ]; then if [ -z "${GCR_TOKEN}" ]; then
error "Failed to get [GPR_TOKEN]!" error "Failed to get [GCR_TOKEN]!"
fatal "[${GPR_TOKEN}]" fatal "[${GCR_TOKEN}]"
else 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 fi
######################################## ########################################
# See if we need values for Ducker hub # # See if we need values for Ducker hub #
@ -138,13 +148,18 @@ ValidateInput() {
fatal "[${IMAGE_REPO}]" fatal "[${IMAGE_REPO}]"
else else
info "Successfully found:${F[W]}[IMAGE_REPO]${F[B]}, value:${F[W]}[${IMAGE_REPO}]" 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 GPR registry and update name # # Need to see if GCR registry and update name #
############################################### ###############################################
if [[ ${REGISTRY} == "GPR" ]]; then if [[ ${REGISTRY} == "GCR" ]]; then
NAME="containers.pkg.github.com/${IMAGE_REPO}/super-linter" NAME="${GCR_URL}/${IMAGE_REPO}/${REPO_NAME}"
# Set the default image repo
IMAGE_REPO="${NAME}" IMAGE_REPO="${NAME}"
info "Updated [IMAGE_REPO] to:[${IMAGE_REPO}] for GPR" # Set the GCR image repo
GCR_IMAGE_REPO="${IMAGE_REPO}"
info "Updated [IMAGE_REPO] to:[${IMAGE_REPO}] for GCR"
fi fi
fi fi
@ -326,6 +341,36 @@ BuildImage() {
fi 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 ###################################################### #### Function UploadImage ######################################################
UploadImage() { UploadImage() {
################ ################
@ -422,6 +467,125 @@ UploadImage() {
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 #
##############
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 #
#######################################
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"
# Increment flag
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"
# Increment flag
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"
# Increment flag
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"
# Increment flag
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 ########################################################### #### Function Footer ###########################################################
Footer() { Footer() {
info "-------------------------------------------------------" info "-------------------------------------------------------"
@ -442,10 +606,17 @@ Header
################## ##################
ValidateInput ValidateInput
###############################
# Find Image if already built #
###############################
FindBuiltImage
################### ###################
# Build the image # # Build the image #
################### ###################
BuildImage if [ "$FOUND_IMAGE" -ne 0 ]; then
BuildImage
fi
###################### ######################
# Login to DockerHub # # Login to DockerHub #
@ -454,12 +625,12 @@ if [[ ${REGISTRY} == "Docker" ]]; then
# Authenticate "Username" "Password" "Url" "Name" # Authenticate "Username" "Password" "Url" "Name"
Authenticate "${DOCKER_USERNAME}" "${DOCKER_PASSWORD}" "" "Dockerhub" Authenticate "${DOCKER_USERNAME}" "${DOCKER_PASSWORD}" "" "Dockerhub"
#################################### ######################################
# Login to GitHub Package Registry # # Login to GitHub Container Registry #
#################################### ######################################
elif [[ ${REGISTRY} == "GPR" ]]; then elif [[ ${REGISTRY} == "GCR" ]]; then
# Authenticate "Username" "Password" "Url" "Name" # 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 Container Registry"
else else
######### #########

View file

@ -53,3 +53,18 @@ jobs:
REGISTRY: Docker REGISTRY: Docker
shell: bash shell: bash
run: .automation/upload-docker.sh 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: GCR
shell: bash
run: .automation/upload-docker.sh

View file

@ -57,7 +57,7 @@ jobs:
############################# #############################
# Run Deploy script for GPR # # Run Deploy script for GPR #
############################# #############################
- name: Deploy Release image to GitHub Package Registry - name: Deploy Release image to GitHub Container Registry
env: env:
# Set the Env Vars # Set the Env Vars
GPR_USERNAME: ${{ secrets.GPR_USERNAME }} GPR_USERNAME: ${{ secrets.GPR_USERNAME }}
@ -65,6 +65,6 @@ jobs:
IMAGE_REPO: github/super-linter IMAGE_REPO: github/super-linter
IMAGE_VERSION: ${{ github.event.release.tag_name }} IMAGE_VERSION: ${{ github.event.release.tag_name }}
DOCKERFILE_PATH: Dockerfile DOCKERFILE_PATH: Dockerfile
REGISTRY: GPR REGISTRY: GCR
shell: bash shell: bash
run: .automation/upload-docker.sh run: .automation/upload-docker.sh

View file

@ -1744,7 +1744,7 @@ if [ "${VALIDATE_R}" == "true" ]; then
########################## ##########################
# Check for local config # # Check for local config #
########################## ##########################
if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ]; then if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ] && [ ${#FILE_ARRAY_R[@]} -ne 0 ]; then
info " " info " "
info "No .lintr configuration file found, using defaults." info "No .lintr configuration file found, using defaults."
cp $R_LINTER_RULES "$GITHUB_WORKSPACE" cp $R_LINTER_RULES "$GITHUB_WORKSPACE"