pipeline {
    agent any

    options {
        disableConcurrentBuilds()
        ansiColor('xterm')
    }

    environment {
        COMPOSER_PROCESS_TIMEOUT = 900
        COMPOSER_HOME = "${HOME}/.composer"

        BUILD_UUID = "${WORKSPACE.tokenize('/').last()}"
        GIT_SHA1 = "${GIT_COMMIT.substring(0,7)}"
    }

    stages {
        stage('PHP 7.1') {
            steps {
                sh '''
                    make PHP_VERSION=7.1 prepare phpunit
                '''
            }
        }
        stage('PHP 7.2') {
            steps {
                sh '''
                    make PHP_VERSION=7.2 prepare phpunit
                '''
            }
        }
    }

    post {
        always {
            echo 'Post actions : Always'
        }
        success {
            echo 'Post actions : Success'

            // Artifacts
            archiveArtifacts allowEmptyArchive: true, artifacts: 'var/logs/*.log', caseSensitive: true, defaultExcludes: true, fingerprint: false, onlyIfSuccessful: false
        }
        unstable {
            echo 'Post actions : Unstable'

            // Mailer notification
            step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '', sendToIndividuals: true])

            // Slack notification
            slackSend channel: '#jenkins', message: "${JOB_NAME} | ${GIT_AUTHOR_NAME} :: ${GIT_SHA1} (${GIT_BRANCH}) (<${BUILD_URL}|Open>)", teamDomain: 'allocine', tokenCredentialId: 'slack'
        }
        failure {
            echo 'Post actions : Failure'
        }
        changed {
            echo 'Post actions : Changed'
        }
        cleanup {
            echo 'Post actions : Clean up'
            sh '''
                make cleanup
            '''
        }
    }
}
