From 58121636ccf3945981f20715a8cf24d101c5e58a Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 13:25:45 +0100 Subject: [PATCH 01/13] Add logic for extracting env from input tag --- app-deploy.sh | 13 +++++----- sources/__auto_update.sh | 4 ++++ sources/__base_tag_handling.sh | 6 +++-- sources/__constants.sh | 17 ++++++++++++++ sources/__env_extractor.sh | 43 ++++++++++++++++++++++++++++++++++ sources/__init.sh | 4 ++++ 6 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 sources/__constants.sh create mode 100644 sources/__env_extractor.sh diff --git a/app-deploy.sh b/app-deploy.sh index c56dc36..9238356 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -1,11 +1,13 @@ #!/usr/bin/env bash source ./.deploy-options.sh +source /usr/local/bin/.app-deploy-sources/__constants.sh source /usr/local/bin/.app-deploy-sources/__auto_update.sh source /usr/local/bin/.app-deploy-sources/__init.sh source /usr/local/bin/.app-deploy-sources/__initial_checkup.sh source /usr/local/bin/.app-deploy-sources/__base_tag_handling.sh source /usr/local/bin/.app-deploy-sources/__deploy_tags.sh +source /usr/local/bin/.app-deploy-sources/__env_extractor.sh ############################################################### # DEPLOY SCRIPT # @@ -35,6 +37,8 @@ normal=$(tput sgr0) function main { + __header_print + # BASE INFO # commit, tag, synced head,... __initial_checkup @@ -66,13 +70,6 @@ function main { if $use_automatic_console_clean ; then clear fi -echo -echo "###############################################################" -echo "# DEPLOY SCRIPT #" -echo "# #" -echo "# Copyright (c) 2024 Infinum. #" -echo "###############################################################" -echo if [ "$1" == '--update' ] ; then __script_auto_update @@ -80,6 +77,8 @@ elif [ "$1" == 'init' ] ; then __init elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" main +elif [ "$1" == 'environments' ] ; then + __env_extractor "$2" else echo echo "Unsuported command!" diff --git a/sources/__auto_update.sh b/sources/__auto_update.sh index aa092e2..93f008e 100644 --- a/sources/__auto_update.sh +++ b/sources/__auto_update.sh @@ -1,8 +1,12 @@ +source /usr/local/bin/.app-deploy-sources/__constants.sh + ################################# # UPDATE # ################################# function __script_auto_update { + + __header_print echo echo "Please wait until main script is finished with updating..." echo diff --git a/sources/__base_tag_handling.sh b/sources/__base_tag_handling.sh index a87cfbb..6c01006 100644 --- a/sources/__base_tag_handling.sh +++ b/sources/__base_tag_handling.sh @@ -1,3 +1,5 @@ +source /usr/local/bin/.app-deploy-sources/__constants.sh + ################################# # CREATE TAG # ################################# @@ -77,7 +79,7 @@ function __create_trigger_ci_timestamp_tag { tags_to_deploy=() # Prefix ci - trigger_tag="ci/" + trigger_tag="$TRIGGER_TAG_PREFIX" # Environments for target in "${environments_to_build[@]}"; do @@ -85,7 +87,7 @@ function __create_trigger_ci_timestamp_tag { done # Sufix timestamp - trigger_tag+="$(date +%Y-%m-%dT%H-%M-%S)" + trigger_tag+="$TRIGGER_TAG_SUFIX" # Assign to shared property tags_to_deploy=("$trigger_tag") diff --git a/sources/__constants.sh b/sources/__constants.sh new file mode 100644 index 0000000..9a636bd --- /dev/null +++ b/sources/__constants.sh @@ -0,0 +1,17 @@ +################################# +# CONSTANTS # +################################# + +function __header_print() { + echo + echo "###############################################################" + echo "# DEPLOY SCRIPT #" + echo "# #" + echo "# Copyright (c) 2024 Infinum. #" + echo "###############################################################" + echo +} + +TRIGGER_TAG_PREFIX="ci/" +TRIGGER_TAG_SUFIX="$(date +%Y-%m-%dT%H-%M-%S)" +TRIGGER_TAG_SUFIX_REGEX=([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}) \ No newline at end of file diff --git a/sources/__env_extractor.sh b/sources/__env_extractor.sh new file mode 100644 index 0000000..1bf3704 --- /dev/null +++ b/sources/__env_extractor.sh @@ -0,0 +1,43 @@ +source /usr/local/bin/.app-deploy-sources/__constants.sh + +################################# +# ENVIRONMENT EXTRACTOR # +################################# + +function __env_extractor { + + # Check if input is valid + if [ -z "$1" ] ; then + echo "Missing input value!" + echo + echo "Usage: app-deploy environments input" + echo "- input: a value from which 'environments' command shoudl extract data. Should be trigger tag generated by this script." + exit 1 + fi + + if [[ $1 =~ ^$TRIGGER_TAG_PREFIX(.+)/$TRIGGER_TAG_SUFIX_REGEX$ ]]; then + paths="${BASH_REMATCH[1]}" + + # Split the captured paths into individual words + IFS='/' read -ra words <<< "$paths" + + for env in "${words[@]}"; do + ENVIRONMENTS+="$env"$'\n' + done + + # Remove last empty line + ENVIRONMENTS=$(echo -n "$ENVIRONMENTS") + echo "$ENVIRONMENTS" + else + echo + echo "Incorrect input value." + echo + echo "The environment can only be extracted from the official trigger tag." + echo + echo "Please use this script for generating the trigger tag, or use the tag in the format:" + echo "prefix: ci/" + echo "environments: env1/env2/..." + echo "sufix: /timestamp where timestamp should be in the format of +%Y-%m-%dT%H-%M-%S (2024-12-06T11-24-53)" + echo "EXAMPLE: ci/env1/env2/2024-12-06T11-24-53" + fi +} \ No newline at end of file diff --git a/sources/__init.sh b/sources/__init.sh index b7b8d0c..321fb60 100644 --- a/sources/__init.sh +++ b/sources/__init.sh @@ -1,9 +1,13 @@ +source /usr/local/bin/.app-deploy-sources/__constants.sh + ################################# # INIT NEW PROJECT # ################################# function __init { + __header_print + if [ -e "./.deploy-options.sh" ]; then echo "Options file already exists." echo "If you continue, stored options will be overriden!" From b8aa24653a68a5235335a337361822855248a561 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 14:49:01 +0100 Subject: [PATCH 02/13] Add silent install support --- install.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 5960b81..b09d0e4 100755 --- a/install.sh +++ b/install.sh @@ -19,10 +19,13 @@ normal=$(tput sgr0) echo "==> ${bold}This script will install:${normal}" echo "/usr/local/bin/app-deploy" echo -read -r -p "Do you want to proceed? [y/n] " c -if ! [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then - exit 1 +if [[ $1 =~ "--silent" ]]; then + read -r -p "Do you want to proceed? [y/n] " c + if ! [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then + exit 1 + fi fi + echo echo "Fetching script data..." From f41f2a519faf778d02703176417036cd6e0841e4 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 14:57:22 +0100 Subject: [PATCH 03/13] Trigger update --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index b09d0e4..d051dca 100755 --- a/install.sh +++ b/install.sh @@ -19,7 +19,7 @@ normal=$(tput sgr0) echo "==> ${bold}This script will install:${normal}" echo "/usr/local/bin/app-deploy" echo -if [[ $1 =~ "--silent" ]]; then +if ! [[ $1 == "--silent" ]]; then read -r -p "Do you want to proceed? [y/n] " c if ! [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then exit 1 From f7de23b93760410f8242164bbd64263d6decd34f Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 16:02:44 +0100 Subject: [PATCH 04/13] Update install script --- install.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/install.sh b/install.sh index d051dca..5a85fbc 100755 --- a/install.sh +++ b/install.sh @@ -19,7 +19,8 @@ normal=$(tput sgr0) echo "==> ${bold}This script will install:${normal}" echo "/usr/local/bin/app-deploy" echo -if ! [[ $1 == "--silent" ]]; then + +if ! [[ "$0" == "--silent" ]]; then read -r -p "Do you want to proceed? [y/n] " c if ! [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then exit 1 From da8dddeb6864d8464a8f99a8ba2ce3f792e0d7e2 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 17:21:30 +0100 Subject: [PATCH 05/13] Update extractor logic --- sources/__env_extractor.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/sources/__env_extractor.sh b/sources/__env_extractor.sh index 1bf3704..333452d 100644 --- a/sources/__env_extractor.sh +++ b/sources/__env_extractor.sh @@ -26,7 +26,6 @@ function __env_extractor { done # Remove last empty line - ENVIRONMENTS=$(echo -n "$ENVIRONMENTS") echo "$ENVIRONMENTS" else echo From 6ee21f4c9b5b77703f7e73665b32dd02e3809c82 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 17:34:40 +0100 Subject: [PATCH 06/13] Return value from extractor --- app-deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app-deploy.sh b/app-deploy.sh index 9238356..ff936df 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -78,7 +78,7 @@ elif [ "$1" == 'init' ] ; then elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" main elif [ "$1" == 'environments' ] ; then - __env_extractor "$2" + echo __env_extractor "$2" else echo echo "Unsuported command!" From 9877a45edb7b07d9995c070ea44b26db892294f7 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 17:37:18 +0100 Subject: [PATCH 07/13] Update install branch --- app-deploy.sh | 2 +- install.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app-deploy.sh b/app-deploy.sh index ff936df..9238356 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -78,7 +78,7 @@ elif [ "$1" == 'init' ] ; then elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" main elif [ "$1" == 'environments' ] ; then - echo __env_extractor "$2" + __env_extractor "$2" else echo echo "Unsuported command!" diff --git a/install.sh b/install.sh index 5a85fbc..81e21c8 100755 --- a/install.sh +++ b/install.sh @@ -38,7 +38,7 @@ else fi # Get install files -git clone --quiet https://github.com/infinum/app-deploy-script.git --branch feature/v2/local-trigger .app_deploy_tmp +git clone --quiet https://github.com/infinum/app-deploy-script.git --branch feature/v2/environments-export .app_deploy_tmp echo "Installing..." # Move main script to bin folder From 263e20ea4f3b9ef2a9499e81f2c412d3d51cc17f Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 17:44:52 +0100 Subject: [PATCH 08/13] Return extraction value --- app-deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app-deploy.sh b/app-deploy.sh index 9238356..6a22dd6 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -78,7 +78,7 @@ elif [ "$1" == 'init' ] ; then elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" main elif [ "$1" == 'environments' ] ; then - __env_extractor "$2" + echo $(__env_extractor "$2") else echo echo "Unsuported command!" From d5e81f0932d45b54f26923398211b190bc0308e7 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Fri, 6 Dec 2024 18:46:47 +0100 Subject: [PATCH 09/13] Update env extraction --- app-deploy.sh | 7 +++---- sources/__constants.sh | 7 ++++++- sources/__env_extractor.sh | 10 ++-------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app-deploy.sh b/app-deploy.sh index 6a22dd6..7b360fa 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -67,15 +67,14 @@ function main { # START EVERYTHING # ################################# -if $use_automatic_console_clean ; then - clear -fi - if [ "$1" == '--update' ] ; then + __clear_console __script_auto_update elif [ "$1" == 'init' ] ; then + __clear_console __init elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" + __clear_console main elif [ "$1" == 'environments' ] ; then echo $(__env_extractor "$2") diff --git a/sources/__constants.sh b/sources/__constants.sh index 9a636bd..ee0275e 100644 --- a/sources/__constants.sh +++ b/sources/__constants.sh @@ -2,7 +2,7 @@ # CONSTANTS # ################################# -function __header_print() { +function __header_print { echo echo "###############################################################" echo "# DEPLOY SCRIPT #" @@ -12,6 +12,11 @@ function __header_print() { echo } +function __clear_console { + if $use_automatic_console_clean ; then + clear + fi +} TRIGGER_TAG_PREFIX="ci/" TRIGGER_TAG_SUFIX="$(date +%Y-%m-%dT%H-%M-%S)" TRIGGER_TAG_SUFIX_REGEX=([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}) \ No newline at end of file diff --git a/sources/__env_extractor.sh b/sources/__env_extractor.sh index 333452d..8cf2d78 100644 --- a/sources/__env_extractor.sh +++ b/sources/__env_extractor.sh @@ -17,16 +17,10 @@ function __env_extractor { if [[ $1 =~ ^$TRIGGER_TAG_PREFIX(.+)/$TRIGGER_TAG_SUFIX_REGEX$ ]]; then paths="${BASH_REMATCH[1]}" - + # Split the captured paths into individual words IFS='/' read -ra words <<< "$paths" - - for env in "${words[@]}"; do - ENVIRONMENTS+="$env"$'\n' - done - - # Remove last empty line - echo "$ENVIRONMENTS" + echo "$(IFS=$'\n'; echo "${words[*]}")" else echo echo "Incorrect input value." From 0d760867842d7a536ca78745c23dcef8efa247ac Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Tue, 10 Dec 2024 13:03:32 +0100 Subject: [PATCH 10/13] Add exit codes --- app-deploy.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app-deploy.sh b/app-deploy.sh index 7b360fa..210f156 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -70,14 +70,18 @@ function main { if [ "$1" == '--update' ] ; then __clear_console __script_auto_update + exit 0 elif [ "$1" == 'init' ] ; then __clear_console __init + exit 0 elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" __clear_console main + exit 0 elif [ "$1" == 'environments' ] ; then - echo $(__env_extractor "$2") + __env_extractor "$2" + exit 0 else echo echo "Unsuported command!" From aa4b7643e16e78ce1d0daa7d1b4eef258f1ab66a Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Tue, 10 Dec 2024 13:26:56 +0100 Subject: [PATCH 11/13] Include deploy options only if file exists --- app-deploy.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app-deploy.sh b/app-deploy.sh index 210f156..ee1744b 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash -source ./.deploy-options.sh +if [ -f ./.deploy-options.sh ]; then + source ./.deploy-options.sh +fi source /usr/local/bin/.app-deploy-sources/__constants.sh source /usr/local/bin/.app-deploy-sources/__auto_update.sh source /usr/local/bin/.app-deploy-sources/__init.sh From d4be6ede43359c0b00299bb518b54ba4fd9c7cf6 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Tue, 10 Dec 2024 14:54:15 +0100 Subject: [PATCH 12/13] Add logs --- app-deploy.sh | 3 +++ sources/__env_extractor.sh | 2 ++ 2 files changed, 5 insertions(+) diff --git a/app-deploy.sh b/app-deploy.sh index ee1744b..459fd2e 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -69,6 +69,8 @@ function main { # START EVERYTHING # ################################# +echo "STARD SCRIPT" + if [ "$1" == '--update' ] ; then __clear_console __script_auto_update @@ -82,6 +84,7 @@ elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" main exit 0 elif [ "$1" == 'environments' ] ; then + echo "EXTRACT ENV" __env_extractor "$2" exit 0 else diff --git a/sources/__env_extractor.sh b/sources/__env_extractor.sh index 8cf2d78..15689a1 100644 --- a/sources/__env_extractor.sh +++ b/sources/__env_extractor.sh @@ -6,6 +6,8 @@ source /usr/local/bin/.app-deploy-sources/__constants.sh function __env_extractor { + echo "ENV EXTRACTOR CALLED" + # Check if input is valid if [ -z "$1" ] ; then echo "Missing input value!" From 95b094981bf81de2d6ef8aaa29eff9b8bdcbe9f0 Mon Sep 17 00:00:00 2001 From: Jasmin Abou Aldan Date: Tue, 10 Dec 2024 15:07:54 +0100 Subject: [PATCH 13/13] Fix text formatting --- app-deploy.sh | 54 +++---------------------------------- sources/__env_extractor.sh | 2 -- sources/__trigger_deploy.sh | 41 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 52 deletions(-) create mode 100644 sources/__trigger_deploy.sh diff --git a/app-deploy.sh b/app-deploy.sh index 459fd2e..38fabe4 100755 --- a/app-deploy.sh +++ b/app-deploy.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash -if [ -f ./.deploy-options.sh ]; then +if [ -z "$1" ] || [ "$1" == 'trigger' ] ; then source ./.deploy-options.sh + source /usr/local/bin/.app-deploy-sources/__trigger_deploy.sh fi source /usr/local/bin/.app-deploy-sources/__constants.sh source /usr/local/bin/.app-deploy-sources/__auto_update.sh @@ -24,72 +25,25 @@ source /usr/local/bin/.app-deploy-sources/__env_extractor.sh # Use global variables at your own risk as this can be overridden in the future. set -e -bold=$(tput bold) -normal=$(tput sgr0) - -################################# -# MAIN # -################################# - -# Private part of the script... -# -# In general, you don't have to edit -# this part of the script but feel free -# to edit any part of it as suits your needs. - -function main { - - __header_print - - # BASE INFO - # commit, tag, synced head,... - __initial_checkup - - # CREATE TAG - - deploy_options # Get from .deploy-options.sh, setup per project - __input_to_tags - - if [ -z "$script_version" ] || [ "$script_version" == "v1" ]; then - __create_app_version_and_build_number - elif [ "$script_version" == "v2" ]; then - __create_trigger_ci_timestamp_tag - fi - - # CREATE CHANGELOG - - __generate_tag_and_changelog - - # DEPLOY - - __push_tag_and_start_deploy -} ################################# # START EVERYTHING # ################################# -echo "STARD SCRIPT" - if [ "$1" == '--update' ] ; then __clear_console __script_auto_update - exit 0 elif [ "$1" == 'init' ] ; then __clear_console __init - exit 0 elif [ -z "$1" ] || [ "$1" == 'trigger' ] ; then # Empty input or "trigger" __clear_console - main - exit 0 + __trigger_deploy elif [ "$1" == 'environments' ] ; then - echo "EXTRACT ENV" __env_extractor "$2" - exit 0 else echo echo "Unsuported command!" echo - exit 0 + exit 29 fi diff --git a/sources/__env_extractor.sh b/sources/__env_extractor.sh index 15689a1..8cf2d78 100644 --- a/sources/__env_extractor.sh +++ b/sources/__env_extractor.sh @@ -6,8 +6,6 @@ source /usr/local/bin/.app-deploy-sources/__constants.sh function __env_extractor { - echo "ENV EXTRACTOR CALLED" - # Check if input is valid if [ -z "$1" ] ; then echo "Missing input value!" diff --git a/sources/__trigger_deploy.sh b/sources/__trigger_deploy.sh new file mode 100644 index 0000000..4159ae2 --- /dev/null +++ b/sources/__trigger_deploy.sh @@ -0,0 +1,41 @@ + +################################# +# DEPLOYMENT TRIGGER TAG # +################################# + +# Private part of the script... +# +# In general, you don't have to edit +# this part of the script but feel free +# to edit any part of it as suits your needs. + +bold=$(tput bold) +normal=$(tput sgr0) + +function __trigger_deploy { + + __header_print + + # BASE INFO + # commit, tag, synced head,... + __initial_checkup + + # CREATE TAG + + deploy_options # Get from .deploy-options.sh, setup per project + __input_to_tags + + if [ -z "$script_version" ] || [ "$script_version" == "v1" ]; then + __create_app_version_and_build_number + elif [ "$script_version" == "v2" ]; then + __create_trigger_ci_timestamp_tag + fi + + # CREATE CHANGELOG + + __generate_tag_and_changelog + + # DEPLOY + + __push_tag_and_start_deploy +} \ No newline at end of file