I have a requirement in Jenkins pipeline where in after the successful build I need an user input to proceed or abort. The developers are only intended for deployment into Dev environment and Ops people can do on QA and Prod.
My issue is if I login as dev into Jenkins and execute build and deploy for QA or Prod after build then it should display a message as not authorized to do deployment on QA or Prod. The Dev team will inform Ops team to carry further deployment in pipeline. The Ops people will login with their credential and when they trigger the procced or abort for deployment to QA or Prod it should happen.
My issue how to get current logged user of Jenkins so that I can validate the user and carry on with deployment.
stage('confirmDeploy'){
inputMessage("${TARGET_PLATFORM}")
echo "Deploying release on ${TARGET_PLATFORM} environment"
}
def inputMessage(String inputEnvName) {
timeout(time: 1, unit: 'HOURS') {
input message: "Are you sure to promote this build to ${inputEnvName} env ?", ok: 'deploy'
wrap([$class: 'BuildUser']) {
sh 'echo "${BUILD_USER}"'
if (( "${inputEnvName}" == "qa" && "${BUILD_USER}" == "ops")||( "${inputEnvName}" == "prod" && "${BUILD_USER}" == "ops")){
input message: "Are you sure to promote this build to ${inputEnvName} env ?", ok: 'deploy'}
else if (( "${inputEnvName}" == "dev" && "${BUILD_USER}" =="devuser")){
input message: "Are you sure to promote this build to ${inputEnvName} env ?", ok: 'deploy'}
else {
echo 'Further code will not be executed'echo "Not authorized to carry deployment to the respective environment", ok: 'deploy'
}
}
}
}
you need following code:
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
}
Note: this is assuming that you have https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin installed