forked from openfrontio/OpenFrontIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.sh
More file actions
executable file
·111 lines (92 loc) · 3.51 KB
/
Copy pathupload.sh
File metadata and controls
executable file
·111 lines (92 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/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
GIT_COMMIT=$(git rev-parse HEAD)
echo "Git commit: $GIT_COMMIT"
# Build the Docker image
echo "Building Docker image..."
docker buildx build \
--platform linux/amd64 \
--build-arg GIT_COMMIT=$GIT_COMMIT \
-t $ECR_REPO_NAME:$VERSION_TAG \
.
# 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"