# Build and deploy the solitaire server Docker image.
name: Build and Deploy

on:
  push:
    branches: [master]
    paths:
      - 'solitaire_server/**'
      - 'solitaire_wasm/**'
      - 'solitaire_web/**'
      - 'solitaire_sync/**'
      - 'solitaire_core/**'
      - 'solitaire_engine/**'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - 'solitaire_server/Dockerfile'
      - '.gitea/workflows/docker-build.yml'

env:
  REGISTRY: git.aleshym.co
  IMAGE: git.aleshym.co/funman300/solitaire-server

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          # Need full history so we can push the tag-update commit back.
          fetch-depth: 0
          token: ${{ secrets.CI_TOKEN }}

      - name: Set image tag
        id: meta
        run: echo "sha=${GITHUB_SHA::8}" >> "$GITHUB_OUTPUT"

      # WASM artifact freshness is owned by the `web-wasm-rebuild` workflow,
      # which rebuilds pkg/ in CI on every master change to a wasm-feeding crate
      # and commits it back (CI is the single source of truth — the artifacts
      # aren't byte-reproducible on contributor machines). That pkg/ commit then
      # triggers this workflow, so the deployed image always ships fresh wasm.
      # No drift check is needed here.

      - name: Log in to Gitea registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ gitea.actor }}
          password: ${{ secrets.CI_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: network=host

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: solitaire_server/Dockerfile
          push: true
          tags: |
            ${{ env.IMAGE }}:${{ steps.meta.outputs.sha }}
            ${{ env.IMAGE }}:latest
          cache-from: type=registry,ref=${{ env.IMAGE }}:buildcache
          cache-to: type=registry,ref=${{ env.IMAGE }}:buildcache,mode=max

      - name: Install kustomize
        run: |
          curl -sL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.4.3/kustomize_v5.4.3_linux_amd64.tar.gz | tar xz
          sudo mv kustomize /usr/local/bin/kustomize

      - name: Pin image tag and push to deploy branch
        run: |
          git config user.email "ci@gitea.local"
          git config user.name "Gitea CI"
          # Switch to the deploy branch, creating it from the current HEAD if absent.
          # Use 'git switch' (branch-only) to avoid ambiguity with the deploy/ directory.
          if git fetch origin deploy 2>/dev/null; then
            git switch deploy
          else
            git switch -c deploy
          fi
          # Update the pinned image tag.
          cd deploy
          kustomize edit set image solitaire-server=${{ env.IMAGE }}:${{ steps.meta.outputs.sha }}
          cd ..
          git add deploy/kustomization.yaml
          git diff --cached --quiet && exit 0
          git commit -m "chore(deploy): bump image to ${{ steps.meta.outputs.sha }} [skip ci]"
          git push origin deploy
