name: Kernel Release

on:
  pull_request:
    types: [closed]
    branches:
      - v*
      - flavor/*/v*
  workflow_dispatch:

jobs:
  build:
    name: Build (${{ matrix.debian }})
    runs-on: [self-hosted, '${{ matrix.debian }}']
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }}
    strategy:
      matrix:
        debian: [bullseye]
        include:
          - build_profile: 'generic'
            build_cc: gcc
            build_cflags: ''
    steps:
    - name: Clean Workspace
      run: rm -rf *.deb *.ddeb *.build *.buildinfo *.changes
    - name: Checkout Sources
      uses: actions/checkout@v3
      with:
          submodules: recursive
          path: pve-edge-kernel
    - name: Clean Repository
      run: git -C pve-edge-kernel submodule foreach git clean -ffdx
    - name: Build Kernel
      run: |
        cd pve-edge-kernel
        debian/rules debian/control
        if [ "$PVE_BUILD_PROFILE" != "generic" ]; then
        	debchange -l +$PVE_BUILD_PROFILE -D edge --force-distribution -U -M "Specialization for $PVE_BUILD_PROFILE"
        fi
        debuild --no-lintian -e PVE* -e CCACHE_DIR=/var/cache/ccache --prepend-path=/usr/lib/ccache --jobs=auto -b -uc -us
      env:
          PVE_BUILD_PROFILE: ${{ matrix.build_profile }}
          PVE_KERNEL_CC: ${{ matrix.build_cc }}
          PVE_KERNEL_CFLAGS: ${{ matrix.build_cflags }}
          PVE_ZFS_CC: ${{ matrix.build_cc }}
    - name: Upload Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: ${{ matrix.debian }}-${{ matrix.build_profile }}
        path: "*.deb"
  publish:
    name: Publish
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }}
    needs: build
    steps:
    - name: Checkout Sources
      uses: actions/checkout@v3
    - name: Download Artifacts
      uses: actions/download-artifact@v3
      with:
          path: artifacts
    - name: Delete Debug Symbols
      run: rm -f artifacts/*/*dbgsym*.deb
    - name: Format Release Name
      id: format_release
      run: |
        echo "release=$(scripts/version.sh)" >> $GITHUB_OUTPUT
        changelog=$(dpkg-parsechangelog -c 1 -l debian/changelog)
        echo "changelog<<EOF\n$changelog\nEOF" >> $GITHUB_OUTPUT
    - name: Create Release
      uses: ncipollo/release-action@v1
      with:
        commit: ${{ github.sha }}
        tag: v${{ steps.format_release.outputs.release }}
        body: ${{ steps.format_release.outputs.changelog }}
        token: ${{ secrets.PAT }}
        artifacts: "artifacts/bullseye-generic/pve-headers-*.deb,artifacts/bullseye-generic/pve-kernel-*.deb,artifacts/bullseye-generic/linux-tools-*.deb"
    - name: Release to CloudSmith
      run: |
        pip install --upgrade cloudsmith-cli
        find artifacts/bullseye-generic \
             -name '*.deb' \
             -not -name "*dbgsym*" \
             -exec cloudsmith push deb pve-edge/kernel/debian/bullseye {} --republish \;
      env:
        CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
