name: nf-core CI
# This workflow is triggered on releases and pull-requests.
# It runs the pipeline with the minimal test dataset to check that it completes without any syntax errors
on:
  push:
    branches:
      - dev
  pull_request:
  release:
    types: [published]

jobs:
  test:
    name: Run workflow tests
    # Only run on push if this is the nf-core dev branch (merged PRs)
    if: ${{ github.event_name != 'push' || (github.event_name == 'push' && github.repository == 'nf-core/atacseq') }}
    runs-on: ubuntu-latest
    env:
      NXF_VER: ${{ matrix.nxf_ver }}
      NXF_ANSI_LOG: false
    strategy:
      matrix:
        # Nextflow versions: check pipeline minimum and current latest
        nxf_ver: ['19.10.0', '']
    steps:
      - name: Check out pipeline code
        uses: actions/checkout@v2

      - name: Check if Dockerfile or Conda environment changed
        uses: technote-space/get-diff-action@v1
        with:
          PREFIX_FILTER: |
            Dockerfile
            environment.yml

      - name: Build new docker image
        if: env.GIT_DIFF
        run: docker build --no-cache . -t nfcore/atacseq:1.2.0

      - name: Pull docker image
        if: ${{ !env.GIT_DIFF }}
        run: |
          docker pull nfcore/atacseq:dev
          docker tag nfcore/atacseq:dev nfcore/atacseq:1.2.0

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

      - name: Run pipeline with test data
        run: |
          nextflow run ${GITHUB_WORKSPACE} -profile test,docker

  parameters:
    name: Test workflow parameters
    if: ${{ github.event_name != 'push' || (github.event_name == 'push' && github.repository == 'nf-core/atacseq') }}
    runs-on: ubuntu-latest
    env:
      NXF_VER: '19.10.0'
      NXF_ANSI_LOG: false
    strategy:
      matrix:
        parameters: [--single_end, --skip_trimming, --skip_merge_replicates, --skip_consensus_peaks]
    steps:
      - name: Check out pipeline code
        uses: actions/checkout@v2

      - name: Check if Dockerfile or Conda environment changed
        uses: technote-space/get-diff-action@v1
        with:
          PREFIX_FILTER: |
            Dockerfile
            environment.yml

      - name: Build new docker image
        if: env.GIT_DIFF
        run: docker build --no-cache . -t nfcore/atacseq:1.2.0

      - name: Pull docker image
        if: ${{ !env.GIT_DIFF }}
        run: |
          docker pull nfcore/atacseq:dev
          docker tag nfcore/atacseq:dev nfcore/atacseq:1.2.0

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

      - name: Run pipeline with various parameters
        run: |
          nextflow run ${GITHUB_WORKSPACE} -profile test,docker ${{ matrix.parameters }}

  push_dockerhub:
    name: Push new Docker image to Docker Hub
    runs-on: ubuntu-latest
    # Only run if the tests passed
    needs: test
    # Only run for the nf-core repo, for releases and merged PRs
    if: ${{ github.repository == 'nf-core/atacseq' && (github.event_name == 'release' || github.event_name == 'push') }}
    env:
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
      DOCKERHUB_PASS: ${{ secrets.DOCKERHUB_PASS }}
    steps:
      - name: Check out pipeline code
        uses: actions/checkout@v2

      - name: Build new docker image
        run: docker build --no-cache . -t nfcore/atacseq:latest

      - name: Push Docker image to DockerHub (dev)
        if: ${{ github.event_name == 'push' }}
        run: |
          echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
          docker tag nfcore/atacseq:latest nfcore/atacseq:dev
          docker push nfcore/atacseq:dev

      - name: Push Docker image to DockerHub (release)
        if: ${{ github.event_name == 'release' }}
        run: |
          echo "$DOCKERHUB_PASS" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
          docker push nfcore/atacseq:latest
          docker tag nfcore/atacseq:latest nfcore/atacseq:${{ github.event.release.tag_name }}
          docker push nfcore/atacseq:${{ github.event.release.tag_name }}
