cicd.yml
yml3 kB98 linesIn solution.zip
solution.zip.githubworkflowscicd.yml
# Every push to main: test the full stack, then build and push both images to Docker Hub.
# The deploy server on my machine listens for this workflow finishing with "success",
# so the name below must stay exactly "CI/CD".
name: CI/CD
on:
push:
branches: [main]
# Two quick pushes: the older run is cancelled, so an old image can't overwrite a newer one.
concurrency:
group: cicd-${{ github.ref }}
cancel-in-progress: true
env:
IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/auth-app
jobs:
test:
name: Playwright tests (full stack)
runs-on: ubuntu-latest
env:
# Throwaway password for the database that lives only on this runner.
POSTGRES_PASSWORD: ci-only-password
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- name: Install test dependencies
run: npm ci
- name: Install Chromium for Playwright
run: npx playwright install --with-deps chromium
- name: Start db, backend and frontend
run: docker compose up -d --build --wait
- name: Wait until the API answers through nginx
run: curl --fail --silent --show-error --retry 30 --retry-delay 1 --retry-all-errors http://localhost:5173/api/health
# playwright.config.js has reuseExistingServer: true, so it tests the stack on :5173
# instead of starting the Vite dev server.
- name: Run the Playwright tests
run: npx playwright test
- name: Show container logs
if: failure()
run: docker compose logs
- name: Keep the HTML report
if: always()
uses: actions/upload-artifact@v7
with:
name: playwright-report
path: playwright-report/
retention-days: 7
build-and-push:
name: Build and push images
needs: test # runs only if every test passed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
# Build for Intel and Apple silicon, so the images run natively on any machine.
- uses: docker/setup-qemu-action@v4
- uses: docker/setup-buildx-action@v4
- name: Log in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push the backend
uses: docker/build-push-action@v7
with:
context: ./backend
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ env.IMAGE }}:backend
cache-from: type=gha,scope=backend
cache-to: type=gha,mode=max,scope=backend
- name: Build and push the frontend
uses: docker/build-push-action@v7
with:
context: .
file: Dockerfile.multistage
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ env.IMAGE }}:frontend
cache-from: type=gha,scope=frontend
cache-to: type=gha,mode=max,scope=frontend