name: nf-core CI
# This workflow runs the pipeline with the minimal test dataset to check that it completes without any syntax errors
on:
  pull_request:
    branches:
      - dev
  release:
    types: [published]

env:
  NXF_ANSI_LOG: false

concurrency:
  group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"

jobs:
  changes:
    name: Check for changes
    runs-on: ubuntu-latest
    outputs:
      # Expose matched filters as job 'modules' output variable
      tags: ${{ steps.filter.outputs.changes }}
    steps:
      - uses: actions/checkout@v2

      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: "tests/config/tags.yml"

  test_changes:
    name: ${{ matrix.tags }} ${{ matrix.component }} ${{ matrix.profile }} ${{ matrix.NXF_VER }}
    runs-on: ubuntu-20.04
    needs: changes
    if: needs.changes.outputs.tags != '[]'
    strategy:
      fail-fast: false
      matrix:
        NXF_VER:
          - "22.10.1"
          - "latest-everything"
        tags: ["${{ fromJson(needs.changes.outputs.tags) }}"]
        profile: ["docker"] # TODO Add singularity/conda
        component: ["pipeline"] # TODO There aren't any subworkflows or modules tests YET
        exclude:
          - NXF_VER: "latest-everything"
            profile: "conda"
    steps:
      - name: Check out pipeline code
        uses: actions/checkout@v3

      - uses: actions/cache@v2
        with:
          path: /usr/local/bin/
          key: ${{ runner.os }}
          restore-keys: |
            ${{ runner.os }}-nextflow-

      - name: Install Nextflow
        env:
          CAPSULE_LOG: none
        run: |
          wget -qO- get.nextflow.io | bash
          sudo mv nextflow /usr/local/bin/

      - name: Install nf-test
        run: |
          wget -qO- https://code.askimed.com/install/nf-test | bash
          sudo mv nf-test /usr/local/bin/

      - name: Set up Singularity
        if: matrix.profile == 'singularity'
        uses: eWaterCycle/setup-singularity@v5
        with:
          singularity-version: 3.7.1

      - name: Set up miniconda
        if: matrix.profile == 'conda'
        uses: conda-incubator/setup-miniconda@v2
        with:
          auto-update-conda: true
          channels: conda-forge,bioconda,defaults
          python-version: ${{ matrix.python-version }}

      - name: Conda clean
        if: matrix.profile == 'conda'
        run: conda clean -a

      - name: Run nf-test
        run: nf-test test --profile=test,${{ matrix.profile }} tests/${{ matrix.component }}/${{ matrix.tags }}/*.nf.test --junitxml=${{ matrix.tags }}.xml

      - name: Publish Test Report
        uses: mikepenz/action-junit-report@v3
        if: always() # always run even if the previous step fails
        with:
          report_paths: "${{ matrix.tags }}.xml"
