Skip to content

Commit

Permalink
perf(acions): update existing latest tag with new version number and …
Browse files Browse the repository at this point in the history
…add new latest image to ecr
  • Loading branch information
lordgrim18 committed Jul 10, 2024
1 parent 1a37f9e commit cc61f8e
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions .github/workflows/ECR_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit cc61f8e

Please sign in to comment.