pipeline {
  agent { label 'linux' }
  parameters {
    choice(name: 'TARGET', choices: ['staging', 'production'], description: 'Where to deploy')
    booleanParam(name: 'DEPLOY', defaultValue: false, description: 'Run the deploy stage')
  }
  environment {
    APP = 'demo'
    IMAGE_TAG = "${env.BUILD_NUMBER}"
  }
  stages {
    stage('Build') {
      steps { sh 'echo "building $APP:$IMAGE_TAG"' }
    }
    stage('Deploy') {
      when { expression { params.DEPLOY } }
      steps { sh 'echo "deploying $APP:$IMAGE_TAG to $TARGET (DEPLOY is the text $DEPLOY)"' }
    }
    stage('Production check') {
      when {
        allOf {
          expression { params.DEPLOY }
          expression { params.TARGET == 'production' }
        }
      }
      steps { echo 'running the production smoke test' }
    }
  }
}
