Hi Christian,
I'm doing it in a pipeline that runs various git commands to check out, push and open PRs automatically for some DevOps.
This is how I did:
I've created a bash script git_password_helper.sh with the following content:
#!/bin/sh
exec echo "$GITHUB_PSW"
The in the pipeline, I set the environment to use the GitHub credentials I need:
|
| environment { |
| GITHUB = credentials('user_github_token') |
| } |
|
I use the personal access token instead of user password because it works and it's more secure than storing password on Jenkins credentials.
Then, I have a bash script containing all the git commands I have to run and the trick is to set GIT_ASKPASS to the bash script above:
export GIT_ASKPASS=./scripts/git_password_helper.sh
git ...
git ...
git ...
How does it works?
The pipeline injects into environment variable GITHUB_USR/PWD the user and access token and they are available to the bash script running the git command, then the GIT_ASKPASS instructs git to call the git_password_helper.sh when it needs the user's password instead of prompting ... and the git_password_helper.sh echo the GITHUB_PWD containing the access token stored into Jenkins credentials.
That works for me.
Cheers,
Gianluca.