add AWS update and deployment scripts

This commit is contained in:
Evan
2025-03-02 18:52:42 -08:00
parent bf5b3ef993
commit 89c2681c89
2 changed files with 128 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
#!/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
# 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
docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest
docker run -d -p 80:80 ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest
Executable
+107
View File
@@ -0,0 +1,107 @@
#!/bin/bash
# Script to build and upload OpenFront Docker image to ECR
# Usage: ./upload-openfront.sh [version_tag]
# Load environment variables from .env file if it exists
if [ -f .env ]; then
echo "Loading configuration from .env file..."
export $(grep -v '^#' .env | xargs)
fi
# Configuration with fallbacks
AWS_REGION=${AWS_REGION:-"eu-west-1"}
ECR_REPO_NAME=${ECR_REPO_NAME:-"openfront"}
AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID:-$(aws sts get-caller-identity --query Account --output text)}
ECR_REPO_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME"
# Default version tag is 'latest' if not provided
VERSION_TAG=${1:-"latest"}
echo "===== OpenFront Docker Image Upload Script ====="
echo "Repository: $ECR_REPO_URI"
echo "Version tag: $VERSION_TAG"
echo "================================================"
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
echo "Error: Docker is not installed. Please install Docker first."
exit 1
fi
# Check if AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "Error: AWS CLI is not installed. Please install AWS CLI first."
exit 1
fi
# Check if we're in the correct directory
if [ ! -f "Dockerfile" ]; then
echo "Error: Dockerfile not found in current directory."
echo "Please run this script from the directory containing your Dockerfile."
exit 1
fi
# Ensure the ECR repository exists
echo "Ensuring ECR repository exists..."
aws ecr describe-repositories --repository-names $ECR_REPO_NAME --region $AWS_REGION &> /dev/null
if [ $? -ne 0 ]; then
echo "Creating ECR repository $ECR_REPO_NAME..."
aws ecr create-repository --repository-name $ECR_REPO_NAME --region $AWS_REGION
if [ $? -ne 0 ]; then
echo "Error: Failed to create ECR repository."
exit 1
fi
fi
# Build the Docker image
echo "Building Docker image..."
docker buildx build --platform linux/amd64 -t $ECR_REPO_NAME:$VERSION_TAG .
if [ $? -ne 0 ]; then
echo "Error: Docker build failed."
exit 1
fi
# Authenticate to ECR
echo "Authenticating to ECR..."
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO_URI
if [ $? -ne 0 ]; then
echo "Error: Failed to authenticate to ECR."
exit 1
fi
# Tag the image for ECR
echo "Tagging image for ECR..."
docker tag $ECR_REPO_NAME:$VERSION_TAG $ECR_REPO_URI:$VERSION_TAG
if [ $? -ne 0 ]; then
echo "Error: Failed to tag image."
exit 1
fi
# Push the image to ECR
echo "Pushing image to ECR..."
docker push $ECR_REPO_URI:$VERSION_TAG
if [ $? -ne 0 ]; then
echo "Error: Failed to push image to ECR."
exit 1
fi
# Also tag and push as 'latest' if we're using a specific version
if [ "$VERSION_TAG" != "latest" ]; then
echo "Also tagging as 'latest'..."
docker tag $ECR_REPO_NAME:$VERSION_TAG $ECR_REPO_URI:latest
docker push $ECR_REPO_URI:latest
fi
echo "Verifying upload..."
aws ecr describe-images --repository-name $ECR_REPO_NAME --region $AWS_REGION --query "imageDetails[?contains(imageTags, '$VERSION_TAG')]"
echo "================================================"
echo "✅ Success! Image uploaded to $ECR_REPO_URI:$VERSION_TAG"
echo "================================================"
# Print helpful deployment instructions
echo "To deploy this image to your EC2 instance, SSH into your instance and run:"
echo "docker pull $ECR_REPO_URI:$VERSION_TAG"
echo "docker stop \$(docker ps -q --filter ancestor=$ECR_REPO_URI)"
echo "docker run -d -p 80:80 $ECR_REPO_URI:$VERSION_TAG"