From d0e1680c0d836616f94fd999dd09b9ec27323713 Mon Sep 17 00:00:00 2001 From: Marcel Amirault Date: Thu, 20 Oct 2022 10:54:42 +0000 Subject: Re-implement Review Apps 2.0 (with correct environments) --- scripts/deploy-review-app | 68 +++++++++++++++++++++++++++++++++++++++++++++ scripts/review-replace-urls | 4 +++ 2 files changed, 72 insertions(+) create mode 100755 scripts/deploy-review-app create mode 100755 scripts/review-replace-urls (limited to 'scripts') diff --git a/scripts/deploy-review-app b/scripts/deploy-review-app new file mode 100755 index 00000000..c6c09c85 --- /dev/null +++ b/scripts/deploy-review-app @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +# +# Copied and adapted from https://gitlab.com/gitlab-com/www-gitlab-com/-/blob/10b95368133ea0a23326d293b7a80fc71317d011/scripts/deploy +# + +# Exponential backoff, found from StackOverflow at https://stackoverflow.com/questions/8350942/how-to-re-run-the-curl-command-automatically-when-the-error-occurs/8351489#8351489 +# Retries a command a with backoff. +# +# The retry count is given by ATTEMPTS (default 5), the +# initial backoff timeout is given by TIMEOUT in seconds +# (default 1.) +# +# Successive backoffs double the timeout. +# +# Beware of set -e killing your whole script! +function with_backoff { + local max_attempts=${ATTEMPTS-5} + local timeout=${TIMEOUT-1} + local attempt=0 + local exitCode=0 + + while [[ $attempt < $max_attempts ]] + do + "$@" + exitCode=$? + + if [[ $exitCode == 0 ]] + then + break + fi + + echo "Failure! Retrying in $timeout.." 1>&2 + sleep $timeout + attempt=$(( attempt + 1 )) + timeout=$(( timeout * 2 )) + done + + if [[ $exitCode != 0 ]] + then + echo "You've failed me for the last time! ($@)" 1>&2 + fi + + return $exitCode +} + +echo "Starting deploy for review app." + +gcp_project=$GCP_PROJECT_REVIEW_APPS +gcp_bucket=$GCP_BUCKET_REVIEW_APPS +gcp_service_account_key=$GCP_SERVICE_ACCOUNT_KEY_REVIEW_APPS +cache_control_max_age='60' +src='public/' +dest="gs://$gcp_bucket/$CI_COMMIT_REF_SLUG" + +echo "$gcp_service_account_key" > key.json +gcloud auth activate-service-account --key-file key.json +gcloud config set project "$gcp_project" + +if [ "$DEPLOY_DELETE_APP" = 'true' ]; then + echo "Deleting review app from ${dest}..." + echo "gsutil -m rm -r \"$dest\"" + gsutil -m rm -r "$dest" +else + echo "Deploying review app to ${dest}..." + echo "gsutil -h \"Cache-Control:public, max-age=$cache_control_max_age\" -m cp -z css,html,js,txt -c -r \"$src\" \"$dest\"" + with_backoff gsutil -h "Cache-Control:public, max-age=$cache_control_max_age" -m cp -z css,html,js,txt -c -r "$src" "$dest" +fi diff --git a/scripts/review-replace-urls b/scripts/review-replace-urls new file mode 100755 index 00000000..f78e8324 --- /dev/null +++ b/scripts/review-replace-urls @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +# Note this uses linux-specific (GNU) find syntax, it does not work with MacOS (BSD) find syntax because of the usage of the 'regextype' flag (and possibly other reasons) +find public/ -type f -regextype egrep -iregex ".*\.(html|js|css|json|xml|txt)" -exec sed --in-place "s#https\?://docs.gitlab.com#https://$CI_COMMIT_REF_SLUG.docs.gitlab-review.app#g" "{}" +; -- cgit v1.2.3