name: Release

on:
  workflow_dispatch:
    inputs:
      commit:
        description: "Commit (e.g., 52f0348)"
        default: "master"

permissions:
  contents: read

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Check out code
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
        with:
          ref: ${{ github.event.inputs.commit }}
          persist-credentials: false
      - name: Set up info
        env:
          INPUT_COMMIT: ${{ github.event.inputs.commit }}
        run: |
          set -x -e
          VERSION=$(make -f Makefile.release version)
          COMMIT=$(git rev-parse --quiet --verify "${INPUT_COMMIT}")
          if [[ "$(git tag -l v${VERSION})" == "v${VERSION}" ]]; then
            echo "v${VERSION} already released"
            exit 1
          fi
          echo "commit=${COMMIT}" >> $GITHUB_OUTPUT
          echo "version=${VERSION}" >> $GITHUB_OUTPUT
        id: info
      - name: Build release binary
        run: make -f Makefile.release release
      - name: Build release binary sha256
        run: (cd release; for asset in `ls -A *.tgz *.zip`; do sha256sum $asset > $asset.sha256; done)
      - name: Remove hidden section
        env:
          VERSION: ${{ steps.info.outputs.version }}
        run: sed '/+++/,//d' "notes/coredns-${VERSION}.md" > release.md
      - name: Log release info
        env:
          COMMIT: ${{ steps.info.outputs.commit }}
          VERSION: ${{ steps.info.outputs.version }}
        run: |
          set -x -e
          git log -1
          echo "${COMMIT}"
          echo "${VERSION}"
          cat release.md
          sha256sum release/*.tgz release/*.zip
      - name: Draft release
        uses: softprops/action-gh-release@718ea10b132b3b2eba29c1007bb80653f286566b  # v3.0.1
        with:
          body_path: release.md
          name: v${{ steps.info.outputs.version }}
          tag_name: v${{ steps.info.outputs.version }}
          target_commitish: ${{ steps.info.outputs.commit }}
          files: |
            release/*.tgz
            release/*.tgz.sha256
            release/*.zip
            release/*.zip.sha256
          draft: true
