Action for Container

to automate deployment tasks to both EC2 instances and ECS services based on different branches.

Here's a brief overview of the workflow:

  1. Build Job:

    • This job builds and pushes a Docker image to Amazon ECR based on the branch being pushed.

    • It uses AWS credentials stored in GitHub secrets for authentication.

    • The Docker image is tagged based on the branch name.

  2. Deployment to EC2:

    • This job deploys to either development or staging EC2 instances based on the branch being pushed.

    • It uses SSH to connect to the EC2 instances and execute deployment scripts.

  3. Deployment to Production (ECS):

    • This job deploys to the production ECS cluster.

    • It updates the ECS task definition with the new Docker image and deploys it to the ECS cluster.

    • It waits for the service to stabilize before completing.

circle-info

if your workflow file is named deployment.yml, the path would be: .github/workflows/deployment.yml

name: AWS Deployment to EC2 and ECS
on:
  push:
    branches:
      - development
      - staging
      - production
    tags:
    - '*'
    
jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup AWS ECR
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-south-1 #${{secrets.AWS_REGION}}

      - name: Login to Amazon ECR
        id: login-pf-aws-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Build and push the tagged docker image to Amazon ECR
        id: build-image
        env:
          ECR: ${{ steps.login-pf-aws-ecr.outputs.registry }}
          REPO: test
          BRANCH_NAME: ${{ github.ref_name }} # Branch name
        run: |
          IMAGE_TAG="test-$BRANCH_NAME" 
          docker build -t $ECR/$REPO:$IMAGE_TAG .
          docker push $ECR/$REPO:$IMAGE_TAG
          echo "image=$ECR/$REPO:$IMAGE_TAG" >> $GITHUB_OUTPUT

########################
## Only Dev & Staging ##
########################

  deploy_to_EC2:
    name: deploy_to_EC2
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to Development
        if:  github.ref == 'refs/heads/development' 
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.DEV_EC2_IP }}
          username: ${{ secrets.DEV_EC2_USER }}
          key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
          command_timeout: 30m
          script: |
            cd /home/ubuntu/devops
            bash dev-deploy.sh

      - name: Deploy to Staging
        if:  github.ref == 'refs/heads/staging'
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.DEV_EC2_IP }}
          username: ${{ secrets.DEV_EC2_USER }}
          key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
          command_timeout: 30m
          script: |
            cd /home/ubuntu/devops
            bash staging-deploy.sh
            
######################
## Only Production  ##
######################

  deploy_to_production:
    name: deploy_to_production
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/production'
    steps:
      - name: Download task definition
        run: aws ecs describe-task-definition --region ap-south-1 --task-definition TD-TEST_SERVICE --query taskDefinition > task-definition.json

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: task-definition.json
          container-name: TEST
          image: ${{ secrets.AWS_ECR }}:image-tag

      - name: Deploy ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: TEST_SERVICE
          cluster: TEST_ECS
          wait-for-service-stability: true

Last updated