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: "Get run info"
        id: "get-run-info"
        uses: "./.gitea/actions/get-run-info"
        with:
          compose-project-base: "${{ vars.COMPOSE_PROJECT_BASE }}"
      - name: "Get build options"
        id: "get-build-options"
        uses: "./.gitea/actions/get-build-options"
      - name: "Set up Docker Buildx"
        id: "setup-docker-buildx"
        uses: "docker/setup-buildx-action@v3"
        with:
          driver: "remote"
          endpoint: "tcp://builder-01.bthlab:2375"
          platforms: "linux/amd64"
          append: |
            - endpoint: "tcp://builder-mac-01.bthlab:2375"
              platforms: "linux/arm64"
      - 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-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - 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-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - 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-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - 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-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - 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-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - 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-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - name: "Build `apple-ci` image"
        uses: docker/build-push-action@v6
        with:
          file: "services/apple/Dockerfile"
          context: "services/"
          target: "ci"
          push: false
          load: true
          tags: "docker-hosted.nexus.bthlabs.pl/hotpocket/apple:ci-${{ steps.get-run-info.outputs.compose-project }}"
          platforms: "${{ steps.get-build-options.outputs.build-platform }}"
          cache-from: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket"
          cache-to: "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max"
      - name: "Prepare the build"
        id: "prepare"
        env:
          COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}"
        run: |
          set -x
          ./.gitea/tools/render-docker-compose-ci.sh
      - name: "Run `backend` checks"
        if: "steps.prepare.conclusion == 'success'"
        env:
          COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}"
        run: |
          set -x
          docker compose \
            -p "${COMPOSE_PROJECT}" \
            -f "docker-compose.yaml" \
            -f "docker-compose-ci.yaml" \
            -f "docker-compose-ci-${COMPOSE_PROJECT}.yaml" \
            run --rm \
            backend-ci inv ci
      - name: "Run `packages` checks"
        if: "steps.prepare.conclusion == 'success'"
        env:
          COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}"
        run: |
          set -x
          docker compose \
            -p "${COMPOSE_PROJECT}" \
            -f "docker-compose.yaml" \
            -f "docker-compose-ci.yaml" \
            -f "docker-compose-ci-${COMPOSE_PROJECT}.yaml" \
            run --rm \
            packages-ci inv ci
      - name: "Run `extension` checks"
        if: "steps.prepare.conclusion == 'success'"
        env:
          COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}"
        run: |
          set -x
          docker compose \
            -p "${COMPOSE_PROJECT}" \
            -f "docker-compose.yaml" \
            -f "docker-compose-ci.yaml" \
            -f "docker-compose-ci-${COMPOSE_PROJECT}.yaml" \
            run --rm \
            extension-ci inv ci
      - name: "Run `apple` checks"
        if: "steps.prepare.conclusion == 'success'"
        env:
          COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}"
        run: |
          set -x
          docker compose \
            -p "${COMPOSE_PROJECT}" \
            -f "docker-compose.yaml" \
            -f "docker-compose-ci.yaml" \
            -f "docker-compose-ci-${COMPOSE_PROJECT}.yaml" \
            run --rm \
            apple-ci inv ci
      - name: "Clean up"
        if: always()
        env:
          COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}"
        run: |
          set -x
          docker compose \
            -p "${COMPOSE_PROJECT}" \
            -f "docker-compose.yaml" \
            -f "docker-compose-ci.yaml" \
            -f "docker-compose-ci-${COMPOSE_PROJECT}.yaml" \
            down --volumes --rmi all || true
          rm -f "docker-compose-ci-${COMPOSE_PROJECT}.yaml" || true
