Refactor caprover deploy script
This commit is contained in:
parent
428ba37bb3
commit
956a685ac9
|
|
@ -3,52 +3,41 @@
|
|||
# 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"
|
||||
function generate_default_app_name() {
|
||||
local branch_name=$(git rev-parse --abbrev-ref HEAD)
|
||||
local branch_name_slug=$(echo "$branch_name" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
|
||||
|
||||
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"
|
||||
local default_app_name="vbv-$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
|
||||
APP_NAME=${1:-$DEFAULT_APP_NAME}
|
||||
# remove multiple dashes and make sure it does not end with a dash
|
||||
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
|
||||
# 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 "$default_app_name"
|
||||
}
|
||||
|
||||
# remove multiple dashes and make sure it does not end with a dash
|
||||
APP_NAME=$(echo "$APP_NAME" | sed -E 's/-+/-/g')
|
||||
APP_NAME=$(echo "$APP_NAME" | sed 's/-\+$//')
|
||||
# take app name as first argument or use the default generated app name
|
||||
APP_NAME=${1:-$(generate_default_app_name)}
|
||||
|
||||
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"
|
||||
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
|
||||
|
|
@ -56,6 +45,11 @@ npm run build
|
|||
python server/manage.py collectstatic --no-input
|
||||
|
||||
# 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 push "$VERSION_TAG"
|
||||
|
||||
|
|
@ -64,16 +58,16 @@ 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"
|
||||
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
|
||||
echo "Running within Bitbucket Pipelines"
|
||||
export CAPROVER_APP_URL="$APP_URL"
|
||||
echo "https://$CAPROVER_APP_URL" >/tmp/caprover_app_url.txt
|
||||
fi
|
||||
|
|
|
|||
Loading…
Reference in New Issue