update deployment: add alt deployment (#223)

This commit is contained in:
evanpelle
2025-03-12 10:40:51 -07:00
committed by GitHub
parent b1035a8e77
commit 920dafc425
6 changed files with 20 additions and 197 deletions
-38
View File
@@ -1,38 +0,0 @@
steps:
# Build the Docker image
- name: "gcr.io/cloud-builders/docker"
args:
[
"build",
"-t",
"us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}",
"--build-arg",
"GAME_ENV=${_GAME_ENV}",
".",
]
# Push the image to Artifact Registry
- name: "gcr.io/cloud-builders/docker"
args:
[
"push",
"us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}",
]
# Update the GCE instance with the new container image
- name: "gcr.io/cloud-builders/gcloud"
args:
- "compute"
- "instances"
- "update-container"
- "${_INSTANCE_NAME}"
- "--container-image"
- "us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}"
- "--zone=us-central1-a"
substitutions:
_INSTANCE_NAME: "openfrontio-dev-instance"
_GAME_ENV: "preprod" # Default to preprod
TAG_NAME: "dev"
options:
substitutionOption: "ALLOW_LOOSE"
logging: CLOUD_LOGGING_ONLY
images:
- "us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}"
Regular → Executable
+8 -3
View File
@@ -21,9 +21,9 @@ if [ -f .env ]; then
fi
# Check command line argument
if [ $# -ne 1 ] || ([ "$1" != "staging" ] && [ "$1" != "prod" ]); then
echo "Error: Please specify environment (staging or prod)"
echo "Usage: $0 [staging|prod]"
if [ $# -ne 1 ] || ([ "$1" != "staging" ] && [ "$1" != "prod" ] && [ "$1" != "alt" ]); then
echo "Error: Please specify environment (staging, prod, or alt)"
echo "Usage: $0 [staging|prod|alt]"
exit 1
fi
@@ -39,6 +39,11 @@ if [ "$ENV" == "staging" ]; then
print_header "DEPLOYING TO STAGING ENVIRONMENT"
SERVER_HOST=$SERVER_HOST_STAGING
DOCKER_REPO=$DOCKER_REPO_STAGING
elif [ "$ENV" == "alt" ]; then
print_header "DEPLOYING TO ALT ENVIRONMENT"
SERVER_HOST=$SERVER_HOST_ALT
DOCKER_REPO=$DOCKER_REPO_PROD # Uses prod Docker repo for alt environment
ENV="prod"
else
print_header "DEPLOYING TO PRODUCTION ENVIRONMENT"
SERVER_HOST=$SERVER_HOST_PROD
-8
View File
@@ -1,8 +0,0 @@
services:
openfront:
build: .
ports:
- "80:80"
- "443:443"
env_file:
- .env
-31
View File
@@ -1,31 +0,0 @@
#!/bin/bash
# Executed on ec2 startup
yum update -y
amazon-linux-extras install docker -y
service docker start
systemctl enable docker
usermod -a -G docker ec2-user
# Install AWS CLI v2
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
# Install CloudWatch agent (simplified)
yum install -y amazon-cloudwatch-agent
# Start CloudWatch agent with default config (collects basic system metrics)
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
# Authenticate to ECR and run container
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com
# Pull and run container with simple CloudWatch logging
docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest
docker run -d -p 80:80 \
--log-driver=awslogs \
--log-opt awslogs-region=eu-west-1 \
--log-opt awslogs-group=/aws/ec2/docker-containers \
--log-opt awslogs-create-group=true \
${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest
-110
View File
@@ -1,110 +0,0 @@
#!/bin/bash
# deploy.sh - Complete deployment script for staging and production environments
# This script:
# 1. Builds and uploads the Docker image to ECR with appropriate tag
# 2. Copies the update script to EC2 instance (staging or prod)
# 3. Executes the update script on the EC2 instance
set -e # Exit immediately if a command exits with a non-zero status
# Function to print section headers
print_header() {
echo "======================================================"
echo "🚀 $1"
echo "======================================================"
}
# Load environment variables
if [ -f .env ]; then
echo "Loading configuration from .env file..."
export $(grep -v '^#' .env | xargs)
fi
# Check command line argument
if [ $# -ne 1 ] || ([ "$1" != "staging" ] && [ "$1" != "prod" ]); then
echo "Error: Please specify environment (staging or prod)"
echo "Usage: $0 [staging|prod]"
exit 1
fi
ENV=$1
VERSION_TAG=""
# Set environment-specific variables
if [ "$ENV" == "staging" ]; then
print_header "DEPLOYING TO STAGING ENVIRONMENT"
EC2_HOST=$EC2_HOST_STAGING
VERSION_TAG="staging"
else
print_header "DEPLOYING TO PRODUCTION ENVIRONMENT"
EC2_HOST=$EC2_HOST_PROD
VERSION_TAG="latest"
fi
# Check required environment variables
if [ -z "$EC2_HOST" ]; then
echo "Error: EC2_HOST_${ENV^^} not defined in .env file or environment"
exit 1
fi
# Configuration
EC2_KEY=${EC2_KEY:-"~/.ssh/id_rsa"} # Use default or override from .env
BUILD_SCRIPT="./upload.sh" # Path to your build script
UPDATE_SCRIPT="./update.sh" # Path to your update script
REMOTE_UPDATE_SCRIPT="/home/ec2-user/update-openfront.sh" # Where to place the script on EC2
# Check if required scripts exist
if [ ! -f "$BUILD_SCRIPT" ]; then
echo "Error: Build script $BUILD_SCRIPT not found!"
exit 1
fi
if [ ! -f "$UPDATE_SCRIPT" ]; then
echo "Error: Update script $UPDATE_SCRIPT not found!"
exit 1
fi
# Step 1: Build and upload Docker image to ECR
print_header "STEP 1: Building and uploading Docker image to ECR"
echo "Environment: ${ENV}"
echo "Using version tag: $VERSION_TAG"
# Execute the build script with the version tag
$BUILD_SCRIPT $VERSION_TAG
if [ $? -ne 0 ]; then
echo "❌ Build and upload failed. Stopping deployment."
exit 1
fi
# Step 2: Copy update script to EC2 instance
print_header "STEP 2: Copying update script to EC2 instance"
echo "Target: $EC2_HOST"
# Make sure the update script is executable
chmod +x $UPDATE_SCRIPT
# Copy the update script to the EC2 instance
scp -i $EC2_KEY $UPDATE_SCRIPT $EC2_HOST:$REMOTE_UPDATE_SCRIPT
scp -i $EC2_KEY .env $EC2_HOST:/home/ec2-user/.env
# After copying the .env file, secure it
ssh -i $EC2_KEY $EC2_HOST "chmod 600 /home/ec2-user/.env"
if [ $? -ne 0 ]; then
echo "❌ Failed to copy update script to EC2 instance. Stopping deployment."
exit 1
fi
echo "✅ Update script successfully copied to EC2 instance."
# Step 3: Execute the update script on the EC2 instance
print_header "STEP 3: Executing update script on EC2 instance"
# Make the script executable on the remote server and execute it with the environment parameter
ssh -i $EC2_KEY $EC2_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && $REMOTE_UPDATE_SCRIPT $ENV"
if [ $? -ne 0 ]; then
echo "❌ Failed to execute update script on EC2 instance."
exit 1
fi
print_header "DEPLOYMENT COMPLETED SUCCESSFULLY"
echo "✅ New version deployed to ${ENV} environment!"
echo "🌐 Check your ${ENV} server to verify the deployment."
echo "======================================================"
+12 -7
View File
@@ -1,15 +1,20 @@
#!/bin/bash
# Script to update Docker container
# update.sh - Script to update Docker container on Hetzner server
# Called by deploy.sh after uploading Docker image to Docker Hub
# Check if environment parameter is provided
if [ -z "$1" ]; then
echo "Error: Environment parameter is required (prod or staging)"
echo "Usage: $0 <environment>"
if [ $# -lt 3 ]; then
echo "Error: Required parameters missing"
echo "Usage: $0 <environment> <docker_username> <docker_repo>"
exit 1
fi
# Set environment from parameter
# Set parameters
ENV=$1
DOCKER_USERNAME=$2
DOCKER_REPO=$3
# Container and image configuration
CONTAINER_NAME="openfront-${ENV}"
IMAGE_NAME="${DOCKER_USERNAME}/${DOCKER_REPO}"
FULL_IMAGE_NAME="${IMAGE_NAME}:latest"
@@ -70,7 +75,6 @@ echo "Starting new container for ${ENV} environment..."
docker run -d -p 80:80 \
--restart=always \
$VOLUME_MOUNTS \
$NETWORK_FLAGS \
--env GAME_ENV=${ENV} \
--env-file /root/.env \
--name ${CONTAINER_NAME} \
@@ -78,7 +82,8 @@ docker run -d -p 80:80 \
if [ $? -eq 0 ]; then
echo "Update complete! New ${ENV} container is running."
# Final cleanup after successful deployment
# Final cleanup after successful deployment
echo "Performing final cleanup of unused Docker resources..."
echo "Removing unused images (not tagged and not referenced)..."
docker image prune -f