1+ # docker/build_push_to_ecr.sh
2+
3+ #! /bin/bash
4+
5+ # Set variables
6+
7+ dirname=$( dirname " $0 " )
8+ DOCKERFILE_DIR=$( realpath " $dirname " )
9+ echo " DOCKERFILE_DIR: $DOCKERFILE_DIR "
10+
11+ AWS_REGION=" eu-west-2"
12+ ACCOUNT_ID=$( aws sts get-caller-identity --query Account --output text)
13+ REPOSITORY_NAME=" imms-fhir-api-grafana"
14+ IMAGE_TAG=" 11.0.0-22.04_stable"
15+ TAGS=" Key=Environment,Value=non-prod Key=Project,Value=immunisation-fhir-api-grafana"
16+ LIFECYCLE_POLICY_FILE=" lifecycle-policy.json"
17+
18+ # Change to the directory containing the Dockerfile
19+ cd $DOCKERFILE_DIR
20+
21+ # Check if Dockerfile exists
22+ if [ ! -f Dockerfile ]; then
23+ echo " Dockerfile not found in the current directory."
24+ exit 1
25+ fi
26+
27+ # Create ECR repository if it does not exist
28+ aws ecr describe-repositories --repository-names $REPOSITORY_NAME --region $AWS_REGION > /dev/null 2>&1
29+
30+ if [ $? -ne 0 ]; then
31+ echo " Creating ECR repository: $REPOSITORY_NAME "
32+ aws ecr create-repository --repository-name $REPOSITORY_NAME --region $AWS_REGION
33+ # Add tags to the repository
34+ aws ecr tag-resource --resource-arn arn:aws:ecr:$AWS_REGION :$ACCOUNT_ID :repository/$REPOSITORY_NAME --tags $TAGS
35+ fi
36+
37+ # Apply lifecycle policy to the ECR repository
38+ aws ecr put-lifecycle-policy --repository-name $REPOSITORY_NAME --lifecycle-policy-text file://$LIFECYCLE_POLICY_FILE --region $AWS_REGION
39+
40+ printf " Building and pushing Docker image to ECR...\n"
41+ # Authenticate Docker to ECR
42+ aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com
43+
44+ printf " Building Docker image...\n"
45+ # Build Docker image
46+ # docker build -t $REPOSITORY_NAME:$IMAGE_TAG .
47+ # Build Docker image for linux/amd64 architecture
48+ # Remove existing Docker image if it exists
49+ docker rmi $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$REPOSITORY_NAME :$IMAGE_TAG --force
50+ docker pull --platform linux/amd64 grafana/grafana:latest
51+ docker buildx create --use
52+ docker buildx build --platform linux/amd64 -t $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$REPOSITORY_NAME :$IMAGE_TAG --push .
53+
54+ # Check if the build was successful
55+ if [ $? -ne 0 ]; then
56+ echo " Docker build failed."
57+ exit 1
58+ fi
59+
60+ printf " Tagging Docker image...\n"
61+ # Tag Docker image
62+ docker tag $REPOSITORY_NAME :$IMAGE_TAG $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$REPOSITORY_NAME :$IMAGE_TAG
63+
64+ # Check if the tag was successful
65+ if [ $? -ne 0 ]; then
66+ echo " Docker tag failed."
67+ exit 1
68+ fi
69+
70+ printf " Pushing Docker image to ECR...\n"
71+ # Push Docker image to ECR
72+ docker push $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$REPOSITORY_NAME :$IMAGE_TAG
73+
74+ # Check if the push was successful
75+ if [ $? -ne 0 ]; then
76+ echo " Docker push failed."
77+ exit 1
78+ fi
79+
80+ echo " Docker image built and pushed to ECR successfully."
0 commit comments