11#! /bin/bash
2- # deploy.sh - Complete deployment script for Hetzner with Docker Hub and R2
2+ # deploy.sh - Deploy application to Hetzner server
33# This script:
4- # 1. Builds and uploads the Docker image to Docker Hub with appropriate tag
5- # 2. Copies the update script to Hetzner server
6- # 3. Executes the update script on the Hetzner server
4+ # 1. Copies the update script to Hetzner server
5+ # 2. Executes the update script on the Hetzner server
76
87set -e # Exit immediately if a command exits with a non-zero status
98
2827# Restore positional parameters
2928set -- " ${POSITIONAL_ARGS[@]} "
3029
30+ # Function to print section headers
31+ print_header () {
32+ echo " ======================================================"
33+ echo " 🚀 $1 "
34+ echo " ======================================================"
35+ }
36+
3137# Check command line arguments
32- if [ $# -lt 2 ] || [ $# -gt 3 ]; then
33- echo " Error: Please specify environment and host, with optional subdomain"
34- echo " Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
38+ if [ $# -ne 4 ]; then
39+ echo " Error: Please specify environment, host, version tag, and subdomain"
40+ echo " Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [version_tag] [ subdomain] [--enable_basic_auth]"
3541 exit 1
3642fi
3743
3844# Validate first argument (environment)
3945if [ " $1 " != " prod" ] && [ " $1 " != " staging" ]; then
4046 echo " Error: First argument must be either 'prod' or 'staging'"
41- echo " Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
47+ echo " Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [version_tag] [ subdomain] [--enable_basic_auth]"
4248 exit 1
4349fi
4450
4551# Validate second argument (host)
4652if [ " $2 " != " eu" ] && [ " $2 " != " nbg1" ] && [ " $2 " != " staging" ] && [ " $2 " != " masters" ]; then
4753 echo " Error: Second argument must be either 'eu', 'nbg1', 'staging', or 'masters'"
48- echo " Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
54+ echo " Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [version_tag] [ subdomain] [--enable_basic_auth]"
4955 exit 1
5056fi
5157
52- # Function to print section headers
53- print_header () {
54- echo " ======================================================"
55- echo " 🚀 $1 "
56- echo " ======================================================"
57- }
58-
5958ENV=$1
6059HOST=$2
61- SUBDOMAIN=$3 # Optional third argument for custom subdomain
60+ VERSION_TAG=$3
61+ SUBDOMAIN=$4
6262
63- # Set subdomain - use the custom subdomain if provided, otherwise use REGION
64- if [ -n " $SUBDOMAIN " ]; then
65- echo " Using custom subdomain: $SUBDOMAIN "
66- else
67- SUBDOMAIN=$HOST
68- echo " Using host as subdomain: $SUBDOMAIN "
69- fi
63+ # Set subdomain - use the provided subdomain
64+ echo " Using subdomain: $SUBDOMAIN "
7065
7166# Load common environment variables first
7267if [ -f .env ]; then
@@ -80,6 +75,14 @@ if [ -f .env.$ENV ]; then
8075 export $( grep -v ' ^#' .env.$ENV | xargs)
8176fi
8277
78+ # Check required environment variables for deployment
79+ if [ -z " $DOCKER_USERNAME " ] || [ -z " $DOCKER_REPO " ]; then
80+ echo " Error: DOCKER_USERNAME or DOCKER_REPO not defined in .env file or environment"
81+ exit 1
82+ fi
83+
84+ DOCKER_IMAGE=" ${DOCKER_USERNAME} /${DOCKER_REPO} :${VERSION_TAG} "
85+
8386if [ " $HOST " == " staging" ]; then
8487 print_header " DEPLOYING TO STAGING HOST"
8588 SERVER_HOST=$SERVER_HOST_STAGING
@@ -121,43 +124,22 @@ REMOTE_USER="openfront"
121124REMOTE_UPDATE_PATH=" /home/$REMOTE_USER "
122125REMOTE_UPDATE_SCRIPT=" $REMOTE_UPDATE_PATH /update-openfront.sh" # Where to place the script on server
123126
124- VERSION_TAG=$( date +" %Y%m%d-%H%M%S" )
125- DOCKER_IMAGE=" ${DOCKER_USERNAME} /${DOCKER_REPO} :${VERSION_TAG} "
126-
127127# Check if update script exists
128128if [ ! -f " $UPDATE_SCRIPT " ]; then
129129 echo " Error: Update script $UPDATE_SCRIPT not found!"
130130 exit 1
131131fi
132132
133- # Step 1: Build and upload Docker image to Docker Hub
134- print_header " STEP 1: Building and uploading Docker image to Docker Hub "
133+ # Display deployment information
134+ print_header " DEPLOYMENT INFORMATION "
135135echo " Environment: ${ENV} "
136136echo " Host: ${HOST} "
137137echo " Subdomain: ${SUBDOMAIN} "
138- echo " Using version tag: $VERSION_TAG "
139- echo " Docker repository: $DOCKER_REPO "
140-
141- # Get Git commit for build info
142- GIT_COMMIT=$( git rev-parse HEAD 2> /dev/null || echo " unknown" )
143- echo " Git commit: $GIT_COMMIT "
138+ echo " Docker Image: $DOCKER_IMAGE "
139+ echo " Target Server: $SERVER_HOST "
144140
145- docker buildx build \
146- --platform linux/amd64 \
147- --build-arg GIT_COMMIT=$GIT_COMMIT \
148- -t $DOCKER_IMAGE \
149- --push \
150- .
151-
152- if [ $? -ne 0 ]; then
153- echo " ❌ Docker build failed. Stopping deployment."
154- exit 1
155- fi
156-
157- echo " ✅ Docker image built and pushed successfully."
158-
159- # Step 2: Copy update script to Hetzner server
160- print_header " STEP 2: Copying update script to server"
141+ # Copy update script to Hetzner server
142+ print_header " COPYING UPDATE SCRIPT TO SERVER"
161143echo " Target: $REMOTE_USER @$SERVER_HOST "
162144
163145# Make sure the update script is executable
175157# when multiple deployments are happening at the same time.
176158ENV_FILE=" ${REMOTE_UPDATE_PATH} /${SUBDOMAIN} -${RANDOM} .env"
177159
160+ print_header " EXECUTING UPDATE SCRIPT ON SERVER"
161+
178162ssh -i $SSH_KEY $REMOTE_USER @$SERVER_HOST " chmod +x $REMOTE_UPDATE_SCRIPT && \
179163cat > $ENV_FILE << 'EOL'
180164GAME_ENV=$ENV
0 commit comments