Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- name: Build Multi-Arch & Deploy Application Update to EC2 ASG
- on:
- push:
- branches:
- - main # Trigger deployment only on pushes to the main branch
- permissions:
- contents: read # Required to checkout the repository code
- # id-token: write # Add this if switching to OIDC for AWS credentials
- env:
- AWS_REGION: ap-south-1
- APP_NAME: caloriemitra # Must match app name used in infra stack for resource naming conventions
- # Derive ECR repo name consistently
- # IMPORTANT: Verify this matches your actual ECR repo name created by CDK/CFN.
- ECR_REPOSITORY_NAME: caloriemitra-app-repo
- # IMPORTANT: Set this to the exact name of your Auto Scaling Group created by CFN/CDK
- # Find this in AWS Console > EC2 > Auto Scaling Groups, or CloudFormation/CDK outputs.
- # Replace the example value below with your REAL ASG name.
- ASG_NAME: CalorieMitra-Stack-CalorieMitraAsgASG6077E899-OUQnFodW0E0D # <--- REPLACE WITH YOUR ACTUAL ASG NAME
- # IMPORTANT: Set this to the exact name of the SSM Parameter storing the image tag
- # Verify this matches the output/definition from your CDK stack.
- # The CDK code used '/app/caloriemitra/image-tag'. Make sure this is consistent.
- SSM_IMAGE_TAG_PARAMETER_NAME: /app/caloriemitra/image-tag # <--- VERIFY OR REPLACE WITH YOUR ACTUAL PARAMETER NAME
- jobs:
- build-and-push-ecr:
- name: Build Multi-Arch Docker Image and Push to ECR
- runs-on: ubuntu-latest
- outputs:
- image_tag: ${{ steps.set_image_tag.outputs.tag }}
- steps:
- - name: Checkout repository
- uses: actions/checkout@v4
- # Set up Docker Buildx for multi-platform builds
- - name: Set up QEMU
- uses: docker/setup-qemu-action@v3
- - name: Set up Docker Buildx
- id: buildx
- uses: docker/setup-buildx-action@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: ${{ env.AWS_REGION }}
- - name: Login to Amazon ECR
- id: login-ecr
- uses: aws-actions/amazon-ecr-login@v2
- - name: Set Image Tag from Git SHA
- id: set_image_tag
- run: echo "tag=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_OUTPUT
- - name: Build, tag, and push multi-arch image to Amazon ECR
- id: build-image
- env:
- ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
- IMAGE_TAG: ${{ steps.set_image_tag.outputs.tag }}
- run: |
- ECR_REPO_URI="$ECR_REGISTRY/${{ env.ECR_REPOSITORY_NAME }}"
- echo "Building and pushing multi-arch image to $ECR_REPO_URI with tags :$IMAGE_TAG and :latest"
- # Use docker buildx build command
- docker buildx build \
- --platform linux/arm64 \
- -t $ECR_REPO_URI:$IMAGE_TAG \
- -t $ECR_REPO_URI:latest \
- --push \
- . # Build context is the current directory
- echo "Pushed multi-arch image manifest for tags: $IMAGE_TAG, latest"
- deploy-application:
- name: Deploy Application via ASG Instance Refresh
- runs-on: ubuntu-latest
- needs: build-and-push-ecr # Depends on the image being pushed
- steps:
- - name: Configure AWS credentials
- uses: aws-actions/configure-aws-credentials@v4
- with:
- # This role/user needs ssm:PutParameter and autoscaling:StartInstanceRefresh permissions
- aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
- aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- aws-region: ${{ env.AWS_REGION }}
- - name: Update Image Tag in SSM Parameter Store
- run: |
- echo "Updating SSM parameter '${{ env.SSM_IMAGE_TAG_PARAMETER_NAME }}' to tag '${{ needs.build-and-push-ecr.outputs.image_tag }}'"
- aws ssm put-parameter \
- --name "${{ env.SSM_IMAGE_TAG_PARAMETER_NAME }}" \
- --value "${{ needs.build-and-push-ecr.outputs.image_tag }}" \
- --type String \
- --overwrite \
- --region ${{ env.AWS_REGION }}
- - name: Trigger ASG Instance Refresh
- run: |
- echo "Starting Instance Refresh for ASG: '${{ env.ASG_NAME }}'"
- # Validate ASG name variable is set
- if [ -z "${{ env.ASG_NAME }}" ] || [ "${{ env.ASG_NAME }}" == "YOUR_ASG_NAME_HERE" ]; then
- echo "Error: ASG_NAME environment variable is not set correctly. Please update the workflow."
- exit 1
- fi
- aws autoscaling start-instance-refresh \
- --auto-scaling-group-name "${{ env.ASG_NAME }}" \
- --region ${{ env.AWS_REGION }} \
- --preferences '{ "MinHealthyPercentage": 50, "InstanceWarmup": 300 }' # Adjust MinHealthy and Warmup as needed
- # Optionally add a step here to monitor the refresh progress using `aws autoscaling describe-instance-refreshes`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement