CI - Test, Build & Push (Backend + Frontend) #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI - Test, Build & Push (Backend + Frontend) | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| branches: [development, main] | |
| paths: | |
| - 'backend/**' | |
| - 'frontend/**' | |
| - '.github/workflows/ci.yml' | |
| push: | |
| branches: [development, main] | |
| paths: | |
| - 'backend/**' | |
| - 'frontend/**' | |
| - '.github/workflows/ci.yml' | |
| env: | |
| REGISTRY_NAME: ${{ secrets.AZURE_ACR_NAME }} # e.g. myregistry | |
| REGISTRY_LOGIN_SERVER: ${{ secrets.AZURE_ACR_LOGIN_SERVER }} # e.g. myregistry.azurecr.io | |
| IMAGE_TAG: ${{ github.sha }} | |
| DOCKER_BUILDKIT: 1 | |
| permissions: | |
| id-token: write | |
| contents: read | |
| packages: write | |
| jobs: | |
| test_backends: | |
| runs-on: ubuntu-latest | |
| services: | |
| product_db: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DB: products | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: ["5432:5432"] | |
| order_db: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_DB: orders | |
| options: >- | |
| --health-cmd "pg_isready -U postgres" | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: ["5433:5432"] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Python 3.10 (with pip cache) | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.10' | |
| cache: 'pip' | |
| - name: Install deps | |
| run: | | |
| pip install -U pip pytest httpx | |
| for req in backend/*/requirements.txt; do | |
| echo "Installing $req" | |
| pip install -r "$req" | |
| done | |
| - name: Test product_service | |
| working-directory: backend/product_service | |
| env: | |
| POSTGRES_HOST: localhost | |
| POSTGRES_PORT: 5432 | |
| POSTGRES_DB: products | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| run: pytest -q | |
| - name: Test order_service | |
| working-directory: backend/order_service | |
| env: | |
| POSTGRES_HOST: localhost | |
| POSTGRES_PORT: 5433 | |
| POSTGRES_DB: orders | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| run: pytest -q | |
| build_and_push: | |
| needs: test_backends | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' # don’t push images on PRs | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - name: product_service | |
| context: ./backend/product_service | |
| - name: order_service | |
| context: ./backend/order_service | |
| - name: frontend | |
| context: ./frontend | |
| steps: | |
| - uses: actions/checkout@v4 | |
| # Azure login using a Service Principal secret | |
| - name: Azure Login | |
| uses: azure/login@v1 | |
| with: | |
| creds: ${{ secrets.AZURE_CREDENTIALS }} # Needs to be set as a GitHub Secret (Service Principal JSON) | |
| - name: ACR login (use registry NAME with --name) | |
| run: az acr login --name ${{ env.REGISTRY_NAME }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build & push ${{ matrix.name }} | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{ matrix.context }} | |
| push: true | |
| tags: | | |
| ${{ env.REGISTRY_LOGIN_SERVER }}/${{ matrix.name }}:latest | |
| ${{ env.REGISTRY_LOGIN_SERVER }}/${{ matrix.name }}:${{ env.IMAGE_TAG }} | |
| cache-from: type=registry,ref=${{ env.REGISTRY_LOGIN_SERVER }}/${{ matrix.name }}:buildcache | |
| cache-to: type=registry,ref=${{ env.REGISTRY_LOGIN_SERVER }}/${{ matrix.name }}:buildcache,mode=max |