properties([
  disableConcurrentBuilds(abortPrevious: true),
  buildDiscarder(logRotator(numToKeepStr: '8', daysToKeepStr: '20')),
  pipelineTriggers([cron('@weekly')])
])

try {
  timeout(time: 3, unit: 'HOURS') {
    buildImage(context: 'jenkins')
    if (env.BRANCH_NAME == 'main' && currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause')) {
      runPod(gpus: 2, gpuType: 'v100') {
        stage('weekly') {
          withEnv([
            "HOME=$WORKSPACE_TMP",
            "OMP_NUM_THREADS=1"
          ]) {
            try {
              sh 'python3.12 -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.12 -c "import torch; print(torch.cuda.current_device())"
              # issue with cache creation in containers: https://github.com/pytorch/pytorch/issues/140765
              TORCHINDUCTOR_CACHE_DIR=/tmp/torchinductor RUN_REGRESSION_SYNTH=1 python3.12 -m pytest -n 8
              '''
            } finally {
              archiveArtifacts artifacts: "uploaded_files/*pt", fingerprint: true
            }
          }
        }
      }
    } else {
      parallel commit: {
        runPod(gpus: 2, gpuType: 'v100') {
          stage('commit') {
            withEnv([
              "HOME=$WORKSPACE_TMP",
              "OMP_NUM_THREADS=1"
            ]) {
              try {
                sh 'python3.12 -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.12 -c "import torch; print(torch.cuda.current_device())"
                # issue with cache creation in containers: https://github.com/pytorch/pytorch/issues/140765
                TORCHINDUCTOR_CACHE_DIR=/tmp/torchinductor RUN_REGRESSION_SYNTH=1 python3.12 -m pytest -n 8
                '''
              } finally {
                archiveArtifacts artifacts: "uploaded_files/*pt", fingerprint: true
              }
            }
          }
        }
      },
      docs: {
        runPod(gpus: 1) {
          stage('docs') {
            withEnv([
              "HOME=$WORKSPACE",
              "OMP_NUM_THREADS=4",
              "PROJECT_NAME=",
              "GITHUB_PATH=plenoptic-org/plenoptic",
              "BRANCH_NAME=${env.BRANCH_NAME}",
              "ISSUE_NUM=${env.BRANCH_NAME.replace('PR-', '')}"
            ]) {
              sh 'python3.12 -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.12 -c "import torch; print(torch.cuda.current_device())"
                # issue with cache creation in containers: https://github.com/pytorch/pytorch/issues/140765
                TORCHINDUCTOR_CACHE_DIR=/tmp/torchinductor RUN_NB=1 NB_EXECUTION_MODE=force make -C docs html O="-j 1"
                mv docs/_build/ $HOME/built-docs/
             '''
             lock('plenoptic_publish') {
                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')
                  withCredentials([string(credentialsId: 'plenoptic-docs-gh-pr-write', variable: 'GITHUB_TOKEN')]) {
                  // 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
              '''
                }
                }
              }
            }
          }
        }
      }
    }
  }
}
finally {
  emailFailure()
}
