name: Docker

on:
    push:
        paths-ignore:
            - 'doc/**'
    pull_request:
        paths-ignore:
            - 'doc/**'

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

# adapted from https://raw.githubusercontent.com/stefanprodan/podinfo/master/.github/workflows/release.yml
#
jobs:
  containers:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[ci skip]') && !contains(github.event.head_commit.message, '[skip ci]')"
    env:
      PUSH_PACKAGES: ${{ github.repository_owner == 'PDAL' && github.event_name != 'pull_request'  }}
    permissions:
      packages: write
    steps:
      - uses: actions/checkout@v4
      - name: Lint
        id: lint
        run: |
          echo "are we pushing packages" ${{ env.PUSH_PACKAGES }}
          echo "event_name" ${{ github.event_name }}
          echo "ref" ${{ github.ref }}
      - name: Setup Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: latest
      - if: ${{ env.PUSH_PACKAGES == 'true' }}
        name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - if: ${{ env.PUSH_PACKAGES == 'true' }}
        name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Prepare
        id: prep
        run: |
          VERSION=sha-${GITHUB_SHA::8}
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF/refs\/tags\//}
          fi
          BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
          echo "BUILD_DATE=$BUILD_DATE" >> $GITHUB_OUTPUT
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
      - name: Build image
        uses: docker/build-push-action@v6
        with:
          push: ${{ env.PUSH_PACKAGES == 'true' }}
          builder: ${{ steps.buildx.outputs.name }}
          file: "./scripts/docker/ubuntu/Dockerfile"
          context: .
          platforms: linux/amd64

          # We need to pass in the SHA to the Dockerfile so it can checkout
          # our code. It is a different active SHA depending on whether it is a
          # PR or a simple push
          build-args: |
            GITHUB_SHA=${{ github.event.pull_request.head.sha != '' && github.event.pull_request.head.sha || github.sha }}
            GITHUB_REPOSITORY=${{ github.event.pull_request.head.repo.full_name != '' && github.event.pull_request.head.repo.full_name || github.repository }}
          tags: |
            docker.io/pdal/pdal:${{ steps.prep.outputs.VERSION }}
            docker.io/pdal/pdal:latest
            ghcr.io/pdal/pdal:${{ steps.prep.outputs.VERSION }}
            ghcr.io/pdal/pdal:latest
          labels: |
            org.opencontainers.image.title=${{ github.event.repository.name }}
            org.opencontainers.image.description=${{ github.event.repository.description }}
            org.opencontainers.image.source=${{ github.event.repository.html_url }}
            org.opencontainers.image.url=${{ github.event.repository.html_url }}
            org.opencontainers.image.revision=${{ github.sha }}
            org.opencontainers.image.version=${{ steps.prep.outputs.VERSION }}
            org.opencontainers.image.created=${{ steps.prep.outputs.BUILD_DATE }}
