name: Build & Deploy

on:
  push:
    branches: [main, preprod]
  workflow_dispatch: {}

env:
  IMAGE: git.nfteam.ovh/neckfire/the-cycle

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Build & push image
        run: |
          set -e
          echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.nfteam.ovh -u "${{ secrets.REGISTRY_USER }}" --password-stdin
          SHA="${GITHUB_SHA::12}"
          BRANCH="${GITHUB_REF_NAME}"
          if [ "$BRANCH" = "main" ]; then TAGS="-t ${IMAGE}:latest -t ${IMAGE}:${SHA}"; else TAGS="-t ${IMAGE}:${BRANCH} -t ${IMAGE}:${BRANCH}-${SHA}"; fi
          docker build $TAGS -f Dockerfile .
          docker push --all-tags "${IMAGE}"
      - name: Notify ntfy
        if: always()
        run: |
          if [ "${{ job.status }}" = "success" ]; then EMOJI="white_check_mark"; PRIO="default"; else EMOJI="rotating_light"; PRIO="high"; fi
          curl -s -H "Authorization: Bearer ${{ secrets.NTFY_TOKEN }}" -H "Title: ${GITHUB_REPOSITORY} — ${{ job.status }}" \
            -H "Priority: ${PRIO}" -H "Tags: ${EMOJI}" -H "Click: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions" \
            -d "${GITHUB_WORKFLOW} (${GITHUB_REF_NAME} #${GITHUB_RUN_NUMBER}) : ${{ job.status }}" \
            "${{ secrets.NTFY_URL }}/${{ secrets.NTFY_TOPIC }}" || true
