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

pipeline {
    agent any
    options { disableConcurrentBuilds() }
    stages {
        stage('Unit Tests') {
            parallel {
                stage('Unit tests django 1.11') {
                    agent any
                    steps {
                        sh 'tox -rv -e py3-django111-pylint-coverage'
                    }
                    post {
                        always {
                            stash includes:'junit-py3-django111-pylint-coverage.xml', name: 'django111'
                            script {
                                utils = new Utils()
                                utils.publish_coverage('coverage.xml')
                                utils.publish_coverage_native('index.html')
                                utils.publish_pylint('pylint.out')
                            }
                        }
                    }
                }
                stage('Unit tests django 2.2') {
                    agent any
                    steps {
                        sh 'tox -rv -e py3-django22'
                    }
                    post {
                        always {
                            stash includes:'junit-py3-django22.xml', name: 'django22'
                        }
                    }
                }
            }
            post {
                always {
                    unstash 'django111'
                    unstash 'django22'
                    mergeJunitResults()
                }
            }
        }
        stage('Packaging') {
            agent any
            steps {
                script {
                    if (env.JOB_NAME == 'wcs' && env.GIT_BRANCH == 'origin/master') {
                        sh 'sudo -H -u eobuilder /usr/local/bin/eobuilder wcs'
                    } else if (env.GIT_BRANCH.startsWith('hotfix/')) {
                        sh "sudo -H -u eobuilder /usr/local/bin/eobuilder --branch ${env.GIT_BRANCH} --hotfix wcs"
                    }
                }
            }
        }
    }
    post {
        always {
            script {
                utils = new Utils()
                utils.mail_notify(currentBuild, env, 'ci+jenkins-wcs@entrouvert.org')
            }
        }
        success {
            cleanWs()
        }
    }
}
