// Jenkinsfile for jenkins.ipr.univ-rennes1.fr (Institut de Physique de Rennes)
pipeline {
    agent {label 'alambix_agent'}
    environment {
        IPRBENCH_VENV_PATH = "${WORKSPACE}/iprbench.venv"
    }
    stages {
        stage('setup') {
            steps {
                echo 'setting up itinv test environment'
                sh """#!/bin/bash
                python3 -m venv ${IPRBENCH_VENV_PATH} &&
                source ${IPRBENCH_VENV_PATH}/bin/activate &&
                pip install --upgrade pip &&
                pip install --upgrade setuptools &&
                pip install .
                """
            }
        }
        stage('testing') {
            steps {
                sh """#!/bin/bash
                set -o errexit
                source ${IPRBENCH_VENV_PATH}/bin/activate &&
                python -m unittest
                """
            }
        }
        stage('testing (tests that only work on ipr cluster nodes)') {
            steps {
                sh """#!/bin/bash
                set -o errexit
                source ${IPRBENCH_VENV_PATH}/bin/activate &&
                python -m unittest ./ci/test_iprcluster.py
                """
            }
        }
        stage('releasing iprbench...') {
            steps {
                // see https://www.jenkins.io/doc/book/pipeline/jenkinsfile/
                // graffy created the credential 4e327c3e-8b22-462b-8294-199402e22e7c from jenkin's interface in 2020. This credential is named maco-tester, stores a private ssh key that is allowed access to ssh maco@store.ipr.univ-rennes1.fr. This maco-tester credential belongs to global domain and Jenkins store.
                withCredentials([sshUserPrivateKey(credentialsId: '4e327c3e-8b22-462b-8294-199402e22e7c', keyFileVariable: 'MACO_AT_STORE_KEY_FILE_PATH', passphraseVariable: '', usernameVariable: '')]) {
                    // MACO_AT_STORE_KEY_FILE_PATH stores the path of the public key file to access maco@store.ipr.univ-rennes1.fr
                    sh """
                    git archive -o fr.univ-rennes.ipr.iprbench.tar.gz HEAD &&
                    scp -i ${MACO_AT_STORE_KEY_FILE_PATH} fr.univ-rennes.ipr.iprbench.tar.gz maco@store.ipr.univ-rennes1.fr:/mnt/store.ipr/InstallProgs/ipr/ipr-packages
                    """
                }
            }
        }
    }
    post
    {
        // always, success, failure, unstable, changed
        failure
        {
            mail bcc: '', body: "Example
Project: ${env.JOB_NAME} 
Build Number: ${env.BUILD_NUMBER} 
Build URL: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "CI build failed for ${env.JOB_NAME}", to: "guillaume.raffy@univ-rennes1.fr";
        }
        cleanup {
            cleanWs()
        }
    }
}