name: ${{ github.actor }} executed Build Push Prod
on:
  push:
    tags:
      - 'v[0-9]+\.[0-9]+\.[0-9]+'
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      TOKEN: ${{ secrets.TOKEN }}
      USERNAME: ${{ vars.USERNAME }}
      TAG_VERSION: ${{ github.ref_name }}
    steps:
      - name: echo repo dir
        id: repo-url
        run: |
             echo "repourl=$(echo ${{ github.server_url }} | sed -e 's;https://;;')" >> $GITHUB_OUTPUT

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

      - name: Setup Quemu
        uses: docker/setup-qemu-action@v3

      - name: Set Up BuildX
        uses: docker/setup-buildx-action@v3
              
      - name: Login to Docker
        uses: docker/login-action@v3
        with:
           registry: https://git.maximotejeda.com
           username: ${{ vars.USERNAME }}
           password: ${{ secrets.PWD }}

      - name: calculate short sha
        id: calculate-sha
        run: |
          echo "shortsha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: "${{ steps.repo-url.outputs.repourl }}/${{ github.repository }}:${{ github.ref_name }}"
          build-args: |
            version=${{ github.ref_name }}
            SHORTSHA=${{ steps.calculate-sha.outputs.shortsha }}

  deploy:
    runs-on: ubuntu-latest
    needs: build
    env:
      TOKEN: ${{ secrets.TOKEN }}
      TAG: ${{ github.ref_name }}
      PASSWORD: $PWD
      USERNAME: ${{ vars.USERNAME }}
      TAG_VERSION: ${{ github.ref_name }}
      NATS_SERVICE_URL: "${{ vars.NATS_SERVICE_URL }}"
      ENV: "${{ vars.ENVIRONMENT }}"
      APPLICATION_PORT: "${{ vars.APPLICATION_PORT }}"
      DATA_SOURCE_URL: "${{ vars.DATA_SOURCE_URL }}"
    steps:
      - name: echo repo dir
        id: repo-url
        run: |
             echo "repourl=$(echo ${{ github.server_url }} | sed -e 's;https://;;')" >> $GITHUB_OUTPUT
             echo "repoName=$(echo ${{ github.repository }} | sed -e 's;${{ github.actor }}/;;')" >> $GITHUB_OUTPUT
             echo "${{ github.repository }} | sed -e 's;${{ github.actor }}/;;')"
      - name: Checkout code 
        uses: actions/checkout@v4
        
      - name: install envsubst
        run: |
             apt update
             apt install -y gettext-base
             apt clean

      - name:  create deployment files
        env:
          IMAGE: "${{ steps.repo-url.outputs.repourl }}/${{ github.repository }}:${{ github.ref_name }}"
          REPONAME: "${{ steps.repo-url.outputs.repoName }}"
        run: |
             envsubst < k8s/deployment.yml.template > k8s/deployment.yml
      - name: Download kubectl
        run: curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

      - name: install kubectl
        run: sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

      - name: insert kube config
        run: |
             mkdir ~/.kube
             export IMAGE="$( echo ${{ github.server_url }} | sed -e 's;https://;;')/${{ github.repository }}:${{ github.ref_name }}"
             echo "${{ secrets.KUBECONFIG }}" > ~/.kube/config
      - name: check pod
        run: kubectl apply -f k8s/deployment.yml

      - name: scale down deployment
        continue-on-error: true
        run: kubectl scale deployment ${{ steps.repo-url.outputs.repoName }} --replicas=0

      - name: scale up deployment
        run: kubectl scale deployment ${{ steps.repo-url.outputs.repoName }} --replicas=1
