<p>Is there a way to pass git credentials to a Jenkins agent?</p>
<p>I'm running Jenkins in Kubernetes and have an agent defined via a pod template to carry out specific builds.</p>
<p>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</p>
<p>I have exec'd into the agent to dig a little deeper and it turns out that when the <code>git push</code> command is executed within the pipeline, it's prompted to enter a username <code>Username for 'https://github.com':</code></p>
<p>How can credentials be passed into the agent so it just uses it without prompting?</p>
<p>I've looked at this <a href="https://github.com/github/hub/issues/1644" rel="nofollow noreferrer">https://github.com/github/hub/issues/1644</a> and tried to configure the credential helper as explained <a href="https://git-scm.com/docs/git-credential-store" rel="nofollow noreferrer">here</a> but I'm still getting the prompt.</p>
<p>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.</p>
<p>I'd go with ssh but unfortunately, it's not an option at the moment.</p>
<p>Anyone with any ideas please?</p>

Hammed
<p>Usually you need to store your credentials (GitHub username and Token) in the Credentials store and use them by <code>withCredentials</code> block.</p>
<pre><code>withCredentials([usernamePassword(
  credentialsId: 'MY_GITHUB_USERNAME_PASSWORD_CREDENTIALS',
  passwordVariable: 'TOKEN',
  usernameVariable: 'USER')]) {
  sh &quot;git push https://${USER}:${TOKEN}@github.com/mycompany/myrepo.git mybranch -f&quot;
}

</code></pre>
<p>In case you want to use <a href="https://www.jenkins.io/doc/pipeline/steps/ssh-agent/" rel="nofollow noreferrer">SSH Agent plugin</a>, you can use <code>ssh</code> authentication instead of <code>https</code>. After you add your public key to your GitHub user, this may look like this:</p>
<pre><code>sshagent(credentials: ['MY_GITHUB_SSH_CREDENTIALS']) {
    sh &quot;git push git@github.com:mycompany/myrepo.git mybranch -f&quot;)
}    
</code></pre>
