我在Jenkins管道中有一个要求,在成功构建之后我需要用户输入才能继续或中止。开发人员仅用于部署到Dev环境,Ops人员可以在QA和Prod上进行。
我的问题是,如果我作为开发人员登录Jenkins并在构建后执行QA或Prod的构建和部署,那么它应该显示一条消息,因为未授权在QA或Prod上进行部署。开发团队将通知Ops团队进行进一步的部署。 Ops人员将使用他们的凭证登录,当他们触发已部署或中止部署到QA或Prod时,它应该发生。
我的问题是如何获取Jenkins的当前登录用户,以便我可以验证用户并继续部署。
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'
}
}
}
}
答案 0 :(得分:2)
您需要以下代码:
wrap([$class: 'BuildUser']) {
def user = env.BUILD_USER_ID
}
注意:假设您已安装https://wiki.jenkins-ci.org/display/JENKINS/Build+User+Vars+Plugin