diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9676488 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,39 @@ +name: Deploy + +on: + workflow_run: + workflows: ["Run Tests"] + types: [completed] + workflow_dispatch: + +jobs: + deploy: + if: | + github.event_name == 'workflow_dispatch' || + (github.event_name == 'workflow_run' && + github.event.workflow_run.conclusion == 'success' && + (github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'github-actions')) + name: "Deploy to VPS" + runs-on: ubuntu-latest + steps: + - name: Configure SSH + run: | + mkdir -p ~/.ssh/ + echo "$SSH_PRIVATE_KEY" > ~/.ssh/deploy-key.pem + chmod 600 ~/.ssh/deploy-key.pem + cat >> ~/.ssh/config <> $tempf -done - -awk '{sum += $1} END {print sum}' $tempf -rm -f $tempf diff --git a/scripts/redeploy-site.sh b/scripts/redeploy-site.sh new file mode 100644 index 0000000..5572823 --- /dev/null +++ b/scripts/redeploy-site.sh @@ -0,0 +1,62 @@ +#!/bin/bash +# Set up a virtual environment "python3-virtualenv" before executing this script + +set -eo pipefail + +PROJECT_DIR="$HOME/graph-uf/" +URL="https://www.aleguy02.dev" +MAX_RETRIES=5 + + +echo "=== pulling in latest changes ===" + +cd $PROJECT_DIR +git fetch && git reset origin/main --hard > /dev/null + + +echo "=== spinning down containers ===" + +docker compose -f compose.yaml down > /dev/null + + +echo "=== rebuilding containers ===" + +docker compose -f compose.yaml up -d --build > /dev/null + + +echo "=== validating service ===" + +required_containers=("graphuf") + +for container in "${required_containers[@]}"; do + if ! docker ps --format '{{.Names}}' | grep -q "^${container}$"; then + echo "!! Required container '$container' is not running. !!" + exit 1 + fi +done + +# Health check with retries +retry_count=0 +while [ $retry_count -lt $MAX_RETRIES ]; do + if [ "$(curl --head $URL/health | awk '/^HTTP/{print $2}')" = "200" ]; then + echo "Health check passed" + break + fi + + retry_count=$((retry_count + 1)) + echo "Health check attempt $retry_count/$MAX_RETRIES failed" + + if [ $retry_count -lt $MAX_RETRIES ]; then + echo "Retrying in 7 seconds..." + sleep 7 + fi +done +if [ $retry_count -eq $MAX_RETRIES ]; then + echo "!! Could not reach the site at $URL/health or received a non-200 HTTP response. !!" + exit 1 +fi + + +echo "=== redeployment complete ===" + +echo "View the site at $URL"