name: Docker CI

on:
  pull_request:
    paths:
      - ".github/workflows/docker-ci.yml"
      - "Dockerfile"
      - "pom.xml"
      - "src/**"
  push:
    branches: [master, main]
    paths:
      - ".github/workflows/docker-ci.yml"
      - "Dockerfile"
      - "pom.xml"
      - "src/**"

concurrency:
  group: docker-ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6.0.3

      - uses: docker/setup-buildx-action@v4.1.0

      - name: Build image
        uses: docker/build-push-action@v7.2.0
        with:
          context: .
          file: Dockerfile
          tags: owl2vowl:ci
          load: true
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Smoke test
        run: |
          docker run -d --name owl2vowl-ci -p 8080:8080 owl2vowl:ci
          for i in $(seq 1 40); do
            if curl -sf http://127.0.0.1:8080/serverTimeStamp; then
              echo "OK"
              docker rm -f owl2vowl-ci
              exit 0
            fi
            sleep 3
          done
          docker logs owl2vowl-ci
          docker rm -f owl2vowl-ci
          exit 1
