name: Build and Publish
run-name: ${{ gitea.actor }} build 🚀
on: [push]

jobs:
  Metadata:
    runs-on: arm64
    outputs:
      version: ${{ steps.meta.outputs.version }}
      git_commit: ${{ steps.meta.outputs.git_commit }}
      build_date: ${{ steps.meta.outputs.build_date }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - id: meta
        name: Derive build metadata
        run: |
          GIT_COMMIT=$(git rev-parse HEAD)
          if git describe --tags --exact-match >/dev/null 2>&1; then
            VERSION=$(git describe --tags --exact-match)
          else
            VERSION=$(git rev-parse --short=12 HEAD)
          fi
          BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
          echo "git_commit=$GIT_COMMIT" >> $GITHUB_OUTPUT
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "build_date=$BUILD_DATE" >> $GITHUB_OUTPUT

  BuildAndDeployAmd64:
    needs: Metadata
    runs-on: amd64
    steps:
      - uses: actions/checkout@v4
      - name: Build amd64 image
        run: |
          docker build \
            --build-arg VERSION=${{ needs.Metadata.outputs.version }} \
            --build-arg GIT_COMMIT=${{ needs.Metadata.outputs.git_commit }} \
            --build-arg BUILD_DATE=${{ needs.Metadata.outputs.build_date }} \
            --progress=plain \
            -t registry.knatofs.se/go-cart-actor-amd64:latest \
            -t registry.knatofs.se/go-cart-actor-amd64:${{ needs.Metadata.outputs.version }} \
            .
      - name: Push amd64 images
        run: |
          docker push registry.knatofs.se/go-cart-actor-amd64:latest
          docker push registry.knatofs.se/go-cart-actor-amd64:${{ needs.Metadata.outputs.version }}
      - name: Apply deployment manifests
        run: kubectl apply -f deployment/deployment.yaml -n cart
      - name: Rollout amd64 deployment (pin to version)
        run: |
          kubectl set image deployment/cart-actor-x86 -n cart cart-actor-amd64=registry.knatofs.se/go-cart-actor-amd64:${{ needs.Metadata.outputs.version }}
          kubectl rollout status deployment/cart-actor-x86 -n cart

  BuildAndDeployArm64:
    needs: Metadata
    runs-on: arm64
    steps:
      - uses: actions/checkout@v4
      - name: Build arm64 image
        run: |
          docker build \
            --build-arg VERSION=${{ needs.Metadata.outputs.version }} \
            --build-arg GIT_COMMIT=${{ needs.Metadata.outputs.git_commit }} \
            --build-arg BUILD_DATE=${{ needs.Metadata.outputs.build_date }} \
            --progress=plain \
            -t registry.knatofs.se/go-cart-actor:latest \
            -t registry.knatofs.se/go-cart-actor:${{ needs.Metadata.outputs.version }} \
            .
      - name: Push arm64 images
        run: |
          docker push registry.knatofs.se/go-cart-actor:latest
          docker push registry.knatofs.se/go-cart-actor:${{ needs.Metadata.outputs.version }}
      - name: Rollout arm64 deployment (pin to version)
        run: |
          kubectl set image deployment/cart-actor-arm64 -n cart cart-actor-arm64=registry.knatofs.se/go-cart-actor:${{ needs.Metadata.outputs.version }}
          kubectl rollout status deployment/cart-actor-arm64 -n cart
