{#- OZI 0.1
# ozi/templates/github_workflows/release.yml
# Part of the OZI Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#}
  release:
    needs: checkpoint
    runs-on: ubuntu-latest
    concurrency: release
    strategy:
      matrix:
        py:
          - {{ spec.python.support.security|wheel_repr }}
          - {{ spec.python.support.bugfix2|wheel_repr }}
          - {{ spec.python.support.bugfix1|wheel_repr }}
      fail-fast: true
      max-parallel: 1
    outputs:
{%- raw %}
      drafted: ${{ steps.draft.outputs.created }}
{%- endraw %}
    permissions:
      contents: write
      id-token: write
    steps:
    - uses: actions/checkout@v4
      if: matrix.py == '{{ spec.python.support.security|wheel_repr }}'
      with:
{%- raw %}
        token: ${{ secrets.GITHUB_TOKEN }}
{%- endraw %}
        fetch-depth: 0

{% include 'github_workflows/semantic_release.yml.j2' %}

    - name: Pass release draft status to job
      id: draft
      env:
{%- raw %}
        check: ${{ steps.release.outputs.released == 'true' || matrix.py != '{{ spec.python.support.security|wheel_repr }}' }}
      run: echo "created=${{ env.check }}" >> $GITHUB_OUTPUT
{%- endraw %}

    - uses: actions/download-artifact@v4
      if: steps.draft.outputs.created == 'true' && matrix.py != '{{ spec.python.support.security|wheel_repr }}'

    - name: Move artifact
      if: steps.draft.outputs.created == 'true' && matrix.py != '{{ spec.python.support.security|wheel_repr }}'
{%- raw %}
      run: cp -a ${{ matrix.py }}/. ./
{%- endraw %}

    - name: Set version
      if: steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true'
      id: version
      run: echo "tag=$(git describe --tags --abbrev=0)" >> $GITHUB_OUTPUT

    - name: Set up Python {{ spec.python.support.security }}
      if: matrix.py == '{{ spec.python.support.security|wheel_repr }}'
      uses: actions/setup-python@v5
      with:
        cache: 'pip'
        python-version: {{ spec.python.support.security }}

    - name: Set up Python {{ spec.python.support.bugfix2 }}
      if: matrix.py == '{{ spec.python.support.bugfix2|wheel_repr }}'
      uses: actions/setup-python@v5
      with:
        cache: 'pip'
        python-version: {{ spec.python.support.bugfix2 }}

    - name: Set up Python {{ spec.python.support.bugfix1 }}
      if: matrix.py == '{{ spec.python.support.bugfix1|wheel_repr }}'
      uses: actions/setup-python@v5
      with:
        cache: 'pip'
        python-version: {{ spec.python.support.bugfix1 }}
{%- raw %}
    - name: Install ${{ matrix.py }} build dependencies
{%- endraw %}
      if: steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true'
      run: |
          python -m pip install --upgrade pip
          python -m pip install --user --upgrade build
          python -m pip install --user --upgrade pyc_wheel
          python -m pip install --user --upgrade sigstore
          python -m pip install --user --upgrade meson
          python -m pip install --user --upgrade setuptools_scm
          python -m pip install --user --upgrade mesonpep517
          python -m pip install --user --upgrade ninja
          python -m pip install --user --upgrade pip-tools

    - name: Configure git repository
      if: matrix.py == '{{ spec.python.support.security|wheel_repr }}'
      run: |
          git config --global user.email "noreply@oziproject.dev"
          git config --global user.name "OZI Packaging"
          rm -rf .git/COMMIT_EDITMSG
          mkdir sig

{%- raw %}
    - name: Build ${{ matrix.py }} wheel
{%- endraw %}
      id: wheel
      if: steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true'
      run: |
        meson setup build
        python -m build -w
{% include 'github_workflows/pyc_wheel.yml.j2' %}
{%- raw %}
        mv dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}-py3-none-any.whl dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}-${{ matrix.py }}-none-any.whl
{%- endraw %}

{%- raw %}
    - name: Build and sign sdist
{%- endraw %}
      id: sdist
      if: matrix.py == '{{ spec.python.support.bugfix1|wheel_repr }}'
      run: |
        meson setup build --reconfigure
{%- raw %}
        pip install dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}-${{ matrix.py }}-none-any.whl
{%- endraw %}
        meson dist -C build --formats gztar --no-tests
{%- raw %}
        mv build/meson-dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}.tar.gz dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}.tar.gz 
        sigstore sign --output-directory sig dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}.tar.gz
{%- endraw %}

{% include 'github_workflows/sigstore.yml.j2' %}

    - name: Move dist-info file
      if: steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true'
{%- raw %}
      run: mv dist/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}.dist-info sig/${{ github.event.repository.name }}-${{ steps.version.outputs.tag }}-${{ matrix.py }}.dist-info
{%- endraw %}

    - uses: actions/upload-artifact@v4
      if: matrix.py == '{{ spec.python.support.security|wheel_repr }}' && (steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true')
      with:
        name: '{{ spec.python.support.bugfix2|wheel_repr }}'
        path: |
          !build/
          ./

    - uses: actions/upload-artifact@v4
      if: matrix.py == '{{ spec.python.support.bugfix2|wheel_repr }}' && (steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true')
      with:
        name: '{{ spec.python.support.bugfix1|wheel_repr }}'
        path: |
          !build/
          !{{ spec.python.support.bugfix2|wheel_repr }}/
          ./

    - uses: actions/upload-artifact@v4
      if: matrix.py == '{{ spec.python.support.bugfix1|wheel_repr }}' && (steps.release.outputs.released == 'true' || steps.draft.outputs.created == 'true')
      with:
        path: |
          !build/
          !{{ spec.python.support.bugfix1|wheel_repr }}/
          ./
