From bc1d6bd770d3dba87ad1989b06c6469d0e41ecff Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 9 Jan 2020 13:04:41 -0600 Subject: [PATCH 1/4] adding dev stuff --- .automation/cleanup-docker.sh | 288 ++++++++++++++++++ .automation/upload-docker.sh | 45 ++- .github/workflows/cleanup-DEV.yml | 52 ++++ .github/workflows/deploy-DEV.yml | 55 ++++ .../workflows/{deploy.yml => deploy-PROD.yml} | 2 +- 5 files changed, 434 insertions(+), 8 deletions(-) create mode 100644 .automation/cleanup-docker.sh create mode 100644 .github/workflows/cleanup-DEV.yml create mode 100644 .github/workflows/deploy-DEV.yml rename .github/workflows/{deploy.yml => deploy-PROD.yml} (97%) diff --git a/.automation/cleanup-docker.sh b/.automation/cleanup-docker.sh new file mode 100644 index 00000000..15a082b3 --- /dev/null +++ b/.automation/cleanup-docker.sh @@ -0,0 +1,288 @@ +#!/bin/bash + +################################################################################ +############# Cleanup Container on DockerHub @admiralawkbar #################### +################################################################################ + +# NOTES: This script is used to remove a tagged image on DockerHub +# Its based on being built from a GitHub Action, but could be easily updated +# To be ran in a different medium. +# +# PRE-Reqs: +# - Dockerfile +# - System with Docker installed +# - Global variables met + +########### +# 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 +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 + +################################################################################ +############################ FUNCTIONS BELOW ################################### +################################################################################ +################################################################################ +#### Function Header ########################################################### +Header() +{ + echo "" + echo "-------------------------------------------------------" + echo "----- Github Actions remove image from DockerHub ------" + echo "-------------------------------------------------------" + echo "" +} +################################################################################ +#### Function ValidateInput #################################################### +ValidateInput() +{ + # Need to validate we have the basic variables + ################ + # Print header # + ################ + echo "" + echo "----------------------------------------------" + echo "Gathering variables..." + echo "----------------------------------------------" + echo "" + + ############################ + # Validate GITHUB_WORKSPACE # + ############################ + if [ -z "$GITHUB_WORKSPACE" ]; then + echo "ERROR! Failed to get [GITHUB_WORKSPACE]!" + echo "ERROR:[$GITHUB_WORKSPACE]" + exit 1 + else + echo "Successfully found:[GITHUB_WORKSPACE], value:[$GITHUB_WORKSPACE]" + fi + + ############################ + # Validate DOCKER_USERNAME # + ############################ + if [ -z "$DOCKER_USERNAME" ]; then + echo "ERROR! Failed to get [DOCKER_USERNAME]!" + echo "ERROR:[$DOCKER_USERNAME]" + exit 1 + else + echo "Successfully found:[DOCKER_USERNAME], value:[$DOCKER_USERNAME]" + fi + + ############################ + # Validate DOCKER_PASSWORD # + ############################ + if [ -z "$DOCKER_PASSWORD" ]; then + echo "ERROR! Failed to get [DOCKER_PASSWORD]!" + echo "ERROR:[$DOCKER_PASSWORD]" + exit 1 + else + echo "Successfully found:[DOCKER_PASSWORD], value:[********]" + fi + + ####################### + # Validate IMAGE_REPO # + ####################### + if [ -z "$IMAGE_REPO" ]; then + echo "ERROR! Failed to get [IMAGE_REPO]!" + echo "ERROR:[$IMAGE_REPO]" + exit 1 + else + echo "Successfully found:[IMAGE_REPO], value:[$IMAGE_REPO]" + fi + + ########################## + # Validate IMAGE_VERSION # + ########################## + if [ -z "$IMAGE_VERSION" ]; then + echo "ERROR! Failed to get [IMAGE_VERSION]!" + echo "ERROR:[$IMAGE_VERSION]" + exit 1 + else + echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]" + fi + + ################################################## + # Check if we need to get the name of the branch # + ################################################## + if [[ "$IMAGE_VERSION" != "latest" ]]; then + ############################## + # Get the name of the branch # + ############################## + BRANCH_NAME=$(git 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 + echo "ERROR! Failed to get branch name!" + echo "ERROR:[$BRANCH_NAME]" + exit 1 + fi + + ################################### + # Remove non alpha-numberic chars # + ################################### + BRANCH_NAME=$(echo "$BRANCH_NAME" | tr -cd '[:alnum:]') + + ############################################ + # Set the IMAGE_VERSION to the BRANCH_NAME # + ############################################ + IMAGE_VERSION="$BRANCH_NAME" + else + ############################################# + # Image is 'latest' and we will not destroy # + ############################################# + echo "Image Tag is set to:[latest]..." + echo "We will never destroy latest..." + echo "Bye!" + exit 0 + fi +} +################################################################################ +#### Function LoginToDocker #################################################### +LoginToDocker() +{ + ################ + # Print header # + ################ + echo "" + echo "----------------------------------------------" + echo "Login to DockerHub..." + echo "----------------------------------------------" + echo "" + + ###################### + # Login to DockerHub # + ###################### + LOGIN_CMD=$(docker login --username "$DOCKER_USERNAME" --password "$DOCKER_PASSWORD" 2>&1) + + ####################### + # Load the error code # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + # ERROR + echo "ERROR! Failed to authenticate to DockerHub!" + echo "ERROR:[$LOGIN_CMD]" + exit 1 + else + # SUCCESS + echo "Successfully authenticated to DockerHub!" + fi +} +################################################################################ +#### Function RemoveImage ###################################################### +RemoveImage() +{ + ################ + # Print header # + ################ + echo "" + echo "----------------------------------------------" + echo "Removing the DockerFile image:[$IMAGE_REPO:$IMAGE_VERSION]" + echo "----------------------------------------------" + echo "" + + ##################################### + # Create Token to auth to DockerHub # + ##################################### + TOKEN=$(curl -s -k \ + -H "Content-Type: application/json" \ + -X POST \ + -d '{"username": "'$DOCKER_USERNAME'", "password": "'$DOCKER_PASSWORD'"}' \ + "https://hub.docker.com/v2/users/login/" | jq -r .token 2>&1) + + ####################### + # Load the ERROR_CODE # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + # ERROR + echo "ERROR! Failed to gain token from DockerHub!" + echo "ERROR:[$TOKEN]" + exit 1 + else + # SUCCESS + echo "Successfully gained auth token from DockerHub!" + fi + + ################################# + # Remove the tag from DockerHub # + ################################# + REMOVE_CMD=$(curl "https://hub.docker.com/v2/repositories/$IMAGE_REPO/tags/$IMAGE_VERSION/" \ + -X DELETE \ + -H "Authorization: JWT $TOKEN" 2>&1) + + ####################### + # Load the ERROR_CODE # + ####################### + ERROR_CODE=$? + + ############################## + # Check the shell for errors # + ############################## + if [ $ERROR_CODE -ne 0 ]; then + # ERROR + echo "ERROR! Failed to remove tag from DockerHub!" + echo "ERROR:[$REMOVE_CMD]" + exit 1 + else + # SUCCESS + echo "Successfully [removed] Docker image tag from DockerHub!" + fi +} +################################################################################ +#### Function Footer ########################################################### +Footer() +{ + echo "" + echo "-------------------------------------------------------" + echo "The step has completed" + echo "-------------------------------------------------------" + echo "" +} +################################################################################ +################################## MAIN ######################################## +################################################################################ + +########## +# Header # +########## +Header + +################## +# Validate Input # +################## +ValidateInput + +###################### +# Login to DockerHub # +###################### +LoginToDocker + +#################### +# Remove the image # +#################### +RemoveImage + +########## +# Footer # +########## +Footer diff --git a/.automation/upload-docker.sh b/.automation/upload-docker.sh index b9322fa5..6bbdf171 100755 --- a/.automation/upload-docker.sh +++ b/.automation/upload-docker.sh @@ -105,6 +105,41 @@ ValidateInput() echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]" fi + ################################################## + # Check if we need to get the name of the branch # + ################################################## + if [[ "$IMAGE_VERSION" != "latest" ]]; then + ############################## + # Get the name of the branch # + ############################## + BRANCH_NAME=$(git 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 + echo "ERROR! Failed to get branch name!" + echo "ERROR:[$BRANCH_NAME]" + exit 1 + fi + + ################################### + # Remove non alpha-numberic chars # + ################################### + BRANCH_NAME=$(echo "$BRANCH_NAME" | tr -cd '[:alnum:]') + + ############################################ + # Set the IMAGE_VERSION to the BRANCH_NAME # + ############################################ + IMAGE_VERSION="$BRANCH_NAME" + fi + + ############################ # Validate DOCKERFILE_PATH # ############################ @@ -180,7 +215,7 @@ BuildImage() ################### # Build the image # ################### - BUILD_CMD=$(docker build --no-cache -t "$IMAGE_REPO:$IMAGE_VERSION" -f "$DOCKERFILE_PATH" . 2>&1) + docker build --no-cache -t "$IMAGE_REPO:$IMAGE_VERSION" -f "$DOCKERFILE_PATH" . 2>&1 ####################### # Load the error code # @@ -193,7 +228,6 @@ BuildImage() if [ $ERROR_CODE -ne 0 ]; then # ERROR echo "ERROR! failed to [build] Dockerfile!" - echo "ERROR:[$BUILD_CMD]" exit 1 else # SUCCESS @@ -214,11 +248,10 @@ UploadImage() echo "----------------------------------------------" echo "" - ############################################ # Upload the docker image that was created # ############################################ - UPLOAD_CMD=$(docker push "$IMAGE_REPO:$IMAGE_VERSION" 2>&1) + docker push "$IMAGE_REPO:$IMAGE_VERSION" 2>&1 ####################### # Load the error code # @@ -230,13 +263,11 @@ UploadImage() ############################## if [ $ERROR_CODE -ne 0 ]; then # ERROR - echo "ERROR! failed to [build] Dockerfile!" - echo "ERROR:[$UPLOAD_CMD]" + echo "ERROR! failed to [upload] Dockerfile!" exit 1 else # SUCCESS echo "Successfully Uploaded Docker image to DockerHub!" - echo "Info:[$UPLOAD_CMD]" fi ######################### diff --git a/.github/workflows/cleanup-DEV.yml b/.github/workflows/cleanup-DEV.yml new file mode 100644 index 00000000..5358be0d --- /dev/null +++ b/.github/workflows/cleanup-DEV.yml @@ -0,0 +1,52 @@ +--- +########################## +########################## +## Cleanup Docker Image ## +########################## +########################## + +# +# Documentation: +# https://help.github.com/en/articles/workflow-syntax-for-github-actions +# + +######################################## +# Run job when PR is merged and closed # +######################################## + +on: + pull_request: + +############### +# Set the Job # +############### +jobs: + build: + # Name the Job + name: Cleanup Docker Image - DEV + # Set the agent to run on + runs-on: ubuntu-latest + ################## + # Load all steps # + ################## + steps: + ########################## + # Checkout the code base # + ########################## + - name: Checkout Code + if: github.event.pull_request.merged + uses: actions/checkout@master + + ###################### + # Run Removal script # + ###################### + - name: Remove old image from DockerHub + if: github.event.pull_request.merged + env: + # Set the Env Vars + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + IMAGE_REPO: admiralawkbar/super-linter + IMAGE_VERSION: ${{ GITHUB_SHA }} + shell: bash + run: .automation/cleanup-docker.sh diff --git a/.github/workflows/deploy-DEV.yml b/.github/workflows/deploy-DEV.yml new file mode 100644 index 00000000..e8ce2c47 --- /dev/null +++ b/.github/workflows/deploy-DEV.yml @@ -0,0 +1,55 @@ +--- +######################### +######################### +## Deploy Docker Image ## +######################### +######################### + +# +# Documentation: +# https://help.github.com/en/articles/workflow-syntax-for-github-actions +# + +####################################### +# Start the job on all push to master # +####################################### +############################# +# Start the job on all push # +############################# +on: + push: + branches-ignore: + - 'master' + +############### +# Set the Job # +############### +jobs: + build: + # Name the Job + name: Deploy Docker Image - DEV + # Set the agent to run on + runs-on: ubuntu-latest + ################## + # Load all steps # + ################## + steps: + ########################## + # Checkout the code base # + ########################## + - name: Checkout Code + uses: actions/checkout@master + + ##################### + # Run Deploy script # + ##################### + - name: Deploy image to DockerHub + env: + # Set the Env Vars + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + IMAGE_REPO: admiralawkbar/super-linter + IMAGE_VERSION: ${{ GITHUB_SHA }} + DOCKERFILE_PATH: Dockerfile + shell: bash + run: .automation/upload-docker.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy-PROD.yml similarity index 97% rename from .github/workflows/deploy.yml rename to .github/workflows/deploy-PROD.yml index 3696a8ae..ecb7038b 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy-PROD.yml @@ -24,7 +24,7 @@ on: jobs: build: # Name the Job - name: Deploy Docker Image + name: Deploy Docker Image - PROD # Set the agent to run on runs-on: ubuntu-latest ################## From 0c4cfa88d049686f3da5fe1c660f519738307f93 Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 9 Jan 2020 13:10:40 -0600 Subject: [PATCH 2/4] do it another way --- .automation/cleanup-docker.sh | 2 +- .automation/upload-docker.sh | 16 ++++------------ .github/workflows/cleanup-DEV.yml | 1 - .github/workflows/deploy-DEV.yml | 1 - 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/.automation/cleanup-docker.sh b/.automation/cleanup-docker.sh index 15a082b3..04c1d662 100644 --- a/.automation/cleanup-docker.sh +++ b/.automation/cleanup-docker.sh @@ -20,7 +20,7 @@ GITHUB_WORKSPACE="${GITHUB_WORKSPACE}" # Github Workspace DOCKER_USERNAME="${DOCKER_USERNAME}" # Username to login to DockerHub DOCKER_PASSWORD="${DOCKER_PASSWORD}" # Password to login to DockerHub IMAGE_REPO="${IMAGE_REPO}" # Image repo to upload the image -IMAGE_VERSION="${IMAGE_VERSION}" # Version to tag the image +IMAGE_VERSION="${GITHUB_SHA}" # Version to tag the image DOCKERFILE_PATH="${DOCKERFILE_PATH}" # Path to the Dockerfile to be uploaded ################################################################################ diff --git a/.automation/upload-docker.sh b/.automation/upload-docker.sh index 6bbdf171..6df046b0 100755 --- a/.automation/upload-docker.sh +++ b/.automation/upload-docker.sh @@ -98,17 +98,8 @@ ValidateInput() # Validate IMAGE_VERSION # ########################## if [ -z "$IMAGE_VERSION" ]; then - echo "ERROR! Failed to get [IMAGE_VERSION]!" - echo "ERROR:[$IMAGE_VERSION]" - exit 1 - else - echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]" - fi - - ################################################## - # Check if we need to get the name of the branch # - ################################################## - if [[ "$IMAGE_VERSION" != "latest" ]]; then + echo "WARN! Failed to get [IMAGE_VERSION]!" + echo "Pulling from Branch Name..." ############################## # Get the name of the branch # ############################## @@ -137,9 +128,10 @@ ValidateInput() # Set the IMAGE_VERSION to the BRANCH_NAME # ############################################ IMAGE_VERSION="$BRANCH_NAME" + else + echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]" fi - ############################ # Validate DOCKERFILE_PATH # ############################ diff --git a/.github/workflows/cleanup-DEV.yml b/.github/workflows/cleanup-DEV.yml index 5358be0d..1226b9fd 100644 --- a/.github/workflows/cleanup-DEV.yml +++ b/.github/workflows/cleanup-DEV.yml @@ -47,6 +47,5 @@ jobs: DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} IMAGE_REPO: admiralawkbar/super-linter - IMAGE_VERSION: ${{ GITHUB_SHA }} shell: bash run: .automation/cleanup-docker.sh diff --git a/.github/workflows/deploy-DEV.yml b/.github/workflows/deploy-DEV.yml index e8ce2c47..5aac725d 100644 --- a/.github/workflows/deploy-DEV.yml +++ b/.github/workflows/deploy-DEV.yml @@ -49,7 +49,6 @@ jobs: DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} IMAGE_REPO: admiralawkbar/super-linter - IMAGE_VERSION: ${{ GITHUB_SHA }} DOCKERFILE_PATH: Dockerfile shell: bash run: .automation/upload-docker.sh From c4d74841a7b1c56f7631c0bf832da6c3fc32c8e0 Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 9 Jan 2020 13:12:36 -0600 Subject: [PATCH 3/4] more? --- .github/workflows/cleanup-DEV.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/cleanup-DEV.yml b/.github/workflows/cleanup-DEV.yml index 1226b9fd..d42416be 100644 --- a/.github/workflows/cleanup-DEV.yml +++ b/.github/workflows/cleanup-DEV.yml @@ -16,6 +16,7 @@ on: pull_request: + types: [closed] ############### # Set the Job # From 387696834558e3c946df314c6f69021a090c3f87 Mon Sep 17 00:00:00 2001 From: Lucas Gravley <29484535+admiralAwkbar@users.noreply.github.com> Date: Thu, 9 Jan 2020 13:19:21 -0600 Subject: [PATCH 4/4] adding the fixes --- .automation/cleanup-docker.sh | 4 ++-- .automation/upload-docker.sh | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.automation/cleanup-docker.sh b/.automation/cleanup-docker.sh index 04c1d662..594b8230 100644 --- a/.automation/cleanup-docker.sh +++ b/.automation/cleanup-docker.sh @@ -202,7 +202,7 @@ RemoveImage() TOKEN=$(curl -s -k \ -H "Content-Type: application/json" \ -X POST \ - -d '{"username": "'$DOCKER_USERNAME'", "password": "'$DOCKER_PASSWORD'"}' \ + -d "{\"username\": \"$DOCKER_USERNAME\", \"password\": \"$DOCKER_PASSWORD\"}" \ "https://hub.docker.com/v2/users/login/" | jq -r .token 2>&1) ####################### @@ -245,7 +245,7 @@ RemoveImage() exit 1 else # SUCCESS - echo "Successfully [removed] Docker image tag from DockerHub!" + echo "Successfully [removed] Docker image tag:[$IMAGE_VERSION] from DockerHub!" fi } ################################################################################ diff --git a/.automation/upload-docker.sh b/.automation/upload-docker.sh index 6df046b0..36c8dd02 100755 --- a/.automation/upload-docker.sh +++ b/.automation/upload-docker.sh @@ -128,6 +128,7 @@ ValidateInput() # Set the IMAGE_VERSION to the BRANCH_NAME # ############################################ IMAGE_VERSION="$BRANCH_NAME" + echo "Tag:[$IMAGE_VERSION]" else echo "Successfully found:[IMAGE_VERSION], value:[$IMAGE_VERSION]" fi