From 1a37f9e4f9ffcc7d24fc2b716e2a1f6a47045575 Mon Sep 17 00:00:00 2001 From: Jerom Date: Wed, 10 Jul 2024 19:44:49 +0530 Subject: [PATCH 1/2] feat(acions): add basic ecr build workflow --- .github/workflows/CI_workflow.yml | 7 ++--- .github/workflows/ECR_workflow.yml | 47 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/ECR_workflow.yml diff --git a/.github/workflows/CI_workflow.yml b/.github/workflows/CI_workflow.yml index 2b81f49..1b60313 100644 --- a/.github/workflows/CI_workflow.yml +++ b/.github/workflows/CI_workflow.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ "main" ] + branches: [ "prod" ] pull_request: - branches: [ "main" ] + branches: [ "main", "prod" ] jobs: test: @@ -35,9 +35,6 @@ jobs: run: | touch .env echo "${{ secrets.DEV_ENV_FILE }}" > .env - - - name: Verify .env file creation - run: ls -la - name: Run tests run: npm test \ No newline at end of file diff --git a/.github/workflows/ECR_workflow.yml b/.github/workflows/ECR_workflow.yml new file mode 100644 index 0000000..ab4bc3a --- /dev/null +++ b/.github/workflows/ECR_workflow.yml @@ -0,0 +1,47 @@ +name: Deploy to Amazon ECR + +on: + push: + branches: [ "prod" ] + +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 }} + +permissions: + contents: read + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build, tag, and push 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. + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT From cc61f8ee9e6e4eaae2640a609e98cc8aa79447b1 Mon Sep 17 00:00:00 2001 From: Jerom Date: Wed, 10 Jul 2024 19:50:48 +0530 Subject: [PATCH 2/2] 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