diff --git a/docker-compose.yml b/docker-compose.yml index 8ba0a60..adcc20d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ version: '3' # Specify a Docker Compose file version services: site: # Name of your service build: . # Path to your Dockerfile (assumes it's in the same directory) + container_name: site restart: unless-stopped ports: - "3004:8080" # Map external port 3000 to internal port 8080 diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100644 index 0000000..e75d43e --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +echo "$(date --utc +%FT%TZ): Fetching remote repository..." +git fetch + +UPSTREAM=${1:-'@{u}'} +LOCAL=$(git rev-parse @) +REMOTE=$(git rev-parse "$UPSTREAM") +BASE=$(git merge-base @ "$UPSTREAM") + +if [ $LOCAL = $REMOTE]; then + echo "$(date --utc +%FT%TZ): No change detected in git" +elif [ $LOCAL = $BASE ]; then + BUILD_VERSION=$(git rev-parse HEAD) + echo "$(date --utc +%FT%TZ): Changes detected, deploying new version: $BUILD_VERSION" + ./scripts/deploy.sh +elif [$REMOTE = $BASE]; then + echo "$(date --utc +%FT%TZ): Local changes detected, stashing" + git stash + ./scripts/deploy.sh +else + echo "$(date --utc +%FT%TZ): Git is diverged, this is unexpected." +fi diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..56bb36b --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +git pull + +BUILD_VERSION=$(git rev-parse HEAD) + +echo "$(date --utc +%FT%TZ): Releasing new server version. $BUILD_VERSION" + +echo "$(date --utc +%FT%TZ): Running build..." +docker compose rm -f +docker compose build + +OLD_CONTAINER=$(dockerps -aqf "name=site") +echo "$(date --utc +%FT%TZ): Scaling server up..." +BUILD_VERSION=$BUILD_VERSION docker compose up -d --no-deps --scale site=2 --no-recreate site + +sleep 30 + +echo "$(date --utc +%FT%TZ): Scaling old server down..." +docker container rm -f $OLD_CONTAINER +docker compose up -d --no-deps --scale site=1 --no-recreate site