From d9a4724a3d4a341de01f687c4ad970617e319320 Mon Sep 17 00:00:00 2001 From: Barry Matheney Date: Thu, 20 Jun 2024 16:23:02 -0400 Subject: [PATCH] 20240620_162302 --- .gitignore | 1 + bitbucket-pipelines.yml | 6 ++- scr__test__local_scripts.sh | 11 ++++ ...les.sh => scr__test__pipeline_variables.sh | 0 scr_local_secrets_example.sh | 9 ++++ scr_non_secret_env_vars.sh | 4 +- scr_scrub_bitbucket_env_vars.sh | 6 +-- scr_validate_mandatory_variables.sh | 53 +++++++++++++++++++ 8 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 scr__test__local_scripts.sh rename scr_test_pipeline_variables.sh => scr__test__pipeline_variables.sh (100%) create mode 100644 scr_local_secrets_example.sh create mode 100644 scr_validate_mandatory_variables.sh diff --git a/.gitignore b/.gitignore index af0cb9bb0..3deaa2739 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ build/* _site/ *.css !petclinic.css +scr_local_secrets.sh diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 49acb5860..a85bf68a8 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -13,12 +13,16 @@ pipelines: caches: - docker script: - - ps -p $$ && echo whoami + - ps -p $$ && whoami - cat /etc/os-release - echo "$SHELL" - source scr_scrub_bitbucket_env_vars.sh - source scr_non_secret_env_vars.sh - printenv | sort + - source scr_validate_mandatory_variables.sh + - echo "$HARBOR_ACCESS_TOKEN" | docker login "$HARBOR_LOGIN_ADDRESS" -u "$HARBOR_USERNAME" --password-stdin + - docker build -t "${DOCKER_IMAGE_NAME}:${DOCKER_TAG_PIPELINE_UUID}" . + - docker push "${DOCKER_IMAGE_NAME}:${DOCKER_TAG_PIPELINE_UUID}" # - docker build - # - source pack_install.sh # - which pack && pack version diff --git a/scr__test__local_scripts.sh b/scr__test__local_scripts.sh new file mode 100644 index 000000000..9e56d29ab --- /dev/null +++ b/scr__test__local_scripts.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +echo "$(date +%Y%m%d_%H%M%S) Entering $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" + + +source scr__test__pipeline_variables.sh +source scr_local_secrets.sh +source scr_scrub_bitbucket_env_vars.sh +source scr_non_secret_env_vars.sh +source scr_validate_mandatory_variables.sh + +echo "$(date +%Y%m%d_%H%M%S) Leaving $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" diff --git a/scr_test_pipeline_variables.sh b/scr__test__pipeline_variables.sh similarity index 100% rename from scr_test_pipeline_variables.sh rename to scr__test__pipeline_variables.sh diff --git a/scr_local_secrets_example.sh b/scr_local_secrets_example.sh new file mode 100644 index 000000000..f8a883f97 --- /dev/null +++ b/scr_local_secrets_example.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +echo "$(date +%Y%m%d_%H%M%S) - Entering $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" + +export HARBOR_USERNAME="barry_matheney@yahoo.com" +export HARBOR_LOGIN_ADDRESS="c8n.io/barrymatheney" +export HARBOR_ACCESS_TOKEN="whatever the access token that you created is" + +echo "$(date +%Y%m%d_%H%M%S) - Leaving $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" + diff --git a/scr_non_secret_env_vars.sh b/scr_non_secret_env_vars.sh index 480ffaae7..ffe6a8bac 100644 --- a/scr_non_secret_env_vars.sh +++ b/scr_non_secret_env_vars.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash echo "$(date +%Y%m%d_%H%M%S) - Entering $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" -export DOCKER_IMAGE_NAME="${BITBUCKET_REPO_FULL_NAME_SCRUBBED}" -export DOCKER_TAG_COMMIT_SHA="${BITBUCKET_COMMIT_SCRUBBED}" +export DOCKER_IMAGE_NAME="${HARBOR_LOGIN_ADDRESS}/${BITBUCKET_REPO_FULL_NAME_SCRUBBED}" +export DOCKER_TAG_PIPELINE_UUID="${BITBUCKET_PIPELINE_UUID_SCRUBBED}" export DOCKER_TAG_COMMIT_SHA="${BITBUCKET_COMMIT_SCRUBBED}" export DOCKER_TAG_BRANCH_NAME="${BITBUCKET_BRANCH_SCRUBBED}" export DOCKER_TAG_BRANCH_NAME="${BITBUCKET_PIPELINE_UUID_SCRUBBED}" diff --git a/scr_scrub_bitbucket_env_vars.sh b/scr_scrub_bitbucket_env_vars.sh index 19fbec55b..c87c048a7 100644 --- a/scr_scrub_bitbucket_env_vars.sh +++ b/scr_scrub_bitbucket_env_vars.sh @@ -1,11 +1,7 @@ #!/usr/bin/env bash echo "$(date +%Y%m%d_%H%M%S) - Entering $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" -if [ -z ${BITBUCKET_BRANCH+x} ]; then # this shouldn't be defined locally, so we can use it to determine local vs pipeline run and tests... - source scr_test_pipeline_variables.sh -else - echo "Running in a pipeline, using Bitbuckets real variable values." -fi + # List of characters to replace declare -A char_replacements=( diff --git a/scr_validate_mandatory_variables.sh b/scr_validate_mandatory_variables.sh new file mode 100644 index 000000000..a5f12136b --- /dev/null +++ b/scr_validate_mandatory_variables.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +echo "$(date +%Y%m%d_%H%M%S) - Entering $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" + +# Function to check if a variable is defined +check_variable_defined() { + local var_name=$1 + if [ -z "${!var_name}" ]; then + echo "$var_name" + fi +} + +# List of necessary variables to check +necessary_variables=( + "DOCKER_IMAGE_NAME" + "HARBOR_LOGIN_ADDRESS" + "BITBUCKET_REPO_FULL_NAME_SCRUBBED" + "DOCKER_TAG_PIPELINE_UUID" + "BITBUCKET_PIPELINE_UUID_SCRUBBED" + "DOCKER_TAG_COMMIT_SHA" + "BITBUCKET_COMMIT_SCRUBBED" + "DOCKER_TAG_BRANCH_NAME" + "BITBUCKET_BRANCH_SCRUBBED" + "DOCKER_TAG_BRANCH_NAME" + "BITBUCKET_PIPELINE_UUID_SCRUBBED" + "HARBOR_ACCESS_TOKEN" + "HARBOR_USERNAME" + +) + +# Array to store undefined variables +undefined_variables=() + +# Iterate through the list of necessary variables and check if they are defined +for var in "${necessary_variables[@]}"; do + result=$(check_variable_defined "$var") + if [ -n "$result" ]; then + undefined_variables+=("$result") + fi +done + +# Print out which variables are not defined +if [ ${#undefined_variables[@]} -ne 0 ]; then + echo "The following variables are not defined:" + for var in "${undefined_variables[@]}"; do + echo "$var" + done +else + echo "MSG: All necessary variables are defined, should be good to go." +fi + + +echo "$(date +%Y%m%d_%H%M%S) - Leaving $(basename "$(realpath "${BASH_SOURCE[0]}")") on host $(hostname)" +