# vim: ts=2 sw=2:

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

name: Create Release

jobs:
  release:
    name: Create Release
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}

      - name: Get Version Info
        id: get_tag
        shell: bash
        run: |
          SUBJECT=$(/usr/bin/git tag -l ${GITHUB_REF#refs/tags/} --format='%(subject)')
          BODY=$(/usr/bin/git tag -l ${GITHUB_REF#refs/tags/} --format='%(body)' | sed '/-----BEGIN PGP SIGNATURE-----/,$d')

          echo "SUBJECT=$SUBJECT"
          echo "BODY=$BODY"

          echo 'tag_subject<<EOS' >> $GITHUB_ENV
          echo "$SUBJECT" >> $GITHUB_ENV
          echo 'EOS' >> $GITHUB_ENV
          echo 'tag_body<<EOB' >> $GITHUB_ENV
          echo "$BODY" >> $GITHUB_ENV
          echo 'EOB' >> $GITHUB_ENV
          echo "tag_version=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
          echo "version_num=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV

      - name: Get Changelog Content
        id: changelog_reader
        uses: mindsers/changelog-reader-action@v2
        with:
          version: ${{ env.version_num }}
          path: ./CHANGELOG.md

      - name: Publish Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.tag_version }}
          release_name: ${{ env.tag_subject }}
          body: |
            ${{ env.tag_body }}

            ## Changelog

            ${{ steps.changelog_reader.outputs.changes }}
          draft: false
          prerelease: false
