Refactor caprover deploy script

This commit is contained in:
Daniel Egger 2023-05-30 11:23:38 +02:00
parent 428ba37bb3
commit 956a685ac9
1 changed files with 36 additions and 42 deletions

View File

@ -3,52 +3,41 @@
# script should fail when any process returns non zero code # script should fail when any process returns non zero code
set -ev set -ev
VERSION=$(git log -1 --pretty=%h) function generate_default_app_name() {
REPO="iterativ/vbv-lernwelt-django" local branch_name=$(git rev-parse --abbrev-ref HEAD)
LATEST="${REPO}:latest" local branch_name_slug=$(echo "$branch_name" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
BUILD_TIMESTAMP=$( date '+%F_%H:%M:%S' )
VERSION_TAG="${REPO}:$VERSION"
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) local default_app_name="vbv-$branch_name_slug"
BRANCH_NAME_SLUG=$(echo "$BRANCH_NAME" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
echo "$BRANCH_NAME_SLUG"
DEFAULT_APP_NAME="vbv-$BRANCH_NAME_SLUG" # shorten default_app_name to 32 characters
if ((${#default_app_name} > 32)); then
local hash="$(echo -n "$default_app_name" | md5sum | cut -c1-4)"
default_app_name=$(echo -n "${default_app_name:0:28}-${hash}")
fi
# take app name as first argument or use default generated app name # remove multiple dashes and make sure it does not end with a dash
APP_NAME=${1:-$DEFAULT_APP_NAME} default_app_name=$(echo "$default_app_name" | sed -E 's/-+/-/g')
default_app_name=$(echo "$default_app_name" | sed 's/-\+$//')
# shorten APP_NAME to 32 characters echo "$default_app_name"
# app names with more character seem to fail in some steps in CapRover }
# CapRover generates longer names out from the app name and there
# seems to be a limit in Docker of 64 characters
if (( ${#APP_NAME} > 32 )); then
hash="$(echo -n "$APP_NAME" | md5sum | cut -c1-4)"
APP_NAME=$(echo -n "${APP_NAME:0:28}-${hash}")
fi
# remove multiple dashes and make sure it does not end with a dash # take app name as first argument or use the default generated app name
APP_NAME=$(echo "$APP_NAME" | sed -E 's/-+/-/g') APP_NAME=${1:-$(generate_default_app_name)}
APP_NAME=$(echo "$APP_NAME" | sed 's/-\+$//')
echo "Deploy to $APP_NAME" echo "Deploy to $APP_NAME"
VITE_GRAPHQL_URL="/server/graphql/"
VITE_LOGOUT_REDIRECT="/"
VITE_SENTRY_ENV="development" VITE_SENTRY_ENV="development"
if [[ "$APP_NAME" == "myvbv" ]] if [[ "$APP_NAME" == "myvbv" ]]; then
then VITE_LOGOUT_REDIRECT="https://edumgr.b2clogin.com/edumgr.onmicrosoft.com/b2c_1_signupandsignin/oauth2/v2.0/logout/?post_logout_redirect_uri=https://myvbv.iterativ.ch/"
VITE_LOGOUT_REDIRECT="https://edumgr.b2clogin.com/edumgr.onmicrosoft.com/b2c_1_signupandsignin/oauth2/v2.0/logout/?post_logout_redirect_uri=https://myvbv.iterativ.ch/" VITE_SENTRY_ENV="production"
VITE_SENTRY_ENV="production" elif [[ "$APP_NAME" == "myvbv-stage" ]]; then
elif [[ "$APP_NAME" == "myvbv-stage" ]] VITE_LOGOUT_REDIRECT="https://vbvtst.b2clogin.com/vbvtst.onmicrosoft.com/b2c_1_signupandsignin/oauth2/v2.0/logout/?post_logout_redirect_uri=https://myvbv-stage.iterativ.ch/"
then VITE_SENTRY_ENV="stage"
VITE_LOGOUT_REDIRECT="https://vbvtst.b2clogin.com/vbvtst.onmicrosoft.com/b2c_1_signupandsignin/oauth2/v2.0/logout/?post_logout_redirect_uri=https://myvbv-stage.iterativ.ch/" elif [[ "$APP_NAME" == "vbv-lernwelt" ]]; then
VITE_SENTRY_ENV="stage" VITE_LOGOUT_REDIRECT="https://vbvdev.b2clogin.com/vbvdev.onmicrosoft.com/b2c_1_signupandsignin/oauth2/v2.0/logout/?post_logout_redirect_uri=https://vbv-lernwelt.control.iterativ.ch"
elif [[ "$APP_NAME" == "vbv-lernwelt" ]] VITE_SENTRY_ENV="development"
then
VITE_LOGOUT_REDIRECT="https://vbvdev.b2clogin.com/vbvdev.onmicrosoft.com/b2c_1_signupandsignin/oauth2/v2.0/logout/?post_logout_redirect_uri=https://vbv-lernwelt.control.iterativ.ch"
VITE_SENTRY_ENV="development"
fi fi
# create client for django # create client for django
@ -56,6 +45,11 @@ npm run build
python server/manage.py collectstatic --no-input python server/manage.py collectstatic --no-input
# create and push new docker container # create and push new docker container
VERSION=$(git log -1 --pretty=%h)
REPO="iterativ/vbv-lernwelt-django"
LATEST="${REPO}:latest"
BUILD_TIMESTAMP=$(date '+%F_%H:%M:%S')
VERSION_TAG="${REPO}:$VERSION"
docker build --platform=linux/amd64 -f compose/django/Dockerfile -t "$REPO" -t "$LATEST" -t "$VERSION_TAG" --build-arg VERSION="$VERSION" --build-arg BUILD_TIMESTAMP="$BUILD_TIMESTAMP" --build-arg GIT_COMMIT="$(git log -1 --format=%h)" . docker build --platform=linux/amd64 -f compose/django/Dockerfile -t "$REPO" -t "$LATEST" -t "$VERSION_TAG" --build-arg VERSION="$VERSION" --build-arg BUILD_TIMESTAMP="$BUILD_TIMESTAMP" --build-arg GIT_COMMIT="$(git log -1 --format=%h)" .
docker push "$VERSION_TAG" docker push "$VERSION_TAG"
@ -64,16 +58,16 @@ APP_URL="$APP_NAME.control.iterativ.ch"
echo "Checking if $APP_URL is available..." echo "Checking if $APP_URL is available..."
if ! curl --output /dev/null --silent --head --fail "$APP_URL"; then if ! curl --output /dev/null --silent --head --fail "$APP_URL"; then
echo "HTTP request to $APP_URL did not return a 200 status code, so we need to create the caprover app" echo "HTTP request to $APP_URL did not return a 200 status code, so we need to create the caprover app"
python caprover_cleanup.py -a "$APP_NAME*" --automated python caprover_cleanup.py -a "$APP_NAME*" --automated
python caprover_create_app.py -a "$APP_NAME" python caprover_create_app.py -a "$APP_NAME"
fi fi
# deploy to caprover, explicitly use the version tag... so if there is a mismatch you get an error message # deploy to caprover, explicitly use the version tag... so if there is a mismatch you get an error message
caprover deploy -h https://captain.control.iterativ.ch -a "$APP_NAME" -i docker.io/"$VERSION_TAG" caprover deploy -h https://captain.control.iterativ.ch -a "$APP_NAME" -i docker.io/"$VERSION_TAG"
if [ -n "$CI" ]; then if [ -n "$CI" ]; then
echo "Running within Bitbucket Pipelines" echo "Running within Bitbucket Pipelines"
export CAPROVER_APP_URL="$APP_URL" export CAPROVER_APP_URL="$APP_URL"
echo "https://$CAPROVER_APP_URL" > /tmp/caprover_app_url.txt echo "https://$CAPROVER_APP_URL" >/tmp/caprover_app_url.txt
fi fi