Automate Deployment to AWS Cloud #5
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: | |
pull_request: | |
# pull_request_review: | |
# types: | |
# - submitted | |
push: | |
env: | |
BOTO3_DEPENDENCY: "1.34.139" | |
PYTHON_VERSION: "3.12" | |
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 to AWS only if the PR is approved | |
deploy: | |
needs: | |
- test | |
# if: github.event.review.state == 'approved' | |
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: 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 boto3==${{ env.BOTO3_DEPENDENCY }} prettytable | |
- name: Load Constants and Environment Variables | |
working-directory: ./SSG-API-Testing-Application-v2/deploy | |
run: python constants.py | |
- name: Build or Ensure Base Cloud Infrastructure | |
working-directory: ./SSG-API-Testing-Application-v2/deploy | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
run: python infrastructure.py | |
- 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 | |
- name: Create and Push Docker Image to ECR | |
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: Set up ECS Cluster | |
working-directory: ./SSG-API-Testing-Application-v2/deploy | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
ECS_IMAGE: ${{ steps.create-and-push-docker-image.outputs.image }} | |
run: | | |
python ecs.py |