
name: REL

on:
  push:
    tags: ["v[0-9]*"]

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:

      - name: Checkout code
        id: checkout
        uses: actions/checkout@v7

      - name: Build tarball
        id: build
        run: |
          make checkver
          make dist
          make doc/note.md
          PACKAGE=$(grep ^EXTENSION Makefile | sed 's/.*= *//')
          VERSION=$(grep ^EXTVERSION Makefile | sed 's/.*= *//')
          test "${{github.ref}}" = "refs/tags/v${VERSION}" || { echo "ERR: tag mismatch"; exit 1; }
          echo "PACKAGE=${PACKAGE}" >> $GITHUB_ENV
          echo "VERSION=${VERSION}" >> $GITHUB_ENV
          echo "TGZ=${PACKAGE}-${VERSION}.tar.gz" >> $GITHUB_ENV
          echo "DRAFT=false" >> $GITHUB_ENV
          echo "PRERELEASE=false" >> $GITHUB_ENV

      - name: "Create Github release"
        env:
          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
          title="${PACKAGE} v${VERSION}"
          ghf="--notes-file=doc/note.md"
          if test "${DRAFT}" = "true"; then ghf="${ghf} --draft"; fi
          if test "${PRERELEASE}" = "true"; then ghf="${ghf} --prerelease"; fi
          gh release create "v${VERSION}" "${TGZ}" --title="${title}" ${ghf}

  pgxn_upload:
    name: Upload to PGXN
    needs: [release]
    runs-on: ubuntu-latest
    container:
      image: pgxn/pgxn-tools
      env:
        PGXN_USERNAME: ${{secrets.PGXN_USERNAME}}
        PGXN_PASSWORD: ${{secrets.PGXN_PASSWORD}}
    steps:
      - uses: actions/checkout@v7
      - run: pgxn-bundle
      - run: pgxn-release

