#!groovy
def MAVEN_IMAGE = 'maven:3.8.2-openjdk-11-slim'

pipeline {
  // agent any

  agent {
        docker {
      image "${MAVEN_IMAGE}"
        }
  }

  environment {
      BRANCH_NAME = env.GIT_BRANCH.replace('master/', '')
  }

  stages {
    stage('Build') {
      steps {
          // dir('messaging-app') {
          sh 'mvn clean verify'
      // }
      }
    }

    stage('Static Code Analysis') {
      steps {
        withSonarQubeEnv('cicd') {
          sh 'mvn sonar:sonar'
        }
      }

    // maven:end
    }
    stage('Publish Pacts') {
      steps {
          sh 'mvn pact:publish -Dpact.consumer.version=${GIT_COMMIT} -Dpact.tag=${BRANCH_NAME}'
      }
    }
    stage('Check Pact Verifications') {
      steps {
        sh 'cp files/cacert.pem /tmp'
        withEnv(['SSL_CERT_FILE=/tmp/cacert.pem']) {
          sh 'curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.88.3/pact-1.88.3-linux-x86_64.tar.gz'
          sh 'tar xzf pact-1.88.3-linux-x86_64.tar.gz'
            dir('pact/bin') {
              sh "./pact-broker can-i-deploy --retry-while-unknown=12 --retry-interval=10\
                  -a messaging-app -b https://pact.bayhan.ca -e ${GIT_COMMIT}"
            }
        }
      }
    }
    stage('Deploy') {
      when {
        branch 'junit5'
      }
      steps {
        echo 'Deploying to prod now...'
      }
    }
  }
}
