We have a pipeline job that performs a disaster recovery. I’m trying to find a way to compare the user name that started the build to the user name that approved the input. It’s a form of a “4 eyes” check.
I figured out getting the build username but does anyone know a way I can get the username that approved the input question?
I have looked online and tries submitter options but that really did not give me the username.
I don’t know how to find the approver’s username thanks to the API. That’s why I’ll propose you a dirty and flawed workaround, ask the user to input his user name.
You should be able to retrieve the user who approved an input in a Jenkins pipeline using the input step’s submitter parameter. Here’s an untested example:
pipeline {
agent any
stages {
stage('Ask for approval') {
steps {
input message: 'Do you approve?', submitter: 'user123'
}
}
stage('Check approval') {
steps {
script {
def buildUser = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause).getUserName()
def approvalUser = input(id: 'approval', message: 'Approved by?', submitterParameter: 'approver')
if (buildUser == approvalUser) {
echo 'User who started the build is the same as the user who approved the input'
} else {
echo 'User who started the build is different from the user who approved the input'
}
}
}
}
}
}
In this example, we first retrieve the user who started the build using the Cause$UserIdCause class. Then, we use the input step’s submitterParameter parameter to prompt the user to input their username as the approver. Finally, we compare the two usernames to see if they match.
I gave the value of ‘user123’ as an example, assuming that the submitter parameter needs to be set to the username of the person who approved the input in your pipeline job.
In the actual code, you would replace ‘user123’ with the appropriate value that represents the username of the person who approved the input. You can retrieve this value by using the input step in your pipeline, which sets the submitter parameter to the username of the person who approved the input.