From cc61f8ee9e6e4eaae2640a609e98cc8aa79447b1 Mon Sep 17 00:00:00 2001 From: Jerom Date: Wed, 10 Jul 2024 19:50:48 +0530 Subject: [PATCH] perf(acions): update existing latest tag with new version number and add new latest image to ecr --- .github/workflows/ECR_workflow.yml | 38 +++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ECR_workflow.yml b/.github/workflows/ECR_workflow.yml index ab4bc3a..a224ab6 100644 --- a/.github/workflows/ECR_workflow.yml +++ b/.github/workflows/ECR_workflow.yml @@ -2,13 +2,11 @@ name: Deploy to Amazon ECR on: push: - branches: [ "prod" ] + branches: [ "main" ] env: -# set this to your preferred AWS region, e.g. us-west-1 - AWS_REGION: ${{ secrets.MY_AWS_REGION }} -# set this to your Amazon ECR repository name - ECR_REPOSITORY: ${{ secrets.MY_ECR_REPOSITORY }} + AWS_REGION: ${{ secrets.AWS_REGION }} + ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }} permissions: contents: read @@ -33,15 +31,37 @@ jobs: id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - - name: Build, tag, and push image to Amazon ECR + - name: Pull the latest image from Amazon ECR + id: pull-latest + run: | + # Retrieve the latest image tag + latest_image=$(aws ecr describe-images --repository-name $ECR_REPOSITORY --query 'sort_by(imageDetails,&imagePushedAt)[-1].imageTags[0]' --output text --region $AWS_REGION) + echo "Latest image tag: $latest_image" + + # Pull the latest image + docker pull $ECR_REGISTRY/$ECR_REPOSITORY:$latest_image + + # Retag the pulled image + new_tag="new-tag-according-to-your-needs" + docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$latest_image $ECR_REGISTRY/$ECR_REPOSITORY:$new_tag + + # Push the renamed image to ECR + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$new_tag + + - name: Build, tag, and push new image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ github.sha }} run: | - # Build a docker container and - # push it to ECR so that it can - # be deployed to ECS. + # Build a new Docker image docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + + # Tag the new image as latest + docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest + + # Push the new image and the latest tag to ECR docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT