15

I have a Jenkinsfile script that tests for the possibility to perform an SVN merge and then asks the user for the permission to commit the merge.

I would like to know the username that answers the "input" step in order to write it into the commit message.

Is this possibile?

This is what hypothetically I would like to do:

outcome = input message: 'Merge trunk into branch?', ok: 'Merge'
echo "User that allowed merge: ${outcome.user}"

4 Answers 4

25

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.

4
  • 4
    On jenkins v2.128: def submitter = input( message: "Confirm deployment?", submitterParameter: 'submitter' ); echo "Deployment confirmed by: ${submitter}" Jun 20, 2018 at 13:51
  • Was this a question or example code? That you assign the return value of the input call and have it as submitterParameter makes no sense. Jun 20, 2018 at 14:46
  • It is an example code, and yes it does not make sense, thats why I posted it because it works for some unknown reason. If I don't pass the submitterParameter (the value does not matter) it does not work. Thats why I was confused why this answer was accepted. Maybe its a bug or they change this in jenkins. Jun 21, 2018 at 17:43
  • 1
    @AndrzejRehmann's example shows the odd behavior of how return value is handled. Because input is only given message and submitterParameter , the return value is just a String object with the submitter value. If the input method was called in a way that supplied additional parameters, then the returned value would be a Map instead of a String. Jun 11, 2022 at 1:12
4

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.

2
  • It is in the meantime (see my answer) :-) Jan 25, 2017 at 11:02
  • 1
    Very nice, thank you for the update ! Your answer should now be the accepted answer :-)
    – Pom12
    Jan 25, 2017 at 11:11
4

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."
2
  • How is this answer different from the one by StephenKing 2 months before? Jun 28, 2019 at 5:50
  • 1
    I explained in my answer. The difference is that if you're not using other input parameters then the result is not a map, where you can get submitter from feedback.submitter, but instead the submitter is actually the value in the feedback object itself.
    – Kieran
    Jun 28, 2019 at 9:27
1

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}"
}
3
  • 1
    or you whitelist the method instead of turning off the sandbox. Apr 29, 2017 at 7:17
  • using this method you can not get the userid in try block. Jan 2, 2018 at 5:44
  • that would retrieve the user who triggered the build, not necessarily the same that filled the input
    – laffuste
    Nov 12, 2020 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.