Issue
Is there a way to pass git credentials to a Jenkins agent?
I'm running Jenkins in Kubernetes and have an agent defined via a pod template to carry out specific builds.
My challenge is that the build keeps on failing with this error `fatal: could not read Username for 'https://github.com': No such device or address
I have exec'd into the agent to dig a little deeper and it turns out that when the git push
command is executed within the pipeline, it's prompted to enter a username Username for 'https://github.com':
How can credentials be passed into the agent so it just uses it without prompting?
I've looked at this https://github.com/github/hub/issues/1644 and tried to configure the credential helper as explained here but I'm still getting the prompt.
One issue with the credential helper seems to be that you still have to enter your username and password at least once, which I can't do as it's running in automation.
I'd go with ssh but unfortunately, it's not an option at the moment.
Anyone with any ideas please?
Solution
Usually you need to store your credentials (GitHub username and Token) in the Credentials store and use them by withCredentials
block.
withCredentials([usernamePassword(
credentialsId: 'MY_GITHUB_USERNAME_PASSWORD_CREDENTIALS',
passwordVariable: 'TOKEN',
usernameVariable: 'USER')]) {
sh "git push https://${USER}:${TOKEN}@github.com/mycompany/myrepo.git mybranch -f"
}
In case you want to use SSH Agent plugin, you can use ssh
authentication instead of https
. After you add your public key to your GitHub user, this may look like this:
sshagent(credentials: ['MY_GITHUB_SSH_CREDENTIALS']) {
sh "git push [email protected]:mycompany/myrepo.git mybranch -f")
}
Answered By - MaratC
Answer Checked By - Terry (JavaFixing Volunteer)