Skip to content

Finalised the CI/CD pipeline and uncommented out the rest of the code #19

Finalised the CI/CD pipeline and uncommented out the rest of the code

Finalised the CI/CD pipeline and uncommented out the rest of the code #19

Workflow file for this run

name: Build and Deploy Code
on: [push, pull_request]
jobs:
build_and_test:
environment:
name: testing
env:
PG_USERNAME: ${{secrets.PG_USERNAME}}
PG_PASSWORD: ${{secrets.PG_PASSWORD}}
PG_HOST: ${{secrets.PG_HOST}}
PG_PORT: ${{secrets.PG_PORT}}
PG_DATABASE: ${{secrets.PG_DATABASE}}
SECRET_KEY: ${{secrets.SECRET_KEY}}
ALGORITHM: ${{secrets.ALGORITHM}}
ACCESS_TOKEN_EXPIRE_MINUTES: ${{secrets.ACCESS_TOKEN_EXPIRE_MINUTES}}
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: ${{secrets.PG_PASSWORD}}
POSTGRES_DB: ${{secrets.PG_DATABASE}}_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
runs-on: ubuntu-latest
steps:
- name: Pull the git repository
uses: actions/checkout@v4
- name: Install python version 3.11.6
uses: actions/setup-python@v4
with:
python-version: '3.11.6'
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests with Pytest
run: |
pip install pytest
pytest -v -s --disable-warnings
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/sm-fastapi:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
deploy:
environment:
name: production
runs-on: ubuntu-latest
needs: [build_and_test]
steps:
- name: Pull the git repository
uses: actions/checkout@v4
- name: Deploy to Heroku
uses: akhileshns/heroku-deploy@v3.12.14
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: ${{secrets.HEROKU_APP_NAME}}
heroku_email: ${{secrets.HEROKU_EMAIL}}
- name: Deploy to Ubuntu VM
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.UBUNTU_VM_HOST }}
username: ${{ secrets.UBUNTU_VM_USERNAME }}
key: ${{ secrets.UBUNTU_VM_SSH_KEY }}
script: |
cd app/src
git pull
echo ${{secrets.UBUNTU_VM_PASSWORD}} | sudo -S systemctl restart api