Running CI/CD Pipeline #199
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
name: CI/CD Pipeline | |
run-name: Running CI/CD Pipeline | |
on: [push, pull_request] | |
permissions: | |
id-token: write | |
contents: read | |
jobs: | |
Run-Tests: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres | |
env: | |
POSTGRES_PASSWORD: postgres | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
ports: | |
- 5432:5432 | |
strategy: | |
matrix: | |
# Run in all these versions of Python | |
python-version: [ "3.11" ] | |
steps: | |
- name: Checkout Repo | |
uses: actions/checkout@v4 | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
pip install pytest | |
pip install pytest-django | |
pip install pytest-cov | |
- name: Run tests | |
run: | | |
python -m pytest \ | |
--cov=core \ | |
--cov=accounts \ | |
--cov=stapi \ | |
--cov-config=.coveragerc \ | |
--cov-report=term > coverage.txt | |
env: | |
DJANGO_SETTINGS_MODULE: hydroserver.settings | |
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres | |
- name: Parse coverage report | |
id: coverage | |
run: | | |
coverage=$(awk '/TOTAL/ {print $4}' coverage.txt | sed 's/%//') | |
if (( $(echo "$coverage < 50" | bc -l) )); then | |
echo "Test Coverage is below 50%: $coverage%" | |
exit 1 | |
fi | |
echo "Test Coverage is $coverage%" | |
Deploy-Dev: | |
name: Deploy to AWS S3 Dev | |
needs: [Run-Tests] | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
steps: | |
- name: Checkout Repo | |
uses: actions/checkout@v4 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/${{ vars.AWS_IAM_ROLE }} | |
role-session-name: deploy-hydroserver-dev | |
aws-region: ${{ vars.AWS_REGION }} | |
- name: Create deployment package | |
run: zip -r deploy_package.zip ./ | |
- name: Upload deployment package to S3 | |
run: aws s3 cp deploy_package.zip s3://${{ vars.AWS_DJANGO_DEV_BUCKET }}/deploy_package_${{ github.sha }}.zip | |
- name: Create ElasticBeanstalk Application Version | |
run: | | |
aws elasticbeanstalk create-application-version \ | |
--application-name ${{ vars.AWS_DJANGO_DEV_NAME }} \ | |
--source-bundle S3Bucket="${{ vars.AWS_DJANGO_DEV_BUCKET }}",S3Key="deploy_package_${{ github.sha }}.zip" \ | |
--version-label "${{ github.sha }}" \ | |
--description "commit-sha-${{ github.sha }}" | |
- name: Deploy ElasticBeanstalk Application Version | |
run: | | |
aws elasticbeanstalk update-environment \ | |
--environment-name ${{ vars.AWS_DJANGO_DEV_NAME }}-env \ | |
--version-label "${{ github.sha }}" |