From 580d3f351decfe1f0385db2624bf83c32be4c464 Mon Sep 17 00:00:00 2001 From: Mahyar Iranibazaz Date: Tue, 15 Oct 2024 01:47:03 +0800 Subject: [PATCH 1/3] feat(ci): upgrade deploy script - add: force plugin version bump in deployment by unix timestamp - add: logs with types and colors - add: buffered logs - add: background process manager - add: deployment result status --- cli/staging/deploy.sh | 134 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 10 deletions(-) diff --git a/cli/staging/deploy.sh b/cli/staging/deploy.sh index a0a2434..4c2983e 100644 --- a/cli/staging/deploy.sh +++ b/cli/staging/deploy.sh @@ -1,19 +1,133 @@ #!/bin/bash -cd /opt/hejbit + +RED='\033[0;31m' +GREEN='\033[0;32m' +BLUE='\033[0;34m' +YELLOW='\033[0;33m' +NC='\033[0m' + +log_message() { + echo -e "${BLUE}$1${NC}" +} + +log_warning() { + echo -e "${YELLOW}$1${NC}" +} + +log_success() { + echo -e "${GREEN}$1${NC}" +} + +log_error() { + echo -e "${RED}$1${NC}" +} + +log_gap() { + echo -e "-------------------------------------" +} + +output=() +buffer() { + local line=$1 + output+=("$line") +} + +print_buffer() { + for line in "${output[@]}" + do + echo -e "$line" + done + output=() +} + +deploy_error() { + local version=$1 + local result=$2 + print_buffer + log_error "$result" + log_error "failed to deploy hejbit-$version" + log_gap + exit 1 +} + +exec_action() { + local action=$1 + local message=$2 + buffer "$(log_message "$message")" + result=$($action) || deploy_error "$version" "$result" + buffer "$result" +} deploy() { local version=$1 - cd hejbit-$version - git pull - npm install - npm run build - docker exec -u www-data hejbit-$version-nextcloud-1 php occ upgrade + cd hejbit-"$version" || log_error "hejbit-$version not found" + + buffer "$(log_warning "deploying hejbit-$version")" + + exec_action "sync_code" "syncing code" + + exec_action "build_app" "building app" + + exec_action "nextcloud_upgrade" "upgrading Nextcloud" + + buffer "$(log_success "deployed hejbit-$version")" + + print_buffer + log_gap } -for version in {28..30} -do - deploy $version & +sync_code() { + git reset --hard > /dev/null 2>&1 + git pull || return 1 +} + +build_app() { + result=$(npm install 2>&1) + status=$? + echo -e "$result" + + if [ $status -ne 0 ]; then + result=$(npm run build 2>&1) + status=$? + echo -e "$result" + fi + + return $status +} + +nextcloud_upgrade() { + INFO_XML="appinfo/info.xml" + CURRENT_VERSION=$(grep -oPm1 "(?<=)[^<]+" "$INFO_XML") + NEW_VERSION="${CURRENT_VERSION}_${TIMESTAMP}" + + sed -i "s/${CURRENT_VERSION}<\/version>/${NEW_VERSION}<\/version>/" "$INFO_XML" + log_message "bumped version to $NEW_VERSION" + + result=$(docker exec -u www-data hejbit-"$version"-nextcloud-1 php occ upgrade 2>&1) + status=$? + echo -e "$result" + return $status +} + +cd /opt/hejbit || log_error "/opt/hejbit not found" +log_message "deploying hejbit to staging" +log_gap + +DEPLOYMENT=() +TIMESTAMP=$(date +%s) +for version in {28..30}; do + deploy "$version" & + DEPLOYMENT+=($!) done -wait +statuses=0 +for pid in "${DEPLOYMENT[@]}"; do + wait "$pid" + statuses+=$? +done +if [ $statuses -ne 0 ]; then + log_error "failed to deploy hejbit to staging" + exit 1 +fi +log_success "deployed hejbit to staging" From 8a546d45a157f42a8d9e6a02fee955c2ef4495f0 Mon Sep 17 00:00:00 2001 From: Mahyar Iranibazaz Date: Tue, 15 Oct 2024 02:01:00 +0800 Subject: [PATCH 2/3] refactor(ci): code style and logs --- cli/staging/deploy.sh | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/cli/staging/deploy.sh b/cli/staging/deploy.sh index 4c2983e..e49ec43 100644 --- a/cli/staging/deploy.sh +++ b/cli/staging/deploy.sh @@ -6,11 +6,15 @@ BLUE='\033[0;34m' YELLOW='\033[0;33m' NC='\033[0m' +log () { + echo -e "$1" +} + log_message() { echo -e "${BLUE}$1${NC}" } -log_warning() { +log_note() { echo -e "${YELLOW}$1${NC}" } @@ -35,12 +39,12 @@ buffer() { print_buffer() { for line in "${output[@]}" do - echo -e "$line" + log "$line" done output=() } -deploy_error() { +failed() { local version=$1 local result=$2 print_buffer @@ -50,25 +54,25 @@ deploy_error() { exit 1 } -exec_action() { +action() { local action=$1 local message=$2 buffer "$(log_message "$message")" - result=$($action) || deploy_error "$version" "$result" + result=$($action) || failed "$version" "$result" buffer "$result" } deploy() { local version=$1 - cd hejbit-"$version" || log_error "hejbit-$version not found" + cd hejbit-"$version" || failed "hejbit-$version not found" - buffer "$(log_warning "deploying hejbit-$version")" + buffer "$(log_note "deploying hejbit-$version")" - exec_action "sync_code" "syncing code" + action "sync_code" "syncing code" - exec_action "build_app" "building app" + action "build_app" "building app" - exec_action "nextcloud_upgrade" "upgrading Nextcloud" + action "nextcloud_upgrade" "upgrading nextcloud" buffer "$(log_success "deployed hejbit-$version")" @@ -78,18 +82,18 @@ deploy() { sync_code() { git reset --hard > /dev/null 2>&1 - git pull || return 1 + git pull 2>&1 || return 1 } build_app() { result=$(npm install 2>&1) status=$? - echo -e "$result" + log "$result" if [ $status -ne 0 ]; then result=$(npm run build 2>&1) status=$? - echo -e "$result" + log "$result" fi return $status @@ -105,12 +109,12 @@ nextcloud_upgrade() { result=$(docker exec -u www-data hejbit-"$version"-nextcloud-1 php occ upgrade 2>&1) status=$? - echo -e "$result" + log "$result" return $status } cd /opt/hejbit || log_error "/opt/hejbit not found" -log_message "deploying hejbit to staging" +log_note "deploying hejbit to staging" log_gap DEPLOYMENT=() From 76b35c2b4c507c8fd18a61ce3ba064475313d70e Mon Sep 17 00:00:00 2001 From: Mahyar Iranibazaz Date: Wed, 16 Oct 2024 00:16:38 +0800 Subject: [PATCH 3/3] fix(ci): fix npm build action --- cli/staging/deploy.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/staging/deploy.sh b/cli/staging/deploy.sh index e49ec43..832b433 100644 --- a/cli/staging/deploy.sh +++ b/cli/staging/deploy.sh @@ -90,11 +90,11 @@ build_app() { status=$? log "$result" - if [ $status -ne 0 ]; then - result=$(npm run build 2>&1) - status=$? - log "$result" - fi + [ $status -ne 0 ] && return $status + + result=$(npm run build 2>&1) + status=$? + log "$result" return $status }