Issue
I have a jenkins job foo
that works perfectly well. It is authenticated with jenkins_foo_user
. It is able to clone the repo and checkout the right branch.
In my codebase, I have another script - query_github.py
a script used to query Github API to get information such as commits and pull request. To be able to use this API I need an access token.
I want to be able to use the jenkins user access token to authenticate and access the github credentials. How do I do this? Note: Using the Github Enterprise version
Solution
one has to use the credentials api
.
Example
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'jenkins-user',
usernameVariable: 'Foo_USERNAME',
passwordVariable: 'Foo_PASSWORD']]) {
export foo_name=${stds.escape4sh env.Foo_USERNAME}
export foo_pass=${stds.escape4sh env.Foo_PASSWORD}
python foo.py
Inside of foo.py
_name = os.env.get('foo_name')
_pass = os.env.get('foo_pas')
easiest and cleanest way to accomplish the goal!
Note: jenkins-user
should be configured into jenkins user credentials
Answered By - suprita shankar
Answer Checked By - Mildred Charles (JavaFixing Admin)