pipeline {
  agent none
  options {
    disableConcurrentBuilds(abortPrevious: true)
    buildDiscarder(logRotator(numToKeepStr: '8', daysToKeepStr: '20'))
    timeout(time: 2, unit: 'HOURS')
  }
  triggers {
    cron '@weekly'
  }
  stages {
    stage('parallel-jobs') {
      parallel {
        stage('weekly') {
          when {
            triggeredBy 'TimerTrigger'
            branch 'main'
          }
          agent {
            dockerfile {
              dir 'jenkins'
              args '--gpus 2'
            }
          }
          environment {
            HOME = pwd(tmp:true)
            OMP_NUM_THREADS = 4
          }
          steps {
            sh 'python3.11 -m venv --system-site-packages $HOME'
            sh '''#!/bin/bash -ex
            source $HOME/bin/activate
            # need newer version to avoid this: https://github.com/scipy/scipy/issues/16726
            pip install -U pip setuptools
            pip install -U .[test]
            mkdir -p uploaded_files/
            pip freeze
            nvidia-smi
            python3.11 -c "import torch; print(torch.cuda.current_device())"
            RUN_REGRESSION_SYNTH=1 python3.11 -m pytest -n 8
            '''
          }
          post {
            always {
              archiveArtifacts artifacts: "uploaded_files/*pt", fingerprint: true
            }
          }
        }
        stage('commit') {
          when {
            not {
              triggeredBy 'TimerTrigger'
            }
          }
          agent {
            dockerfile {
              dir 'jenkins'
              args '--gpus 2'
            }
          }
          environment {
            HOME = pwd(tmp:true)
            OMP_NUM_THREADS = 4
          }
          steps {
            sh 'python3.11 -m venv --system-site-packages $HOME'
            sh '''#!/bin/bash -ex
            source $HOME/bin/activate
            # need newer version to avoid this: https://github.com/scipy/scipy/issues/16726
            pip install -U pip setuptools
            pip install .[test]
            mkdir -p uploaded_files/
            pip freeze
            nvidia-smi
            python3.11 -c "import torch; print(torch.cuda.current_device())"
            RUN_REGRESSION_SYNTH=1 python3.11 -m pytest -n 8
            '''
          }
          post {
            always {
              archiveArtifacts artifacts: "uploaded_files/*pt", fingerprint: true
            }
          }
        }
        stage('docs') {
          when {
            not {
              triggeredBy 'TimerTrigger'
            }
          }
          agent {
            dockerfile {
              dir 'jenkins'
              args '--gpus 1'
            }
          }
          environment {
            HOME = pwd(tmp:false)
            OMP_NUM_THREADS = 4
            PROJECT_NAME = ""
            GITHUB_PATH = "plenoptic-org/plenoptic"
            GITHUB_TOKEN = credentials("plenoptic-docs-gh-pr-write")
            BRANCH_NAME = "${env.BRANCH_NAME}"
            ISSUE_NUM = "${env.BRANCH_NAME.replace('PR-', '')}"
          }
          steps {
            sh 'python3.11 -m venv --system-site-packages $HOME'
            sh '''#!/bin/bash -ex
              # make sure we have the git tags, since we need that to correctly version plenoptic
              git fetch --tags
              source $HOME/bin/activate
              # need newer version to avoid this: https://github.com/scipy/scipy/issues/16726
              pip install -U pip setuptools
              pip install .[docs]
              python3.11 -c "import torch; print(torch.cuda.current_device())"
              RUN_NB=1 NB_EXECUTION_MODE=force make -C docs html O="-T -j $PARALLEL"
              mv docs/_build/ $HOME/built-docs/
           '''
            lock('plenoptic_publish') {
              script {
                def scm = scmGit(branches: [[name: 'refs/heads/main']], userRemoteConfigs: [[credentialsId: 'github-jenkins', url: 'https://github.com/plenoptic-org/plenoptic-documentation.git']])
                dir(path: 'docs') {
                  checkout(changelog: false, poll: false, scm: scm)
                  sh """#!/bin/bash -ex
                if [[ "\$BRANCH_NAME" =~ PR ]]; then
                   out_dir="pulls/\$ISSUE_NUM"
                elif [[ "${env.TAG_NAME}" != null ]]; then
                   out_dir="tags/${env.TAG_NAME}"
                else
                   out_dir="branch/\$BRANCH_NAME"
                fi
                echo \$GITHUB_PATH > docs/\$PROJECT_NAME/.gh_path
                DOCS_DIR=docs/\$PROJECT_NAME/\$out_dir
                rm -rf \$DOCS_DIR
                mkdir -p \$DOCS_DIR
                cp -rp \$HOME/built-docs/html/* \$DOCS_DIR
                # using -f here makes sure we add the index.html files, which are included in workshops .gitignore (since some are auto-generated)
                git add -A -f --verbose docs/\$PROJECT_NAME
                GIT_COMMITTER_EMAIL="jenkins@flatironinstitute.org" GIT_COMMITTER_NAME="Flatiron Jenkins" git commit --author='Flatiron Jenkins <jenkins@flatironinstitute.org>' --allow-empty -m "Generated documentation for \$DOCS_DIR" -m '${env.BUILD_TAG}'
              """
                  gitPush(gitScm: scm, targetBranch: 'main', targetRepo: 'origin')
                  // this being single quotes is VERY IMPORTANT. otherwise the
                  // GITHUB_TOKEN value would be exposed, see
                  // https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#interpolation-of-sensitive-environment-variables
                  sh '''#!/bin/bash -ex
                if [[ "$BRANCH_NAME" =~ PR ]]; then
                   DOCS_DIR=docs/$PROJECT_NAME/pulls/$ISSUE_NUM

                   # figure out if we've already posted to the PR
                   comments=$(curl -L \
                       -H "Accept: application/vnd.github+json" \
                       -H "Authorization: Bearer $GITHUB_TOKEN" \
                       -H "X-GitHub-Api-Version: 2022-11-28" \
                       https://api.github.com/repos/$GITHUB_PATH/issues/$ISSUE_NUM/comments)

                   if [[ -z $(echo $comments | jq ".[].body" | grep "Documentation built by flatiron-jenkins") ]]; then
                       curl -L \
                         -X POST \
                         -H "Accept: application/vnd.github+json" \
                         -H "Authorization: Bearer $GITHUB_TOKEN" \
                         -H "X-GitHub-Api-Version: 2022-11-28" \
                         https://api.github.com/repos/$GITHUB_PATH/issues/$ISSUE_NUM/comments \
                         -d \'{"body":"Documentation built by flatiron-jenkins at http://docs.plenoptic.org/\'$DOCS_DIR\'"}\'
                   fi
                fi
              '''
                }
              }
            }
          }
        }
      }
    }
  }
  post {
    failure {
      emailext subject: '$PROJECT_NAME - Build #$BUILD_NUMBER - $BUILD_STATUS',
        body: '''$PROJECT_NAME - Build #$BUILD_NUMBER - $BUILD_STATUS

Check console output at $BUILD_URL to view full results.

Building $BRANCH_NAME for $CAUSE
$JOB_DESCRIPTION

Changes:
$CHANGES

End of build log:
${BUILD_LOG,maxLines=200}
''',
	      recipientProviders: [
		    [$class: 'DevelopersRecipientProvider'],
	    ],
	      replyTo: '$DEFAULT_REPLYTO'
    }
  }
}
