Skip to content

update: testing backend deployment #8

update: testing backend deployment

update: testing backend deployment #8

Workflow file for this run

name: Backend CI Pipeline
on:
workflow_dispatch:
push:
branches:
- 'integration'
paths:
- 'backend/**'
- '!backend/*.md'
jobs:
build-and-test:
name: Build & Test
runs-on: ubuntu-latest
services:
db:
image: postgres:13
env:
POSTGRES_USER: app
POSTGRES_PASSWORD: changethis123
POSTGRES_DB: app
POSTGRES_HOST: localhost
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
cd backend
curl -sSL https://install.python-poetry.org | python3 -
poetry install
- name: Copy env file
run: |
cd backend
cp .env.sample .env
- name: Run app
run: |
cd backend
poetry run bash ./prestart.sh
poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 &
- name: Run tests
run: |
cd backend
poetry run pytest | tee /dev/null
build-push-image:
name: Build & Push Image
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set environment variables
run: |
COMMIT_SHA=$(git rev-parse --short HEAD)
echo "IMAGE_NAME=${{ secrets.DOCKERHUB_USERNAME }}/backend" >> $GITHUB_ENV
echo "IMAGE_TAG_1=latest" >> $GITHUB_ENV
echo "IMAGE_TAG_2=${{ github.run_number }}-$COMMIT_SHA" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: "{{defaultContext}}:backend"
push: true
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG_1 }}, ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG_2 }}
update-deployment:
name: Update Deployment File
runs-on: ubuntu-latest
needs: build-push-image
env:
GIT_USER_NAME: DrInTech
GIT_USER_EMAIL: ${{ secrets.TF_CERT_EMAIL }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set environment variables
run: |
COMMIT_SHA=$(git rev-parse --short HEAD)
echo "IMAGE_NAME=${{ secrets.DOCKERHUB_USERNAME }}/backend" >> $GITHUB_ENV
echo "IMAGE_TAG_2=${{ github.run_number }}-$COMMIT_SHA" >> $GITHUB_ENV
- name: Update Deployment YAML
run: |
sed -i "s|image: ${{ env.IMAGE_NAME }}:.*|image: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG_2 }}|" compose.yml
- name: Commit and Push Changes
run: |
BRANCH_NAME=${{ github.ref_name }}
git config --global user.email "${{ env.GIT_USER_EMAIL }}"
git config --global user.name "${{ env.GIT_USER_NAME }}"
git add compose.yml
git commit -m "Update Docker Image Tag to ${{ env.IMAGE_TAG_2 }}"
git pull --rebase origin $BRANCH_NAME
git push origin $BRANCH_NAME
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}