Jenkins获取当前登录用户

方法一:

import hudson.model.*
def specificCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
println "specificCause: $specificCause" //返回 specificCause: [[_class:hudson.model.Cause$UserIdCause, shortDescription:Started by user 张三, userId:zhangsan1, userName:张三]]
if (specificCause) {
    println("Executed by user $specificCause.userName")  //返回 Executed by user [张三]
    println "specificCause-userid0: ${specificCause.userId[0]}"  //返回 specificCause-userid0: zhangsan1
    println "shortDescription: $specificCause.shortDescription" //返回 shortDescription: [Started by user 张三]
}

方法二:

import hudson.model.*
def job = Jenkins.getInstance().getItemByFullName(env.JOB_BASE_NAME, Job.class)
def build = job.getBuildByNumber(env.BUILD_ID as int)
def userId = build.getCause(Cause.UserIdCause).getUserId()
def user = User.current()
println "userid is $userId" //返回当前登录用户ID “zhangsan”
println "user is $user"  //返回“System”
println User.get('zhangsan') //返回用户zhangsan的显示名称“张三”

方法二:安装插件build user vars

pipeline{
    agent any
    stages{

        stage("任务申请"){
            steps{
                wrap([$class: 'BuildUser']) {
                   script {
                   //获取当前登录用户账户、姓名、邮箱
                   Applier_id = "${BUILD_USER_ID}"
                   Applier_name = "${env.BUILD_USER}"
                   Applier_mail = "${env.BUILD_USER_EMAIL}"
                   }
                   script{
                    
                    println Applier_id   

                   }
                }
            }
        }
        
    }
}
原文地址:https://www.cnblogs.com/dreamer-fish/p/13528294.html