on:
  pull_request:
    branches:
      - main
    paths:
      - backend/**

name: Build and deploy the backend to staging

jobs:
  build-and-push:
    name: Build and push image
    uses: ./.gitea/workflows/workflow_build-image.yaml
    with:
      # sets a unique tag for each commit in the PR - this gets deployed to a separate application instance using argocd
      tag: sha${{ github.sha }}
    secrets:
      PACKAGE_REGISTRY_ACCESS: ${{ secrets.PACKAGE_REGISTRY_ACCESS }}

  notify:
    runs-on: ubuntu-latest
    name: Add a comment to the PR to notify about the deployment
    steps:
      - name: Download gitea client
        run: |
          curl -sSL -o tea https://dl.gitea.com/tea/0.11.0/tea-0.11.0-linux-amd64
          chmod +x tea
      - name: Post comment
        run: |
          ./tea pr comment ${{ github.event.number }} "The backend has been deployed to staging with url https://pr-${{ github.event.number }}.anyway-stg.anydev.info. Check the deployment status in ArgoCD:

          [![App Status](https://argocd.kluster.moll.re/api/badge?name=anydev-anyway-backend-stg-pr-${{ github.event.number }}&revision=true&showAppName=true)](https://argocd.kluster.moll.re/applications/anydev-anyway-backend-stg-pr-${{ github.event.number }})"
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          GITEA_BASE_URL: ${{ secrets.GITEA_BASE_URL }}
          GITEA_REPO: ${{ secrets.GITEA_REPO }}
