name: Build and Push Image
on:
    push:
        branches:
            - master

jobs:
    build:
        name: Build and push image
        runs-on: ubuntu-latest
        container: catthehacker/ubuntu:act-latest
        if: gitea.ref == 'refs/heads/master'

        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Create Kubeconfig
              run: |
                  mkdir $HOME/.kube
                  echo "${{ secrets.KUBEC_CONFIG_BUILDX }}" > $HOME/.kube/config

            - name: Set up Docker Buildx
              uses: docker/setup-buildx-action@v3
              with:
                  driver: kubernetes
                  driver-opts: |
                      namespace=gitea-runner
                      qemu.install=true

            - name: Login to Docker Registry
              uses: docker/login-action@v3
              with:
                  registry: git.aridgwayweb.com
                  username: armistace
                  password: ${{ secrets.REG_PASSWORD }}

            - name: Build and push
              uses: docker/build-push-action@v5
              with:
                  context: .
                  push: true
                  platforms: linux/amd64,linux/arm64
                  tags: |
                      git.aridgwayweb.com/armistace/blog:latest

            - name: Deploy
              approvers: armistace
              minimum-approvals: 1
              issue-title: "Approval Required for Deploy"
              issue-body: "Do you want to deploy this blog to production?"
              run: |
                  kubectl delete namespace blog
                  kubectl create namespace blog
                  kubectl create secret docker-registry regcred --docker-server=${{ vars.DOCKER_SERVER }} --docker-username=${{ vars.DOCKER_USERNAME }} --docker-password='${{ secrets.DOCKER_PASSWORD }}' --docker-email=${{ vars.DOCKER_EMAIL }} --namespace=blog
                  kubectl apply -f kube/blog_pod.yaml && kubectl apply -f kube/blog_deployment.yaml && kubectl apply -f kube/blog_service.yaml
