name: Kernel Release

on:
  push:
    tags:
      - v*
      - flavor/*/v*

jobs:
   build:
    name: Build
    runs-on: [self-hosted, build]
    strategy:
      matrix:
        include:
          - build_type: 'generic'
            build_cc: gcc-10
            build_cflags: ''
          - build_type: zen2
            build_cc: gcc-10
            build_cflags: '-march=znver2'
          - build_type: cascadelake
            build_cc: gcc-10
            build_cflags: '-march=cascadelake'
    steps:
    - name: Checkout Sources
      uses: actions/checkout@v2
      with:
          submodules: recursive
    - name: Build Kernel
      run: make
      env:
          PVE_BUILD_TYPE: ${{ matrix.build_type }}
          PVE_BUILD_CC: ${{ matrix.build_cc }}
          PVE_BUILD_CFLAGS: ${{ matrix.build_cflags }}
    - name: Read Build Information
      id: read_build_info
      run: |
        echo "::set-output name=release::$(sed '1q;d' release.txt)"
        echo "::set-output name=dst::$(sed '1q;d' artifacts.txt)"
        echo "::set-output name=hdr::$(sed '2q;d' artifacts.txt)"
        echo "::set-output name=tools::$(sed '3q;d' artifacts.txt)"
    - name: Upload Artifacts
      uses: actions/upload-artifact@v2-preview
      with:
        name: debs-${{ matrix.build_type }}
        path: "*.deb"
   publish:
    name: Publish
    runs-on: [self-hosted]
    needs: build
    steps:
    - name: Checkout Sources
      uses: actions/checkout@v2
    - name: Download Artifacts
      uses: actions/download-artifact@v2
      with:
          path: artifacts
    - name: Display structure of downloaded files
      run: ls -R artifacts
    - name: Format Release Name
      id: format_release
      run: |
        ref="${{ github.ref}}"
        release_name=${ref#"refs/tags/"}
        echo "::set-output name=release::$release_name"
        changelog=$(dpkg-parsechangelog -c 1 -l debian/changelog)
        changelog="${changelog//'%'/'%25'}"
        changelog="${changelog//$'\n'/'%0A'}"
        changelog="${changelog//$'\r'/'%0D'}"
        echo "::set-output name=changelog::$changelog"
    - name: Create Release
      uses: softprops/action-gh-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        name: pve-edge-kernel ${{ steps.format_release.outputs.release }}
        body: ${{ steps.format_release.outputs.changelog }}
        files: |
            artifacts/debs-generic/pve-edge-headers-*.deb
            artifacts/debs-generic/linux-tools-*.deb
            artifacts/debs-generic/pve-kernel-libc-*.deb
            artifacts/debs-*/pve-edge-kernel-*.deb