name: On Create Tag

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
  publish:
    uses: ./.github/workflows/publish-image.yml
    with:
      build_mode: production
    secrets: inherit

  update-compose:
    needs: publish
    runs-on: ubuntu-latest
    steps:
      - name: Checkout main
        uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ github.token }}

      - name: Update image tag in production compose.yml
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          sed -i "s|\(image:.*:\)[^[:space:]]*|\1${VERSION}|" infra/production/site/compose.yml

      - name: Commit and push
        run: |
          git config user.name "respellion-bot"
          git config user.email "no-reply.git@respellion.nl"
          git add infra/production/site/compose.yml
          git commit -m "chore: update prod image to ${GITHUB_REF_NAME#v}"
          git push origin main

  deploy-prod:
    needs: update-compose
    uses: ./.github/workflows/deploy-prod.yml
    secrets: inherit
