 name: Post tag checkout on supplier-integration env

 on:
   push:
     branches:
       - supplier_Int

 env:
   GIT_HELM_BRANCH: "supplier-integration"        

 jobs:

   build:
     name: Build
     runs-on: self-hosted
     steps:
       - name: Checkout code
         uses: actions/checkout@v4

       - name: Extract branch name and SHA
         id: extract_sha_branch
         run: |
           echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
           echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
            
       - name: Build Docker image
         run: docker build -t sgch-overlay .

       - name: Tag Docker image with Git tag #Tagging docker image
         run: |
            docker tag sgch-overlay shn-cntr-reg.cname.ro/sgchg/i-sgch-k8s-overlay:${{ steps.extract_sha_branch.outputs.branch }}-${{ steps.extract_sha_branch.outputs.sha_short }}
       - name: Manual Docker Login Test
         env:
          HARBOR_USER: "robot$sgchg+git_in_push"
          HARBOR_PASS: "xzmRMUWvYkd3nAHe1EGkAmdLB5kTA8jA"
          HARBOR_URL: "https://shn-cntr-reg.cname.ro"
         run: |
          echo $HARBOR_PASS | docker login -u $HARBOR_USER --password-stdin $HARBOR_URL
       - name: Push Image
         run: |
          docker push shn-cntr-reg.cname.ro/sgchg/i-sgch-k8s-overlay:${{ steps.extract_sha_branch.outputs.branch }}-${{ steps.extract_sha_branch.outputs.sha_short }}

       - name: Clone Helm Charts Repository
         run: |
           pwd
           git clone https://${{ secrets.GH_DEVOPS_RW_USER_NAME }}:${{ secrets.GH_DEVOPS_RW_USER_TOKEN }}@github.com/SG-ContentHub/sg-content-hub-helm.git
           cd sg-content-hub-helm
           pwd
           git checkout supplier-integration
           git pull
           pwd

       - name: Configure Git Credentials
         run: |
           git config --global user.name "sgghdevops.rw"
           git config --global user.email "sgghdevops.rw@sgilottery.com"
           git config --global credential.helper cache

       - name: Update Helm Image Tag
         run: |
           pwd
           cd $GITHUB_WORKSPACE/sg-content-hub-helm/charts/sgch-overlay
           sed -i "s/tag: .*/tag: ${{ steps.extract_sha_branch.outputs.branch }}-${{ steps.extract_sha_branch.outputs.sha_short }}/" ./values.yaml
           cat ./values.yaml
           git add .
           git commit -m "CI/CD pipeline updated ${{ steps.extract_sha_branch.outputs.branch }}-${{ steps.extract_sha_branch.outputs.sha_short }} image to new image tag"
           git push origin ${{ env.GIT_HELM_BRANCH }}

           #### EOF
