pipeline {
    agent any
    environment {
       REGISTRY = "registry.local:5000"
       UAT_ENGINE = "pwd-domain:80"
       PROD_ENGINE = "pwd-domain:80"
    }
    stages {
        stage('Verify') {
            steps {
                dir('ch15/lab') {
                    sh 'chmod +x ./ci/00-verify.bat'
                    sh './ci/00-verify.bat'
                }
            }
        }
        stage('Build') {
            steps {
                dir('ch15/lab') {
                    sh 'chmod +x ./ci/01-build.bat'
                    sh './ci/01-build.bat'
                }
            }
        }
        stage('Test') {
            steps {
                dir('ch15/lab') {
                    sh 'chmod +x ./ci/02-test.bat'
                    sh './ci/02-test.bat'
                }
            }
        }
        stage('Push') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'docker-hub-user', usernameVariable: 'hub_user', passwordVariable: 'hub_password')]) {
                    dir('ch15/lab') {
                        sh 'chmod +x ./ci/03-push.bat'
                        sh './ci/03-push.bat'
                        echo "Pushed to http://$REGISTRY/v2/diamol/ch15-timecheck/tags/list"
                        echo "Pushed  to Docker Hub"
                    }
                }
            }
        }
        stage('UAT') {
            steps {
                withCredentials([file(credentialsId: 'docker-ca.pem', variable: 'ca'),
                                 file(credentialsId: 'docker-cert.pem', variable: 'cert'),
                                 file(credentialsId: 'docker-key.pem', variable: 'key')]) {                    
                    dir('ch15/lab') {
                        sh 'chmod +x ./ci/04-uat.bat'
                        sh './ci/04-uat.bat'
                        echo "Deployed to UAT"
                    }
                }
            }
        }
        stage('Await approval') {
            steps {
                input message: 'Deploy to prod?', ok: 'Do it!'
            }
        }
        stage('Production') {
            steps {
                withCredentials([file(credentialsId: 'docker-ca.pem', variable: 'ca'),
                                 file(credentialsId: 'docker-cert.pem', variable: 'cert'),
                                 file(credentialsId: 'docker-key.pem', variable: 'key')]) {                    
                    dir('ch15/lab') {
                        sh 'chmod +x ./ci/05-deploy.bat'
                        sh './ci/05-deploy.bat'
                        echo "Deployed to PROD"
                    }
                }
            }
        }
    }
}