#!groovy

pipeline {

    agent {
        label 'cme-eastwatch'
    }

    stages {
        stage('Checkout') {
            steps {
                // Get some code from a GitHub repository
//                git branch: 'ci',
//                    url: 'https://github.com/BerndDoser/raxml-ng.git'
                sh '''
                    git submodule update --init --recursive
                    git submodule add https://github.com/amkozlov/ngtest.git
                '''
            }
        }
        stage('Build') {
            agent {
                dockerfile {
                    reuseNode true
                    filename 'dockerfile-gcc-11'
                    dir 'ci'
                }
            }
            steps {
                sh '''
                    rm -fr build && mkdir -p build && cd build
                    cmake -DCMAKE_BUILD_TYPE=Release -DUSE_MPI=ON .. 2>&1 |tee cmake.out
                    make 2>&1 |tee make.out
                '''
            }
        }
        stage('Benchmark matrix') {
            matrix {
                axes {
                    axis {
                        name 'COMMAND'
                        values 'all',
                               'start'
                    }
                    axis {
                        name 'INPUT'
                        values 'dna8.fa,GTR+G',
                               'prot21.fa,LG+G'
                    }
                    axis {
                        name 'PARALLEL'
                        values '-np 1 build/bin/raxml-ng-mpi --threads 1',
                               '-np 1 build/bin/raxml-ng-mpi --threads 6',
                               '-np 3 build/bin/raxml-ng-mpi --threads 2 --workers 1',
                               '-np 3 build/bin/raxml-ng-mpi --threads 2 --workers 3'
                    }
                }
                stages {
                    stage('Benchmark') {
                        agent {
                            dockerfile {
                                reuseNode true
                                filename 'dockerfile-gcc-11'
                                dir 'ci'
                            }
                        }
                        options {
                            lock('synchronous-matrix')
                        }
                        environment { 
                            PARSTR = sh (returnStdout: true, script: "ci/get_parallel_prefix.sh $PARALLEL").trim()
                            MSA = sh (returnStdout: true, script: "echo $INPUT | cut -f1 -d ','").trim()
                            MODEL = sh (returnStdout: true, script: "echo $INPUT | cut -f2 -d ','").trim()
                            LOCATION = "${COMMAND}___${MSA}___$PARSTR"
                        }
                        steps {
                            sh """
                                mkdir -p out/${LOCATION}
                                mpirun ${PARALLEL} --${COMMAND} --msa ngtest/data/${MSA} --model ${MODEL} --brlen linked \
                                  --prefix out/${LOCATION}/test --redo --seed 1
                            """
                        }
                    }
                }
            }
        }
        stage('Publish benchmarks') {
            steps {
                sh "cd ci && ./generate_html.py ../out"
            }
            post {
                always {
                publishHTML(target : [
                allowMissing: false,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: 'ci',
                reportFiles: 'benchmark.html',
                reportName: 'Benchmark',
                reportTitles: 'Benchmark'])
                }
            }
        }
    }
    post {
        failure {
            mail to: 'alexey.kozlov@h-its.org', subject: "FAILURE: ${currentBuild.fullDisplayName}", body: "Failed."
        }
    }
}
