name: docker release

on:
  workflow_dispatch:
    inputs:
      TAG_NAME:
        description: 'Tag name that the major tag will point to'
        required: true
      PRERELEASE:
        description: 'Whether this is a prerelease'
        type: boolean
        required: true

  release:
    types: [published]

permissions:
  packages: write
  contents: write
  id-token: write

env:
  TAG_NAME: ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }}
  IS_PRERELEASE: ${{ github.event.release.prerelease || github.event.inputs.PRERELEASE }}

jobs:
  # This workaround is needed, as it's not possible to reference env.FOOBAR directly at this stage
  # - for ex. https://github.com/actions/runner/issues/1189
  release-container-prereq:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.release-param.outputs.tag }}
      tag_latest: ${{ steps.release-param.outputs.tag_latest }}
    steps:
    - id: release-param
      run: |
        # If env.IS_PRERELEASE is true, set tag to alpha and do not enable tag_latest
        # If env.IS_PRERELEASE is not true (aka false), don't set an extra tag and enable tag_latest
        echo "tag=${{ env.IS_PRERELEASE == 'true' && 'alpha' }}" >> $GITHUB_OUTPUT
        echo "tag_latest=${{ env.IS_PRERELEASE == 'true' && 'false' || 'true' }}" >> $GITHUB_OUTPUT

  release-container:
    needs: release-container-prereq
    uses: ./.github/workflows/reusable-container-workflow.yaml
    with:
      build_type: prod
      tag: ${{ needs.release-container-prereq.outputs.tag }}
      tag_latest: ${{ needs.release-container-prereq.outputs.tag_latest == 'true' }}
      image: ghcr.io/${{ github.repository }}
      registry: ghcr.io
      registry_username: ${{ github.repository_owner }}
      fetch_release: true
      release_version: ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }}
    secrets:
      registry_password: ${{ secrets.GITHUB_TOKEN }}
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  release:
    needs: [release-container-prereq, release-container]
    runs-on: ubuntu-latest
    steps:
    - name: print_env
      run: env

    - name: checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Install helm
      uses: azure/setup-helm@v4

    - name: Setup Go
      uses: actions/setup-go@v5

    - name: Configure Git
      if: env.IS_PRERELEASE != 'true'
      run: |
        git config user.name "$GITHUB_ACTOR"
        git config user.email "$GITHUB_ACTOR@users.noreply.github.com"

    - name: Update helm chart
      if: env.IS_PRERELEASE != 'true'
      run: |
        git checkout main
        sed -Ei \
            -e 's/^(version\:) .*/\1 '${{ env.TAG_NAME }}'/g' \
            -e 's/^(appVersion\:) .*/\1 "'${{ env.TAG_NAME }}'"/g' \
            contrib/charts/dragonfly/Chart.yaml

        go test ./contrib/charts/dragonfly/... -update

        git commit \
          -m 'chore(helm-chart): update to ${{ env.TAG_NAME }}' \
          contrib/charts/dragonfly/Chart.yaml \
          contrib/charts/dragonfly/ci || true

    - name: Push Helm chart as OCI to Github
      if: env.IS_PRERELEASE != 'true'
      run: |
        echo "${{ secrets.GITHUB_TOKEN }}" | \
          helm registry login -u ${{ github.actor }} --password-stdin ghcr.io

        helm package contrib/charts/dragonfly

        helm push dragonfly-${{ env.TAG_NAME }}.tgz oci://ghcr.io/${{ github.repository }}/helm

    - name: GitHub Push
      uses: CasperWA/push-protected@v2
      with:
        token: ${{ secrets.DRAGONFLY_TOKEN }}
        branch: main

    - name: Discord notification
      env:
        DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
      uses: Ilshidur/action-discord@0c4b27844ba47cb1c7bee539c8eead5284ce9fa9
      with:
        args: 'DragonflyDB version [${{ env.TAG_NAME }}](https://github.com/dragonflydb/dragonfly/releases/tag/${{ env.TAG_NAME }}) has been released 🎉'

    - name: Re-build Docs
      if: env.IS_PRERELEASE != 'true'
      run: |
        curl -s -X POST '${{ secrets.VERCEL_DOCS_WEBHOOK }}'
