Skip to content

Changes to pull request #11

Changes to pull request

Changes to pull request #11

Workflow file for this run

name: Build and Deploy Code
# when does CI/CD trigger (on)
# it triggers (on) when git push/pull_request is main on specified branches
on:
push:
branches:
- "main"
pull_request:
branches:
- "main"
# A job is a set of steps in a workflow that is executed on the same runner.
# Each step is either a shell script that will be executed, or an action that will be run.
# Steps are executed in order and are dependent on each other.
# Since each step is executed on the same runner, you can share data from one step to another.
jobs:
build:
environment: # fetch variable from github environment
name: testing
env:
# Database Environment Variables
DATABASE_HOSTNAME: ${{secrets.DATABASE_HOSTNAME}} # fetches values from Github Action Secrets / Environment
DB_PASSWORD: ${{secrets.DB_PASSWORD}}
DATABASE_PORT: ${{secrets.DATABASE_PORT}}
DATABASE_NAME: ${{secrets.DATABASE_NAME}}
DB_USERNAME: ${{secrets.DB_USERNAME}}
# Authorization Environment Variables
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.DB_PASSWORD}}
POSTGRES_DB: ${{secrets.DATABASE_NAME}}_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
runs-on: ubuntu-latest
steps:
- name: pulling git repo
uses: actions/checkout@v4
- name: install python version 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: upgrade pip
run: python -m pip install --upgrade pip
- name: install all dependencies
run: pip install -r requirements.txt
- name: test with pytest
run: |
pip install pytest
pytest
# - name: Login to Docker Hub
# uses: docker/login-action@v2
# with:
# username: ${{secrets.DOCKERHUB_USERNAME}}
# password: ${{secrets.DOCKERHUB_ACCESS_TOKEN}}
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v2
# - name: Build and push
# uses: docker/build-push-action@v4
# with:
# context: .
# file: ./Dockerfile
# push: true
# tags: ${{secrets.DOCKERHUB_USERNAME}}/fastapi:latest
# - name: Image Digest
# run: echo ${{steps.docker_build.outputs.digest}}
############# CI part ends here #############################
############# CD part starts here ###########################
deploy:
runs-on: ubuntu-latest
needs: [build] #Which job does it come after
environment:
name: production
steps:
# login to ubuntu server
# cd /app/src
# git pull
# echo password | sudo -S systemctl restart api // passing the password into for authentication
- name: deploy to ubuntu server
uses: appleboy/[email protected]
with:
host: ${{secrets.PRODUCTION_HOST}}
username: ${{secrets.PRODUCTION_USERNAME}}
password: ${{secrets.PRODUCTION_PASSWORD}}
script: |
cd app/src
git pull origin main
echo password | sudo -S systemctl restart api