-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·184 lines (148 loc) · 5.05 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·184 lines (148 loc) · 5.05 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# deploy.sh - Build and deploy Docker image to AWS ECR
# Usage: ./deploy.sh [image-tag]
set -e # Exit on error
# Configuration - Modify these variables as needed
AWS_REGION="${AWS_REGION:-us-east-1}"
ECR_REPOSITORY="${ECR_REPOSITORY:-rag-pinecone-server}"
IMAGE_TAG="${1:-latest}"
AWS_ACCOUNT_ID="${AWS_ACCOUNT_ID:-$(aws sts get-caller-identity --query Account --output text)}"
# Construct the full ECR repository URI
ECR_REGISTRY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
ECR_URI="${ECR_REGISTRY}/${ECR_REPOSITORY}"
# Docker variables
DOCKER_PLATFORM="linux/amd64"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Helper functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check prerequisites
check_prerequisites() {
log_info "Checking prerequisites..."
if ! command -v aws &> /dev/null; then
log_error "AWS CLI is not installed. Please install it first."
exit 1
fi
if ! command -v docker &> /dev/null; then
log_error "Docker is not installed. Please install it first."
exit 1
fi
if ! aws sts get-caller-identity &> /dev/null; then
log_error "AWS credentials not configured. Run 'aws configure' first."
exit 1
fi
log_info "Prerequisites check passed"
}
# Get or create ECR repository
setup_ecr_repository() {
log_info "Setting up ECR repository: $ECR_REPOSITORY"
# Check if repository exists
if ! aws ecr describe-repositories --repository-names "$ECR_REPOSITORY" --region "$AWS_REGION" &> /dev/null; then
log_warn "Repository $ECR_REPOSITORY does not exist. Creating..."
aws ecr create-repository \
--repository-name "$ECR_REPOSITORY" \
--region "$AWS_REGION" \
--image-scanning-configuration scanOnPush=true \
--encryption-configuration encryptionType=AES256
log_info "Repository created successfully"
else
log_info "Repository already exists"
fi
}
# Authenticate Docker with ECR
ecr_login() {
log_info "Authenticating Docker with ECR..."
aws ecr get-login-password --region "$AWS_REGION" | \
docker login --username AWS --password-stdin ${ECR_REGISTRY}
log_info "Docker authentication successful"
}
# Build Docker image
build_image() {
log_info "Building Docker image..."
# Get git commit hash for additional tagging (if in git repo)
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# docker build \
# --platform $DOCKER_PLATFORM \
# --build-arg BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
# --build-arg GIT_COMMIT="$GIT_COMMIT" \
# -t "$ECR_REPOSITORY:$IMAGE_TAG" \
# -t "$ECR_REPOSITORY:$GIT_COMMIT" \
# .
docker build \
--platform $DOCKER_PLATFORM \
--build-arg BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
-t "$ECR_REPOSITORY:$IMAGE_TAG" \
.
log_info "Docker image built successfully"
}
# Tag image for ECR
tag_image() {
log_info "Tagging image for ECR..."
docker tag "$ECR_REPOSITORY:$IMAGE_TAG" "$ECR_URI:$IMAGE_TAG"
# Also tag with git commit if available
# GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "")
# if [ -n "$GIT_COMMIT" ]; then
# docker tag "$ECR_REPOSITORY:$IMAGE_TAG" "$ECR_URI:$GIT_COMMIT"
# log_info "Tagged with commit hash: $GIT_COMMIT"
# fi
log_info "Image tagged successfully"
}
# Push image to ECR
push_image() {
log_info "Pushing image to ECR..."
docker push "$ECR_URI:$IMAGE_TAG"
# Push git commit tag if available
# GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "")
# if [ -n "$GIT_COMMIT" ]; then
# docker push "$ECR_URI:$GIT_COMMIT"
# fi
log_info "Image pushed successfully"
}
# Display summary
display_summary() {
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
echo ""
log_info "==================================================================="
log_info "Deployment Summary"
log_info "==================================================================="
log_info "AWS Account: $AWS_ACCOUNT_ID"
log_info "Region: $AWS_REGION"
log_info "Repository: $ECR_REPOSITORY"
log_info "Image Tag: $IMAGE_TAG"
# log_info "Git Commit: $GIT_COMMIT"
log_info "ECR URI: $ECR_URI:$IMAGE_TAG"
log_info "==================================================================="
echo ""
log_info "To pull this image:"
echo " docker pull $ECR_URI:$IMAGE_TAG"
echo ""
log_info "To run this image:"
echo " docker run -p 8000:8000 --env-file .env $ECR_URI:$IMAGE_TAG"
echo ""
}
# Main execution
main() {
log_info "Starting deployment process..."
echo ""
check_prerequisites
setup_ecr_repository
ecr_login
build_image
tag_image
push_image
display_summary
log_info "Deployment completed successfully!"
}
# Run main function
main