Skip to content

Automate Deployment to AWS Cloud #2

Automate Deployment to AWS Cloud

Automate Deployment to AWS Cloud #2

Workflow file for this run

# Workflow references https://stackoverflow.com/questions/59166099/github-action-aws-cli,
# https://stackoverflow.com/questions/51028677/create-aws-ecr-repository-if-it-doesnt-exist,
# https://github.com/aws-actions/amazon-ecr-login and
# https://medium.com/@octavio/ecs-deployments-with-github-actions-dd34beed6528
# https://stackoverflow.com/questions/75546117/github-action-how-to-edit-a-json-objects-with-github-repository-secrets
name: Run Tests and Deploy to AWS
on: [push, pull_request]
env:
PYTHON_VERSION: "3.12"
ECR_REPO_NAME: "ssg-sample-application"
AWS_REGION: "ap-southeast-1"
ECS_TASK_DEFINITION: "./revamped-application/deploy/task-definition.json"
ECS_CONTAINER_NAME: "app" # should be the same as containerDefinitions of task definition file
ECS_SERVICE_NAME: "arn:aws:ecs:ap-southeast-1:730335480348:service/SSG-ECS-App/ssg-wsg-app"
ECS_CLUSTER_NAME: "arn:aws:ecs:ap-southeast-1:730335480348:cluster/SSG-ECS-App"
jobs:
test:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macOS-latest ]
name: Test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: "pip"
- name: Install dependencies
working-directory: ./SSG-API-Testing-Application-v2/app
run: pip install -r requirements.txt
- name: Run tests
working-directory: ./SSG-API-Testing-Application-v2/app
run: python test_runner.py
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4.0.1
with:
files: ./SSG-API-Testing-Application-v2/coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
deploy:
needs:
- test
runs-on: ubuntu-latest
name: Create Docker Image and Deploy to ECR
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update AWS CLI
run: |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
- name: Configure AWS Credentials
id: configure-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 ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
# we need to use the AWS CLI to create the ECR repository
- name: Create ECR Repository
id: create-ecr-repo
run: aws ecr create-repository --repository-name ${{ env.ECR_REPO_NAME }} || true
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ env.AWS_REGION }}
- name: Create and Push Docker Image
id: create-and-push-docker-image
working-directory: SSG-API-Testing-Application-v2/app
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: ${{ env.ECR_REPO_NAME }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
echo "image=$REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Create ECS Task Definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: SSG-API-Testing-Application-v2/deploy/task-definition.json
container-name: ${{ env.ECS_CONTAINER_NAME }}
image: ${{ steps.create-and-push-docker-image.outputs.image }}
- name: Deploy Task Definition
id: task-def-deploy
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE_NAME }}
cluster: ${{ env.ECS_CLUSTER_NAME }}
wait-for-service-stability: true