Skip to content

Commit 66e5408

Browse files
authored
Merge branch 'main' into max-timer
2 parents eb64a3d + 77dddbf commit 66e5408

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1555
-283
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ jobs:
127127
VERSION_TAG: latest
128128
run: |
129129
echo "::group::deploy.sh"
130-
./deploy.sh "$ENV" "$HOST" "$SUBDOMAIN"
130+
./build-deploy.sh "$ENV" "$HOST" "$SUBDOMAIN"
131131
echo "Deployment created in ${SECONDS} seconds" >> $GITHUB_STEP_SUMMARY
132132
echo "::endgroup::"
133133
- name: ⏳ Wait for deployment to start

build-deploy.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
# build-deploy.sh - Wrapper script that runs build.sh and deploy.sh in sequence
3+
# This script maintains backward compatibility with the original build-deploy.sh
4+
5+
set -e # Exit immediately if a command exits with a non-zero status
6+
7+
# Function to print section headers
8+
print_header() {
9+
echo "======================================================"
10+
echo "🚀 $1"
11+
echo "======================================================"
12+
}
13+
14+
print_header "BUILD AND DEPLOY WRAPPER"
15+
echo "This script will run build.sh and deploy.sh in sequence."
16+
echo "You can also run them separately:"
17+
echo " ./build.sh [prod|staging] [version_tag]"
18+
echo " ./deploy.sh [prod|staging] [eu|nbg1|staging|masters] [version_tag] [subdomain] [--enable_basic_auth]"
19+
echo ""
20+
21+
# Check command line arguments
22+
if [ $# -lt 3 ] || [ $# -gt 5 ]; then
23+
echo "Error: Please specify environment, host, and subdomain"
24+
echo "Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
25+
exit 1
26+
fi
27+
28+
# Validate first argument (environment)
29+
if [ "$1" != "prod" ] && [ "$1" != "staging" ]; then
30+
echo "Error: First argument must be either 'prod' or 'staging'"
31+
echo "Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
32+
exit 1
33+
fi
34+
35+
# Validate second argument (host)
36+
if [ "$2" != "eu" ] && [ "$2" != "nbg1" ] && [ "$2" != "staging" ] && [ "$2" != "masters" ]; then
37+
echo "Error: Second argument must be either 'eu', 'nbg1', 'staging', or 'masters'"
38+
echo "Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
39+
exit 1
40+
fi
41+
42+
# Validate third argument (subdomain)
43+
if [ -z "$3" ]; then
44+
echo "Error: Subdomain is required"
45+
echo "Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
46+
exit 1
47+
fi
48+
49+
# Generate version tag
50+
VERSION_TAG=$(date +"%Y%m%d-%H%M%S")
51+
echo "Generated version tag: $VERSION_TAG"
52+
53+
# Extract arguments
54+
ENV="$1"
55+
HOST="$2"
56+
SUBDOMAIN="$3"
57+
ENABLE_BASIC_AUTH=""
58+
59+
# Parse remaining arguments
60+
shift 3
61+
while [[ $# -gt 0 ]]; do
62+
case $1 in
63+
--enable_basic_auth)
64+
ENABLE_BASIC_AUTH="--enable_basic_auth"
65+
shift
66+
;;
67+
*)
68+
echo "Error: Unknown argument: $1"
69+
echo "Usage: $0 [prod|staging] [eu|nbg1|staging|masters] [subdomain] [--enable_basic_auth]"
70+
exit 1
71+
;;
72+
esac
73+
done
74+
75+
# Step 1: Run build.sh
76+
echo "Step 1: Running build.sh..."
77+
./build.sh "$ENV" "$VERSION_TAG"
78+
79+
if [ $? -ne 0 ]; then
80+
echo "❌ Build failed. Stopping deployment."
81+
exit 1
82+
fi
83+
84+
echo ""
85+
echo "Step 2: Running deploy.sh"
86+
./deploy.sh "$ENV" "$HOST" "$VERSION_TAG" "$SUBDOMAIN"
87+
88+
if [ $? -ne 0 ]; then
89+
echo "❌ Deploy failed."
90+
exit 1
91+
fi
92+
93+
print_header "BUILD AND DEPLOY COMPLETED SUCCESSFULLY"
94+
echo "✅ Both build and deploy operations completed successfully!"
95+
echo "Version tag used: $VERSION_TAG"
96+
echo "======================================================="

build.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
# build.sh - Build and upload Docker image to Docker Hub
3+
# This script:
4+
# 1. Builds and uploads the Docker image to Docker Hub with appropriate tag
5+
# 2. Optionally saves container metadata to a file (if METADATA_FILE is provided as 3rd argument)
6+
7+
set -e # Exit immediately if a command exits with a non-zero status
8+
9+
# Parse command line arguments
10+
DEPLOY_ENV="$1"
11+
VERSION_TAG="$2"
12+
METADATA_FILE="$3"
13+
14+
# Set default metadata file if not provided
15+
if [ -z "$METADATA_FILE" ]; then
16+
METADATA_FILE="/tmp/build-metadata-$RANDOM.json"
17+
fi
18+
19+
# Check required arguments
20+
if [ -z "$DEPLOY_ENV" ] || [ -z "$VERSION_TAG" ]; then
21+
echo "Error: Please specify environment and version tag"
22+
echo "Usage: $0 [prod|staging] [version_tag] [metadata_file]"
23+
echo "Note: Provide metadata_file as third argument to save container metadata to a file"
24+
exit 1
25+
fi
26+
27+
# Validate environment argument
28+
if [ "$DEPLOY_ENV" != "prod" ] && [ "$DEPLOY_ENV" != "staging" ]; then
29+
echo "Error: First argument must be either 'prod' or 'staging'"
30+
echo "Usage: $0 [prod|staging] [version_tag] [metadata_file]"
31+
echo "Note: Provide metadata_file as third argument to save container metadata to a file"
32+
exit 1
33+
fi
34+
35+
print_header() {
36+
echo "======================================================"
37+
echo "🚀 ${1}"
38+
echo "======================================================"
39+
}
40+
41+
# Load common environment variables first
42+
if [ -f .env ]; then
43+
echo "Loading common configuration from .env file..."
44+
set -o allexport
45+
source .env
46+
set +o allexport
47+
fi
48+
49+
# Load environment-specific variables
50+
if [ -f .env.$DEPLOY_ENV ]; then
51+
echo "Loading $DEPLOY_ENV-specific configuration from .env.$DEPLOY_ENV file..."
52+
set -o allexport
53+
source .env.$DEPLOY_ENV
54+
set +o allexport
55+
fi
56+
57+
# Check required environment variables for build
58+
if [ -z "$DOCKER_USERNAME" ] || [ -z "$DOCKER_REPO" ]; then
59+
echo "Error: DOCKER_USERNAME or DOCKER_REPO not defined in .env file or environment"
60+
exit 1
61+
fi
62+
63+
DOCKER_IMAGE="${DOCKER_USERNAME}/${DOCKER_REPO}:${VERSION_TAG}"
64+
65+
# Build and upload Docker image to Docker Hub
66+
echo "Environment: ${DEPLOY_ENV}"
67+
echo "Using version tag: $VERSION_TAG"
68+
echo "Docker repository: $DOCKER_REPO"
69+
echo "Metadata file: $METADATA_FILE"
70+
71+
# Get Git commit for build info
72+
GIT_COMMIT=$(git rev-parse HEAD 2> /dev/null || echo "unknown")
73+
echo "Git commit: $GIT_COMMIT"
74+
75+
docker buildx build \
76+
--platform linux/amd64 \
77+
--build-arg GIT_COMMIT=$GIT_COMMIT \
78+
--metadata-file $METADATA_FILE \
79+
-t $DOCKER_IMAGE \
80+
--push \
81+
.
82+
83+
if [ $? -ne 0 ]; then
84+
echo "❌ Docker build failed."
85+
exit 1
86+
fi
87+
88+
echo "✅ Docker image built and pushed successfully."
89+
echo "Image: $DOCKER_IMAGE"
90+
91+
print_header "BUILD COMPLETED SUCCESSFULLY ${DOCKER_IMAGE}"

deploy.sh

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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

87
set -e # Exit immediately if a command exits with a non-zero status
98

@@ -28,45 +27,41 @@ done
2827
# Restore positional parameters
2928
set -- "${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
3642
fi
3743

3844
# Validate first argument (environment)
3945
if [ "$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
4349
fi
4450

4551
# Validate second argument (host)
4652
if [ "$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
5056
fi
5157

52-
# Function to print section headers
53-
print_header() {
54-
echo "======================================================"
55-
echo "🚀 $1"
56-
echo "======================================================"
57-
}
58-
5958
ENV=$1
6059
HOST=$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
7267
if [ -f .env ]; then
@@ -80,6 +75,14 @@ if [ -f .env.$ENV ]; then
8075
export $(grep -v '^#' .env.$ENV | xargs)
8176
fi
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+
8386
if [ "$HOST" == "staging" ]; then
8487
print_header "DEPLOYING TO STAGING HOST"
8588
SERVER_HOST=$SERVER_HOST_STAGING
@@ -121,43 +124,22 @@ REMOTE_USER="openfront"
121124
REMOTE_UPDATE_PATH="/home/$REMOTE_USER"
122125
REMOTE_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
128128
if [ ! -f "$UPDATE_SCRIPT" ]; then
129129
echo "Error: Update script $UPDATE_SCRIPT not found!"
130130
exit 1
131131
fi
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"
135135
echo "Environment: ${ENV}"
136136
echo "Host: ${HOST}"
137137
echo "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"
161143
echo "Target: $REMOTE_USER@$SERVER_HOST"
162144

163145
# Make sure the update script is executable
@@ -175,6 +157,8 @@ fi
175157
# when multiple deployments are happening at the same time.
176158
ENV_FILE="${REMOTE_UPDATE_PATH}/${SUBDOMAIN}-${RANDOM}.env"
177159

160+
print_header "EXECUTING UPDATE SCRIPT ON SERVER"
161+
178162
ssh -i $SSH_KEY $REMOTE_USER@$SERVER_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && \
179163
cat > $ENV_FILE << 'EOL'
180164
GAME_ENV=$ENV

0 commit comments

Comments
 (0)