vbv/caprover_deploy.sh

72 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# script should fail when any process returns non zero code
set -ev
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"
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
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"
APP_NAME=${1:-$DEFAULT_APP_NAME}
# shorten APP_NAME to 32 characters
# 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
echo "Deploy to $APP_NAME"
VITE_GRAPHQL_URL="/server/graphql/"
VITE_LOGOUT_REDIRECT="/"
VITE_SENTRY_ENV="development"
if [[ "$APP_NAME" == "myvbv" ]]
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_SENTRY_ENV="production"
elif [[ "$APP_NAME" == "myvbv-stage" ]]
then
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/"
VITE_SENTRY_ENV="stage"
elif [[ "$APP_NAME" == "vbv-lernwelt" ]]
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
# create client for django
npm run build
python server/manage.py collectstatic --no-input
# create and push new docker container
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"
APP_URL="$APP_NAME.control.iterativ.ch"
echo "Checking if $APP_URL is available..."
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"
python caprover_cleanup.py -a "$APP_NAME*" --automated
python caprover_create_app.py -a "$APP_NAME"
fi
# 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"
if [ -n "$CI" ]; then
echo "Running within Bitbucket Pipelines"
export CAPROVER_APP_URL="$APP_URL"
echo "https://$CAPROVER_APP_URL" > /tmp/caprover_app_url.txt
fi