name: release
on:
  push:
    tags:
    - "v*"
  workflow_dispatch:
    inputs:
      release_version:
        description: "Release version"
        required: true
        default: ""
      create_release:
        description: "Create release"
        required: true
        default: "true"

jobs:
  create-release:
    name: create-release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.release.outputs.upload_url }}
      release_version: ${{ env.RELEASE_VERSION }}
    steps:
    - name: Get the release version from the tag (push)
      shell: bash
      if: env.RELEASE_VERSION == '' && github.event_name == 'push'
      run: |
        # See: https://github.community/t5/GitHub-Actions/How-to-get-just-the-tag-name/m-p/32167/highlight/true#M1027
        echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
        echo "version is: ${{ env.RELEASE_VERSION }}"
    - name: Get the release version from the tag (dispatch)
      shell: bash
      if: github.event_name == 'workflow_dispatch'
      run: |
        echo "RELEASE_VERSION=${{ github.event.inputs.release_version }}" >> $GITHUB_ENV
        echo "version is: ${{ env.RELEASE_VERSION }}"
    - name: Checkout repository
      uses: actions/checkout@v5
      with:
        fetch-depth: 1
    - name: Create GitHub release
      id: release
      if: github.event.inputs.create_release == 'true' || github.event_name == 'push'
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ env.RELEASE_VERSION }}
        release_name: ${{ env.RELEASE_VERSION }}