name: "CI"

on:
  push:
    branches:
      - "development"
      - "public"
  pull_request:
    branches:
      - "development"
      - "public"

jobs:
  run-checks:
    name: "Checks"
    runs-on: "ubuntu-latest"
    steps:
      - name: "Checkout the code"
        uses: "actions/checkout@v2"
      - name: "Set up Docker Buildx"
        uses: "docker/setup-buildx-action@v3"
      - name: "Build `postgres` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/postgres/Dockerfile"
          context: "services/"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/postgres:15.13-local"
      - name: "Build `keycloak` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/keycloak/Dockerfile"
          context: "services/"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/keycloak:22.0.3-local"
      - name: "Build `rabbitmq` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/rabbitmq/Dockerfile"
          context: "services/"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/rabbitmq:3.10.8-local"
      - name: "Build `backend-ci` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/backend/Dockerfile"
          context: "services/"
          target: "ci"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/backend:ci-local"
      - name: "Build `packages-ci` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/packages/Dockerfile"
          context: "services/"
          target: "ci"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/packages:ci-local"
      - name: "Build `extension-ci` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/extension/Dockerfile"
          context: "services/"
          target: "ci"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/extension:ci-local"
      - name: "Run `backend` checks"
        run: |
          set -x
          docker compose -f docker-compose.yaml -f docker-compose-ci.yaml run --rm backend-ci inv ci
      - name: "Run `packages` checks"
        if: always()
        run: |
          set -x
          docker compose -f docker-compose.yaml -f docker-compose-ci.yaml run --rm packages-ci inv ci
      - name: "Run `extension` checks"
        if: always()
        run: |
          set -x
          docker compose -f docker-compose.yaml -f docker-compose-ci.yaml run --rm extension-ci inv ci
      - name: "Clean up"
        if: always()
        run: |
          set -x
          docker compose -f docker-compose.yaml -f docker-compose-ci.yaml down --volumes
