Skip to content

Commit

Permalink
Add CI/CD config
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbag committed Dec 20, 2024
1 parent e576972 commit 9a6d07a
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 2 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: CD Pipeline

on:
push:
branches: [ main ]
tags: [ 'v*' ]

env:
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REPOSITORY: cv_matcher
ECS_CLUSTER: cv-matcher-cluster
ECS_SERVICE: cv-matcher-service
TASK_DEFINITION: .aws/task-definition.json

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
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@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Download task definition
run: |
aws ecs describe-task-definition \
--task-definition ${{ env.ECS_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: cv-matcher
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}

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

- name: Health check
run: |
HEALTH_CHECK_URL="${{ secrets.APP_URL }}/health"
for i in {1..30}; do
response=$(curl -s -o /dev/null -w "%{http_code}" $HEALTH_CHECK_URL)
if [ $response -eq 200 ]; then
echo "Health check passed"
exit 0
fi
echo "Waiting for service to be healthy..."
sleep 10
done
echo "Health check failed after 5 minutes"
exit 1
notify:
needs: deploy
runs-on: ubuntu-latest
if: always()

steps:
- name: Notify Slack on success
if: ${{ needs.deploy.result == 'success' }}
uses: slackapi/slack-github-action@v1.24.0
with:
channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
slack-message: "✅ Deployment to ${{ github.ref_name }} successful!"
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

- name: Notify Slack on failure
if: ${{ needs.deploy.result == 'failure' }}
uses: slackapi/slack-github-action@v1.24.0
with:
channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
slack-message: "❌ Deployment to ${{ github.ref_name }} failed!"
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI Pipeline

on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
cache: 'pip'

- name: Create virtual environment
run: make venv/create

- name: Install dependencies
run: |
make venv/install/all
- name: Run linters
run: make lint

- name: Run tests
env:
PYTHONPATH: ${PYTHONPATH}:.
run: make test

- name: Upload coverage reports
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
fail_ci_if_error: true
Loading

0 comments on commit 9a6d07a

Please sign in to comment.