-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_image.sh
More file actions
101 lines (83 loc) · 3.16 KB
/
push_image.sh
File metadata and controls
101 lines (83 loc) · 3.16 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
#!/bin/bash
# Configuration
ACCOUNT_ID="509399609859"
AWS_REGION="us-east-1"
FUNCTION_NAME="FrameComparisonModel"
IMAGE_NAME="frame-comparison-model"
ECR_REPO="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}"
ROLE="OrianeCollector"
# Colors for output (only if terminal supports it)
if [ -t 1 ]; then
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
else
GREEN=''
YELLOW=''
NC=''
fi
# Cleanup function
cleanup() {
echo -e "${YELLOW}Cleaning up temporary files...${NC}"
rm -f "${IMAGE_NAME}.tar"
echo -e "${GREEN}Cleanup complete${NC}"
}
echo -e "${YELLOW}Starting deployment process...${NC}"
# Create ECR repository if it doesn't exist
if ! aws ecr describe-repositories --repository-names ${IMAGE_NAME} 2>/dev/null; then
echo -e "${YELLOW}Creating ECR repository...${NC}"
aws ecr create-repository --repository-name ${IMAGE_NAME}
fi
# Authenticate Docker to ECR
echo -e "${YELLOW}Authenticating with ECR...${NC}"
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
# Force Docker to build in the right AWS ECR format
export DOCKER_BUILDKIT=0
export DOCKER_CLI_EXPERIMENTAL=enabled
# Build Docker image with the specified platform
echo -e "${YELLOW}Building Docker image...${NC}"
if ! docker build --platform linux/amd64 --tag ${IMAGE_NAME} .; then
echo "Error: Docker build failed"
fi
# Save Docker image to tar file
echo -e "${YELLOW}Saving Docker image to tar file...${NC}"
if ! docker save -o ${IMAGE_NAME}.tar ${IMAGE_NAME}:latest; then
echo "Error: Failed to save Docker image"
fi
# Remove local Docker image to ensure a clean load
echo -e "${YELLOW}Removing local Docker image...${NC}"
docker rmi ${IMAGE_NAME}:latest || true
# Load Docker image from tar file
echo -e "${YELLOW}Loading Docker image from tar file...${NC}"
if ! docker load -i ${IMAGE_NAME}.tar; then
echo "Error: Failed to load Docker image"
fi
# Tag and push image to ECR
echo -e "${YELLOW}Tagging and pushing image to ECR...${NC}"
echo -e "${YELLOW}Source image: ${IMAGE_NAME}:latest${NC}"
echo -e "${YELLOW}Target image: ${ECR_REPO}:latest${NC}"
# Ensure the source image exists
if ! docker image inspect ${IMAGE_NAME}:latest >/dev/null 2>&1; then
echo "Error: Source image ${IMAGE_NAME}:latest does not exist"
fi
# Remove any existing tags for the target image
docker rmi "${ECR_REPO}:latest" 2>/dev/null || true
# Tag the image with proper validation
if ! docker tag "${IMAGE_NAME}:latest" "${ECR_REPO}:latest"; then
echo "Error: Failed to tag image"
fi
# Push with timeout and plain progress output
echo -e "${YELLOW}Pushing image to ECR (this may take a few minutes)...${NC}"
if ! timeout 3000 docker push "${ECR_REPO}:latest" 2>&1 | tee /logs/docker_push.log; then
if [ $? -eq 124 ]; then
echo "Error: Docker push timed out after 50 minutes"
else
echo "Error: Failed to push image to ECR"
echo "Last 10 lines of push log:"
tail -n 10 /logs/docker_push.log
docker push "${ECR_REPO}:latest"
fi
fi
# Clean up temporary files
cleanup
echo -e "${GREEN}Image deployment completed successfully!${NC}"