name: Test with Nextflow from source

on:
  workflow_dispatch: # Manual trigger
  schedule:
    # Run at 00:00 UTC on Monday, Wednesday, Friday (2:00 CEST)
    - cron: "0 0 * * 1,3,5"

jobs:
  test-with-nextflow-source:
    runs-on: ubuntu-latest
    env:
      NXF_ANSI_LOG: false

    steps:
      - name: Check out Nextflow
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          repository: nextflow-io/nextflow
          path: nextflow

      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        name: Check out nf-core/tools
        with:
          ref: dev
          path: nf-core-tools

      - name: Set up Python
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
        with:
          python-version: "3.13"
          cache: pip
          cache-dependency-path: nf-core-tools/pyproject.toml

      - name: Set up Java
        uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4
        with:
          distribution: "temurin"
          architecture: x64
          cache: gradle
          java-version: "21"

      - name: Build Nextflow
        run: cd nextflow && make pack

      - name: Move Nextflow to local bin/
        run: |
          mkdir -p $HOME/.local/bin/
          mv nextflow/build/releases/nextflow*dist $HOME/.local/bin/nextflow
          chmod +x $HOME/.local/bin/nextflow
          nextflow -version

      - name: Install nf-core/tools
        run: |
          cd nf-core-tools
          python -m pip install --upgrade pip
          pip install .

      - name: Create new pipeline
        run: nf-core pipelines create -n testpipeline -d "This pipeline is for testing" -a "Testing McTestface"

      - name: Run new pipeline
        run: nextflow run nf-core-testpipeline -profile docker,test --outdir ./results

      - name: Send email on failure
        if: failure()
        uses: dsfx3d/action-aws-ses@v1
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: "eu-west-1"
        with:
          to: ${{ secrets.NEXTFLOW_NIGHTLY_NOTIFICATION_EMAIL_ADDRESS }}
          from: core@nf-co.re
          subject: "Nextflow source test CI failed"
          body: |
            The Nextflow source test CI workflow failed!

            See the failed run here: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
