name: Build and Deploy Antora Docs

on:
  push:
    branches: [main]
    paths:
      - "docs/**"
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    container: catthehacker/ubuntu:act-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v5

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ vars.CONTAINER_REGISTRY }}
          username: ${{ secrets.CONTAINER_REGISTRY_USERNAME }}
          password: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: docs/Dockerfile
          push: true
          tags: ${{ vars.CONTAINER_REGISTRY }}/${{ vars.CONTAINER_IMAGE_NAME_DOCS }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

  deploy:
    needs: build
    runs-on: ubuntu-latest
    container: catthehacker/ubuntu:act-latest
    environment:
      name: prod
      url: https://docs.servala.com/

    steps:
      - name: Checkout repository
        uses: actions/checkout@v5

      - name: Deploy to OpenShift
        uses: docker://quay.io/appuio/oc:v4.19
        with:
          entrypoint: /bin/bash
          args: |
            -c "oc login --token=${OPENSHIFT_TOKEN} --server=${OPENSHIFT_URL} && \
              oc -n ${NAMESPACE} apply --overwrite -k docs/deployment/ && \
              oc -n ${NAMESPACE} rollout restart deployment/docs"
        env:
          NAMESPACE: ${{ vars.NAMESPACE_DOCS }}
          KUBECONFIG: /tmp/kube_config
          OPENSHIFT_TOKEN: ${{ secrets.OPENSHIFT_TOKEN }}
          OPENSHIFT_URL: ${{ secrets.OPENSHIFT_URL }}
