From 1033f3933382c41ea819a4eab136f27f03090086 Mon Sep 17 00:00:00 2001 From: Irfan Date: Fri, 25 Oct 2024 16:24:36 +0100 Subject: [PATCH] Implement CI/CD using Github Actions (#41) --- .../workflows/docker-build-test-deploy.yml | 60 +++++++++++++++++++ backend/Dockerfile | 2 + docker-compose.yml | 8 +++ scripts/test-local.sh | 8 +-- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/docker-build-test-deploy.yml diff --git a/.github/workflows/docker-build-test-deploy.yml b/.github/workflows/docker-build-test-deploy.yml new file mode 100644 index 0000000..7d7153e --- /dev/null +++ b/.github/workflows/docker-build-test-deploy.yml @@ -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 + diff --git a/backend/Dockerfile b/backend/Dockerfile index 807e86b..8838a4e 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -48,6 +48,8 @@ ENV PYTHONPATH=/app COPY ./scripts/ /app/ +COPY ../scripts/ /app/scripts/ + COPY ./alembic.ini /app/ COPY ./prestart.sh /app/ diff --git a/docker-compose.yml b/docker-compose.yml index 525db7c..f151beb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 @@ -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 @@ -152,6 +159,7 @@ services: context: ./frontend args: - NODE_ENV=production + <<: *common-cache environment: - DTT_VITE_API_URL=https://${DOMAIN?Variable not set} labels: diff --git a/scripts/test-local.sh b/scripts/test-local.sh index 4c180f2..0318c29 100644 --- a/scripts/test-local.sh +++ b/scripts/test-local.sh @@ -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 "$@"