build user id in declarative pipeline

3,590 views
Skip to first unread message

Russ Cox

unread,
Sep 12, 2017, 3:09:17 PM9/12/17
to Jenkins Users
Hi, I'm using Jenkins to run a bunch of self service jobs and need to be able to determine the user id of the person who ran a job.

We've been doing this with the scripted pipeline up until now by using the BuildUser plugin (quite why this isn't just a default exported variable I don't know, other CI servers do)

        wrap([$class: 'BuildUser']) {
         sh
'echo "${BUILD_USER}"'
         sh
'echo "${BUILD_USER_ID}"'
         sh
'echo "${BUILD_USER_EMAIL}"'
       
}

I can run the following within a step but the variable isn't available to subsequent steps.

      steps {
        wrap([$class: 'BuildUser']) {
         sh 'echo "${BUILD_USER}"'
         sh 'echo "${BUILD_USER_ID}"'
         sh 'echo "${BUILD_USER_EMAIL}"'
        }
        echo "${USER_ID}"

Seems like there must be a way round this but can't find an answer in documentation or google, any help would be great.

Andrew Bayer

unread,
Sep 13, 2017, 7:01:47 PM9/13/17
to jenkins...@googlegroups.com
Put the rest of your steps inside the {}

A.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/4be2dcc6-fc13-4e70-bbd4-96556f3d27d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Russ Cox

unread,
Sep 19, 2017, 4:23:43 AM9/19/17
to Jenkins Users
Cheers, yeah I have it set out like that currently but need to repeat this for each and every stage and post section.

Ideally I could set this variable in the 'environment' section so it's available throughout the script

Russ Cox

unread,
Sep 20, 2017, 4:18:39 AM9/20/17
to Jenkins Users
Found a solution, setting the variable within a 'script' block.
The variable is then available across all stages.

That was a ballache to find

    stage("Checkout") {
      steps {
        git url: "ssh://xxxxxxxxx.git", credentialsId: 'xxxxxxxxx'
        wrap([$class: 'BuildUser']) {
          script {
             USER_ID = "${BUILD_USER_ID}"
          }
        }
        echo "${USER_ID}"
      }
    }
Reply all
Reply to author
Forward
0 new messages