How to know which user answered a Jenkins-Pipeline input step?

浪尽此生 提交于 2019-12-05 02:06:55

The input step got an optional submitterParameter, which allows to specify the key of the returned Map that should contain the user who's submitting the input dialog:

If specified, this is the name of the return value that will contain the ID of the user that approves this input.
The return value will be handled in a fashion similar to the parameters value.
Type: String

This looks then as follows:

def feedback = input(submitterParameter: 'submitter', ...)
echo "It was ${feedback.submitter} who submitted the dialog."

P.S: If anybody is interested in a full-fledged code snippet returning the user both for positive and negative feedback to the dialog (and timeout as well), I kindly point to our pipeline library.

Pom12

It is not currently possible, for now only entry parameters are returned in the input step answer, as mentionned in source code :

// TODO: perhaps we should return a different object to allow the workflow to look up
// who approved it, etc?
switch (mapResult.size()) {
case 0:
    return null;    // no value if there's no parameter
case 1:
    return mapResult.values().iterator().next();
default:
    return mapResult;
}

If you'd like to restrict which user(s) can approve the input step, you can however use the submitter parameter, e.g. :

input message: 'Approve ?', submitter: 'authorized-submitter'

EDIT

Since January 2017 it is now possible to request additional parameters to be sent. Please see StephenKing answer above.

If you are not asking for any parameters on the input, then adding the submitterParameter kind of worked. It didn't add it as a parameter on the return object, instead, it turned the returned object into a string with the username in it.

def feedback = input(submitterParameter: 'submitter')
echo "It was ${feedback} who submitted the dialog."

You can do this for exceptions if you turn off the groovy-sandbox:

try {
 'Deploy to production?'

 node {
  sh 'echo deploying'
  }
} catch(e) {
  def user = e.getCauses()[0].getUser()
   echo "Production deployment aborted by:\n ${user}"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!