#!/usr/bin/env groovy

pipeline {
    agent any
    options {
        disableConcurrentBuilds()
    }
    stages {
        stage('Build') {
            when {branch 'master'}
            steps {
                script {
                    def packageJson = readJSON file: 'package.json'
                    env.VERSION = packageJson.version
                }
                sh 'docker-compose build --build-arg BUILD_ID=$BUILD_ID --parallel'
            }
        }
        stage('Push') {
            when {branch 'master'}
            environment {
                CREDENTIALS = credentials('docker-registry')
            }
            steps {
                script {
                    def packageJson = readJSON file: 'package.json'
                    env.VERSION = packageJson.version
                }
                sh 'docker login --username $CREDENTIALS_USR --password $CREDENTIALS_PSW $REGISTRY'
                sh 'docker-compose push'
            }
        }
    }
    post {
        unsuccessful {
            setEnv()
            sendEmail()
        }
    }
}