Action for Container
to automate deployment tasks to both EC2 instances and ECS services based on different branches.
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: trueLast updated