@Library('eo-jenkins-lib@master') import eo.Utils

pipeline {
    agent any
    environment {
        TMPDIR = "/tmp/${JOB_NAME.take(32)}-${BUILD_NUMBER}"
    }
    stages {
        stage('Unit Tests') {
            steps {
                sh "mkdir ${env.TMPDIR}"
                sh """
python3 -m venv ${env.TMPDIR}/venv/
${env.TMPDIR}/venv/bin/pip install tox
PGPORT=`python -c 'import struct; import socket; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'` pg_virtualenv -o fsync=off ${env.TMPDIR}/venv/bin/tox -r"""
                sh """${env.TMPDIR}/venv/bin/tox -r -e pylint"""
            }
            post {
                always {
                    script {
                        utils = new Utils()
                        utils.publish_coverage('coverage.xml')
                        utils.publish_coverage_native('index.html')
                        utils.publish_pylint('pylint.out')
                    }
                    mergeJunitResults()
                }
            }
        }
        stage('Packaging') {
            steps {
                script {
                    if (env.JOB_NAME == 'barbacompta' && env.GIT_BRANCH == 'origin/master') {
                        sh 'sudo -H -u eobuilder /usr/local/bin/eobuilder barbacompta'
                    }
                }
            }
        }
    }
    post {
        always {
            script {
                utils = new Utils()
                utils.mail_notify(currentBuild, env, 'ci+jenkins-barbacompta@entrouvert.org')
            }
        }
        success {
            cleanWs()
        }
    }
}
