name: Release

on:
  push:
    tags:
    - 0.*
    - 1.*

jobs:
  binary:
    strategy:
      fail-fast: false
      matrix:
        msystem:
        - 'MINGW32'
        - 'UCRT64'
    runs-on: windows-2022
    defaults:
      run:
        shell: msys2 {0}
    steps:
    - uses: actions/checkout@v4

    - uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.msystem }}
        update: true
        install: >-
          git
        pacboy: >-
          toolchain:p
          glib2:p
          cmake:p

    - name: Pre-build
      run: |
        cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo

    - name: Build
      run: |
        cmake --build build -v

    - name: Create binary archive
      run: |
        cmake --build build -v --target package

    - uses: actions/upload-artifact@v4
      with:
        name: binary-${{ matrix.msystem }}
        path: 'build/rifiuti2-*.zip'
        if-no-files-found: error

  source:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@v4

    - name: Install dependencies
      run: >
        sudo apt-get install -y
        build-essential
        cmake
        ninja-build
        libglib2.0-dev
        libxml2-utils

    - name: Pre-build
      run: |
        cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo

    - name: Create source archive
      run: |
        cmake --build build -v --target package_source

    - uses: actions/upload-artifact@v4
      with:
        name: source
        path: 'build/rifiuti2-*.xz'
        if-no-files-found: error

  release:
    needs:
    - binary
    - source
    runs-on: ubuntu-24.04
    permissions:
      contents: write
      id-token: write
      attestations: write
    environment: release
    steps:
      - name: Get source artifacts
        uses: actions/download-artifact@v4
        with:
          name: source

      - name: Get binary artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: binary-*
          merge-multiple: true

      - uses: actions/attest-build-provenance@v2
        with:
          subject-path: "rifiuti2-*"

      - name: Create release notes fragment
        run: |
          echo ${{ vars.RELEASE_NOTES }} | base64 -d - > notes.md

      - uses: ncipollo/release-action@v1
        with:
          artifacts: 'rifiuti2-*'
          artifactErrorsFailBuild: true
          draft: true
          bodyFile: 'notes.md'

