Getting Credentials of Current User in Jenkins Pipeline Script

Viewed 1184

I have a job that needs to be triggered manually by a logged-in LDAP account. The job contains a step that syncs a git repo, in which the same LDAP account is used for git credentials.

Is it possible to get the credentialsId of the person who is currently logged in and is triggering a job? I'm imagining code like this:

def credential = getCurrentUserCredential() // ???
git credentialsId: credential, url: 'git.repo'
1 Answers

This could work for you

BUILD_CAUSE_JSON=$(curl --silent ${BUILD_URL}/api/json | tr "{}" "\n" | grep "Started by")
BUILD_USER_ID=$(echo $BUILD_CAUSE_JSON | tr "," "\n" | grep "userId" | awk -F\" '{print $4}')
BUILD_USER_NAME=$(echo $BUILD_CAUSE_JSON | tr "," "\n" | grep "userName" | awk -F\" '{print $4}')
Related