1- # docker/build_push_to_ecr.sh
2-
31#! /bin/bash
42
53# Set variables
4+
5+ dirname=$( dirname " $0 " )
6+ DOCKERFILE_DIR=$( realpath " $dirname " )
7+ echo " DOCKERFILE_DIR: $DOCKERFILE_DIR "
8+
69AWS_REGION=" eu-west-2"
710ACCOUNT_ID=$( aws sts get-caller-identity --query Account --output text)
811REPOSITORY_NAME=" imms-fhir-api-grafana"
912IMAGE_TAG=" 11.0.0-22.04_stable"
10- DOCKERFILE_DIR=" /Users/watess01/Documents/NHS/code/immunisation-fhir-api/grafana/non-prod/docker"
13+ LOCAL_IMAGE_NAME=" $REPOSITORY_NAME :$IMAGE_TAG "
14+ IMAGE_NAME=" $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$LOCAL_IMAGE_NAME "
1115TAGS=" Key=Environment,Value=non-prod Key=Project,Value=immunisation-fhir-api-grafana"
16+ LIFECYCLE_POLICY_FILE=" lifecycle-policy.json"
1217
1318# Change to the directory containing the Dockerfile
1419cd $DOCKERFILE_DIR
@@ -29,42 +34,31 @@ if [ $? -ne 0 ]; then
2934 aws ecr tag-resource --resource-arn arn:aws:ecr:$AWS_REGION :$ACCOUNT_ID :repository/$REPOSITORY_NAME --tags $TAGS
3035fi
3136
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+
3240printf " Building and pushing Docker image to ECR...\n"
3341# Authenticate Docker to ECR
3442aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com
3543
3644printf " Building Docker image...\n"
37- # Build Docker image
38- # docker build -t $REPOSITORY_NAME:$IMAGE_TAG .
39- # Build Docker image for linux/amd64 architecture
40- docker buildx create --use
41- docker buildx build --platform linux/amd64 -t $ACCOUNT_ID .dkr.ecr. $AWS_REGION .amazonaws.com/ $REPOSITORY_NAME : $IMAGE_TAG --push .
45+ # Remove existing Docker image if it exists
46+ docker rmi $IMAGE_NAME --force
47+
48+ # Pull the base image for linux/amd64 architecture
49+ docker pull --platform linux/amd64 grafana/grafana:latest
4250
51+ # Build Docker image for linux/amd64 architecture and push to ECR
52+ docker buildx create --use
53+ docker buildx build --platform linux/amd64 -t $IMAGE_NAME --push .
4354
4455# Check if the build was successful
4556if [ $? -ne 0 ]; then
4657 echo " Docker build failed."
4758 exit 1
4859fi
4960
50- printf " Tagging Docker image...\n"
51- # Tag Docker image
52- docker tag $REPOSITORY_NAME :$IMAGE_TAG $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$REPOSITORY_NAME :$IMAGE_TAG
53-
54- # Check if the tag was successful
55- if [ $? -ne 0 ]; then
56- echo " Docker tag failed."
57- exit 1
58- fi
59-
60- printf " Pushing Docker image to ECR...\n"
61- # Push Docker image to ECR
62- docker push $ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$REPOSITORY_NAME :$IMAGE_TAG
63-
64- # Check if the push was successful
65- if [ $? -ne 0 ]; then
66- echo " Docker push failed."
67- exit 1
68- fi
61+ # Inspect the built image
62+ echo " Image: $LOCAL_IMAGE_NAME "
6963
7064echo " Docker image built and pushed to ECR successfully."
0 commit comments