# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Build & Test

on:
  push:
    paths-ignore:
      - "wiki/**"
      - "**.md"
  pull_request:
    paths-ignore:
      - "wiki/**"
      - "**.md"

env:
  dotnetVersion: "6.0.x"

jobs:
  test:
    name: Test
    strategy:
      fail-fast: true
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 # avoid shallow clone for GitVersion

      - name: Setup .NET Core SDK ${{ env.dotnetVersion }}
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.dotnetVersion }}

      - name: Test
        run: dotnet test src --configuration Release --logger GitHubActions

  build:
    name: Build Non-MUSL
    needs: test
    strategy:
      fail-fast: true
      matrix:
        runtime:
          - win-x64
          - win-arm64
          - linux-x64
          - linux-arm
          - linux-arm64
          - osx-x64
          - osx-arm64
    # Must run on Windows so that version info gets properly set in host EXE. See:
    # https://github.com/dotnet/runtime/issues/3828
    runs-on: windows-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 # avoid shallow clone for GitVersion

      - name: Setup .NET Core SDK ${{ env.dotnetVersion }}
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.dotnetVersion }}

      - name: Publish
        shell: pwsh
        run: ci/Publish.ps1 ${{ matrix.runtime }}

      - name: Upload Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: recyclarr-${{ matrix.runtime }}
          path: publish/${{ matrix.runtime }}/*

  smoke:
    name: Smoke
    needs: build
    strategy:
      fail-fast: false
      matrix:
        include:
          - { os: windows-latest, runtime: win-x64 }
          - { os: ubuntu-latest, runtime: linux-x64 }
          - { os: macos-latest, runtime: osx-x64 }
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Download Artifacts
        uses: actions/download-artifact@v3
        with:
          name: recyclarr-${{ matrix.runtime }}

      - name: Run Smoke Test
        shell: pwsh
        run: ci/SmokeTest.ps1 ./recyclarr

  # NOTE: This is duplicated from the 'build' job. Sadly, reusable workflows cannot be invoked from
  # matrix jobs. See here:
  # https://docs.github.com/en/actions/using-workflows/reusing-workflows#limitations
  musl:
    name: Build MUSL
    needs: test
    strategy:
      fail-fast: true
      matrix:
        runtime:
          - linux-musl-x64
          - linux-musl-arm
          - linux-musl-arm64
    runs-on: windows-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 # avoid shallow clone for GitVersion

      - name: Setup .NET Core SDK ${{ env.dotnetVersion }}
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.dotnetVersion }}

      - name: Publish
        shell: pwsh
        run: ci/Publish.ps1 ${{ matrix.runtime }} -noSingleFile

      - name: Upload Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: recyclarr-${{ matrix.runtime }}
          path: publish/${{ matrix.runtime }}/*

  release:
    name: Release
    needs: [smoke, musl]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 # avoid shallow clone for GitVersion
          # token: ${{ secrets.GITHUB_TOKEN }} # Allows git push

      - name: Install GitVersion
        uses: gittools/actions/gitversion/setup@v0
        with:
          versionSpec: 5.x

      - name: Determine Version
        uses: gittools/actions/gitversion/execute@v0
        id: gitversion

      - name: Download Artifacts
        uses: actions/download-artifact@v3
        with:
          path: publish

      - name: Create Zip Files
        shell: pwsh
        run: ci/CreateZip.ps1 publish

      - name: Extract Changelog
        id: changelog
        uses: ffurrer2/extract-release-notes@v1

      - name: Create Release
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.DEPLOY_PAT }}
        with:
          files: publish-zip/recyclarr-*.zip
          body: ${{ steps.changelog.outputs.release_notes }}
          tag_name: ${{ github.event.create.ref }}
          draft: false
          prerelease: ${{ steps.gitversion.outputs.preReleaseTag != '' }}

  docker:
    name: Docker
    needs: musl
    uses: ./.github/workflows/docker.yml
    secrets: inherit

  # The main purpose of this job is to group all the other jobs together into one single job status
  # that can be set as a requirement to merge pull requests. This is easier than enumerating all
  # jobs in a workflow to ensure they all pass.
  check:
    if: always()
    name: Report Build Status
    needs: [build, smoke]
    runs-on: ubuntu-latest
    steps:
      - name: Check if all jobs succeeded
        uses: re-actors/alls-green@release/v1
        with:
          jobs: ${{ toJSON(needs) }}