Get username logged in Jenkins from Jenkins Workflow (Pipeline) Plugin

后端 未结 10 622
忘了有多久
忘了有多久 2020-12-25 12:36

I am using the Pipeline plugin in Jenkins by Clouldbees (the name was Workflow plugin before), I am trying to get the user name in the Groovy script but I am not able to ach

相关标签:
10条回答
  • 2020-12-25 13:01
    //Below is a generic groovy function to get the XML metadata for a Jenkins build.
    //curl the env.BUILD_URL/api/xml parse it with grep and return the string
    //I did an or true on curl, but possibly there is a better way
    //echo -e "some_string \c" will always return some_string without \n char     
    //use the readFile() and return the string
    def GetUserId(){
     sh """
     /usr/bin/curl -k -s -u \
     \$USERNAME:\$PASSWORD -o \
     /tmp/api.xml \
     \$BUILD_URL/api/xml || true 
    
     THE_USERID=`cat /tmp/api.xml | grep -oP '(?<=<userId>).*?(?=</userId>)'`
     echo -e "\$THE_USERID \\c" > /tmp/user_id.txt                               
     """
    def some_userid = readFile("/tmp/user_id.txt")
    some_userid
    }
    
    0 讨论(0)
  • 2020-12-25 13:09
    def jobUserId, jobUserName
    //then somewhere
    wrap([$class: 'BuildUser']) {
        jobUserId = "${BUILD_USER_ID}"
        jobUserName = "${BUILD_USER}"
    }
    //then
    println("Started By: ${jobUserName}")
    

    We were using this plugin : Build User Vars Plugin. More variables are available.

    0 讨论(0)
  • 2020-12-25 13:09

    Edit: I re-read the question - the below only gets you the user running the build (which technically is often more interesting), not the one triggering the build in the frontend (be it REST-API or WebUI). If you have Jenkins impersonation enabled, then I believe the result should be equivalent, otherwise this will only get you the user who owns the jenkins agent on the build machine.

    Original answer:

    Another way would be to

    sh 'export jenkins_user=$(whoami)'
    

    Downside: Linux-dependent, difficult to port across multiple agents in a single build (but then, the auth context may be different on each slave)

    Upside: No need to install plugins (which on shared/large Jenkins instances can be tricky)

    0 讨论(0)
  • 2020-12-25 13:10

    This works for me without the Build User plugin:

    // get first entry of JSONArray
    def buildCause = currentBuild.getBuildCauses()[0]
    def buildPrincipal = [type:"unknown", name:""]
    
    if (buildCause._class ==~ /.+BranchEventCause/) {
      def branchCause = currentBuild.getRawBuild().getCause(jenkins.branch.BranchEventCause)
      buildPrincipal = [type:"branch",name:buildCause.shortDescription]
    
    } else
    if (buildCause._class ==~ /.+TimerTriggerCause/) {
      def timerCause = currentBuild.getRawBuild().getCause(hudson.triggers.TimerTrigger.TimerTriggerCause)
      buildPrincipal = [type:"timer", name:"Timer event"]
    
    } else
    if (buildCause._class ==~ /.+UserIdCause/) {
      def buildUserCause = currentBuild.getRawBuild().getCause(hudson.model.Cause.UserIdCause)
      buildPrincipal = [type:"user", name:buildCause.userId]
    
    } else
    // ... other causes
    
    0 讨论(0)
提交回复
热议问题