Skip to content

Commit

Permalink
Setup github actions for CI/CD pipeline with build testing, deploy to…
Browse files Browse the repository at this point in the history
… production server and docker image
  • Loading branch information
dotpep committed Feb 29, 2024
1 parent 2d36508 commit a9efdc0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
51 changes: 21 additions & 30 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ name: Build and Deploy Code
on: [push, pull_request]

jobs:
# Continuous Integration
# Building and Testing project before pushing it to production
build-ci:
# Environment variables
environment:
name: testing
env:
Expand All @@ -16,6 +19,7 @@ jobs:
ALGORITHM: ${{ secrets.ALGORITHM }}
ACCESS_TOKEN_EXPIRE_MINUTES: ${{ secrets.ACCESS_TOKEN_EXPIRE_MINUTES }}

# Postgres Database instance
services:
postgres:
image: postgres
Expand All @@ -32,9 +36,9 @@ jobs:
runs-on: ubuntu-20.04
steps:
# Check-out repo and Set-up python
- name: Checkout Pulling Git Repo
uses: actions/checkout@v4

- name: Install Python version 3.10
uses: actions/setup-python@v5
with:
Expand All @@ -44,49 +48,35 @@ jobs:
# 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

#- name: Install Poetry version 1.7.1
# uses: abatilo/actions-poetry@v2
# with:
# poetry-version: '1.7.1'
#- name: Setup a local virtual environment (if no poetry.toml file)
# run: |
# poetry config virtualenvs.create true --local
# poetry config virtualenvs.in-project true --local
#- uses: actions/cache@v3
# name: Define a cache for the virtual environment based on the dependencies lock file
# with:
# path: ./.venv
# key: venv-${{ hashFiles('poetry.lock') }}
#- name: Install the project dependencies
# run: poetry install
#- name: Run the automated tests with PyTest
# run: poetry run pytest -v

# Install & Configure Poetry
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: '1.7.1'
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

# Load cached venv if cache exists
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

# Install dependencies if cache does not exist
- name: Install project dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

- name: Run tests
# Run test suite
- name: Run tests with Pytest
run: poetry run pytest -v tests/

#- name: Login to Docker Hub
Expand All @@ -108,6 +98,8 @@ jobs:
#- name: Image digest
# run: echo ${{ steps.docker_build.outputs.digest }}

# Continuous Delivery
# Deployment to production server
deploy-cd:
environment:
name: production
Expand All @@ -124,16 +116,15 @@ jobs:
username: ${{ secrets.PROD_USERNAME }}
password: ${{ secrets.PROD_PASSWORD }}
script: |
cd api/src
git pull
poetry install --no-interaction
poetry run alembic upgrade head
echo ${{ secrets.PROD_PASSWORD }} | sudo -S systemctl restart api
# login to our vps ubuntu server with ssh
# cd api/src
# git pull
# systemctl restart api
bash -l -c '
cd api/src
git pull origin main
poetry install --no-interaction --no-root
poetry run alembic upgrade head
echo ${{ secrets.PROD_PASSWORD }} | sudo -S systemctl restart api
'
# Pushing DockerHub image for production
#docker:
# runs-on: ubuntu-20.04
# steps:
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
app.include_router(auth.router)

app.get('/', tags=["Welcome Home Page"])(lambda: {
"message": "Welcome to my Social Media API powered by FastAPI, API documentation in '/docs' and `/redoc` endpoint.",
"message": "Welcome to my Social Media API powered by FastAPI, API documentation in '/docs' and `/redoc` endpoint. Successfully CI/CD pipeline deployment!",
"source_code": "https://github.com/dotpep/social-media-api",
"domain_name": "https://dotpep.xyz/"})

Expand Down

0 comments on commit a9efdc0

Please sign in to comment.