pipeline {
    agent any

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

    environment {
        COMPOSER_PROCESS_TIMEOUT = 900

        BUILD_UUID = "${JOB_BASE_NAME.replaceAll(/[^a-z0-9]/, '')}_${UUID.randomUUID()}"
        GIT_SHA1 = "${GIT_COMMIT.substring(0,7)}"
    }

    stages {
        stage('Install deps') {
            steps {
                echo 'Installing deps...'
            }
        }
        stage('Build') {
            steps {
                echo 'Building...'
                sh '''
                    phing \
                        prepare
                '''
            }
        }
        stage ('Test') {
            steps {
                echo 'Testing...'
                sh '''
                    phing 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 '''
                phing cleanup
            '''
        }
    }
}
