Nikhil
Nikhil

Reputation: 31

Get input approver user name in Jenkins Workflow plugin

I am trying to get userid who approved an "input" step in workflow jenkins groovy script. Below is the sample script

node('node1'){
    stage "test"

    input  message: 'test'
}

In the workflow UI if a person hits "thumbs up" I want to print his userid in the log. I dont see any option to do it.

def cause = currentBuild.rawBuild.getCause(Cause.UserIdCause)
cause.userId 

will print the person who started the build. I have googled this for days but i am not finding anything. Any help here will be greatly appreciated :)

Upvotes: 2

Views: 3782

Answers (2)

Jason Swager
Jason Swager

Reputation: 6501

The JIRA incident referenced u-phoria has been resolved and the fix released.

By setting the submitterParameter to a value, the variable specified by submitterParameter will be populated with the Jenkins user ID that responded to the input field.

Upvotes: 2

u-phoria
u-phoria

Reputation: 364

This Jira issue describes how this is likely to work going forward, however it is still open.

In the meantime, the approach of getting the latest ApproverAction via the build actions API was suggested on #Jenkins IRC recently and should work, note it's not sandbox safe.

Something along the lines of the below for getting the most recent approver:

@NonCPS
def getLatestApprover() {
   def latest = null

   // this returns a CopyOnWriteArrayList, safe for iteration
   def acts = currentBuild.rawBuild.getAllActions()
   for (act in acts) {
       if (act instanceof org.jenkinsci.plugins.workflow.support.steps.input.ApproverAction) {
           latest = act.userId
       }
   }
   return latest
}

Upvotes: 4

Related Questions