Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clone not working in jenkins shell script

Tags:

git

shell

jenkins

I am setting up jenkins build pipeline and in one of the build steps, I am trying to git clone a repository.

Build steps(execute shell):

git clone [email protected]:orgname/reponame.git

It fails at this step with

10:14:01 Permission denied (publickey).
10:14:01 fatal: The remote end hung up unexpectedly
10:14:01 Build step 'Execute shell' marked build as failure
10:14:01 Finished: FAILURE

However, while setting up the repo, I have set scm as git and have specified the repository url as git clone [email protected]:orgname/reponame.git and this works perfectly.

Any idea what I might be missing here.

like image 751
Anoop Avatar asked Jun 27 '26 18:06

Anoop


1 Answers

Github uses an ssh public/private keypair for authentication. When you use github, you need to provide your public key. You probably did that once already, following these instructions.

When you generate a keypair, the default behavior is that only the user which generated the keypair has access to it. That means if you generate it as upr, the user jenkins has no access to the keypair needed to connect to github.

To fix this problem, switch to the jenkins user. If you're in unix, you can do this via

sudo su - jenkins

Then follow the keygeneration steps provided by github. For Jenkins to use the key, the simplest approach is to create it with no password.

I've not used it, but a Jenkins plugin like this might allow you to use a password protected keypair.

like image 171
T.D. Smith Avatar answered Jun 29 '26 12:06

T.D. Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!