Skip to content

Commit

Permalink
Implement CI/CD using Github Actions (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3rfan authored Oct 25, 2024
1 parent 9bd3799 commit 1033f39
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/docker-build-test-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build frontend and backend, test and deploy

on:
push:
branches:
- main
- gha
pull_request:
branches:
- main

jobs:
build-and-test:
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.ref_name }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build the docker images
run: |
DOCKER_BUILDKIT=1 docker compose build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
backend frontend
- name: Update the buildx cache with new one
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Run tests
run: sh scripts/test-local.sh

login-and-deploy:
runs-on: self-hosted
needs: build-and-test
if: ${{ github.event_name != 'pull_request' && github.ref_name == 'main' }}
steps:
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Deploy the images
run: |
source .env
docker push $DOCKER_IMAGE_BACKEND:latest
docker push $DOCKER_IMAGE_FRONTEND:latest
2 changes: 2 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ ENV PYTHONPATH=/app

COPY ./scripts/ /app/

COPY ../scripts/ /app/scripts/

COPY ./alembic.ini /app/

COPY ./prestart.sh /app/
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
x-common-cache: &common-cache
cache_from:
- type=local,src=/tmp/.buildx-cache
cache_to:
- type=local,dest=/tmp/.buildx-cache-new,mode=max

services:
db:
image: postgis/postgis:12-3.4
Expand Down Expand Up @@ -76,6 +82,7 @@ services:
context: ./backend
args:
INSTALL_DEV: ${INSTALL_DEV-false}
<<: *common-cache
platform: linux/amd64 # Patch for M1 Mac
labels:
- traefik.enable=true
Expand Down Expand Up @@ -152,6 +159,7 @@ services:
context: ./frontend
args:
- NODE_ENV=production
<<: *common-cache
environment:
- DTT_VITE_API_URL=https://${DOMAIN?Variable not set}
labels:
Expand Down
8 changes: 4 additions & 4 deletions scripts/test-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# Exit in case of error
set -e

docker-compose down -v --remove-orphans # Remove possibly previous broken stacks left hanging after an error
docker compose down -v --remove-orphans # Remove possibly previous broken stacks left hanging after an error

if [ $(uname -s) = "Linux" ]; then
echo "Remove __pycache__ files"
sudo find . -type d -name __pycache__ -exec rm -r {} \+
fi

docker-compose build
docker-compose up -d
docker-compose exec -T backend bash /app/tests-start.sh "$@"
docker compose build
docker compose up -d
docker compose exec -T backend bash /app/tests-start.sh "$@"

0 comments on commit 1033f39

Please sign in to comment.