2
votes

I am trying to create a tag via Jenkinsfile and followed by pushing it to the remote repository copnnected via ssh.

For reference:

Using single pipeline in Jenkins.

I have webhooks setup in bitbucket which triggers a build in Jenkins whenever I push code. Works if I connect via https.

I already have generated ssh keys stored locally. These keys works when I work on the code (pull/push) via my ide.

Using this same ssh key (thus no change for public key in bitbucket), added the private key in Jenkins via Credentials. The store is called Jenkins, Domain is Global. Refer to screen shot on how my credential is setup.

enter image description here

But when I try to set up the repository via ssh, I get the following error:

Failed to connect to repository : Command "git.exe ls-remote -h -- [email protected]:myname/jenproject.git HEAD" returned status code 128: stdout: stderr: Load key "C:\Windows\TEMP\jenkins-gitclient-ssh145544752032398406.key": invalid format [email protected]: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

enter image description here

It looks like it is not reading from the right place. Could I get some help on this. Do I need further settings? Been following tutorials and video guides and the suggestion was to set up ssh keys which I have. Please advice. Thanks.

1

1 Answers

3
votes

invalid format error in Jenkins can arise if you've incorrectly pasted your private key in Jenkins' Credentials section. Make sure you're pasting the complete content of your private key file as shown below:

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFQAAAAdz
nYr+I/KWGeCBrl+y5kGNkOy68aUC1BMRGecfQ773DQxLVrdvDTrVD3K3fDMKvD3a
70e67IyiWZP7Ti83NvsZNxZ2KmBMc/qh0YsQeyvWvOxOM9IRqWAF63ZNF/ShJv9G
...
...
...
m/VXqS6KmjemlbYbcrGaEkH5eXhT+gW7eQr7oEouHwAAAIEArUSTb8Z6+D3DbMFF
mf9YRulDVHwU5aCFQMCGkI1Kx7h2FQcOgqhmJAlthMOdzlP5wVC6pbBug6sT9f6H
xO8RbjNHwXMDl3QKsHCNcIy0Uj0AAAAHc2FwaWVudAECAwQF
-----END OPENSSH PRIVATE KEY-----

As you can see from the above example, it should include both -----BEGIN OPENSSH PRIVATE KEY----- and -----END OPENSSH PRIVATE KEY----- part as well. Don't trim anything including dashes (-)

After the above changes, if you're getting error [email protected]: Permission denied (publickey), then please ensure that you're running Jenkins with the same user with which you're able to successfully run the command (ssh -vT [email protected]) from the command line (Git Bash in your case). To do that, follow the instructions explained here

Summarizing it here as well:

1. Run > 'services.msc' (Enter)
2. Select Jenkins service. Right-click and select Properties
3. Click on 'Log On' tab
4. Select user 'X' and provide credentials
5. Restart Jenkins