4

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 -- git@bitbucket.org:myname/jenproject.git HEAD" returned status code 128: stdout: stderr: Load key "C:\Windows\TEMP\jenkins-gitclient-ssh145544752032398406.key": invalid format git@bitbucket.org: 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.

kar
  • 4,791
  • 12
  • 49
  • 74

1 Answers1

4

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 git@bitbucket.org: 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 git@bitbucket.org) 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
Technext
  • 7,887
  • 9
  • 48
  • 76
  • Nice I was trimming those and adding them back did make the error disappear. But now getting: git@bitbucket.org: Permission denied (publickey) Am I also supposed to add public key into Jenkins? – kar Apr 19 '20 at 13:30
  • Have you loaded your public key in BitBucket? Check this [link](https://confluence.atlassian.com/bitbucket/troubleshoot-ssh-issues-271943403.html) – Technext Apr 19 '20 at 13:33
  • Wil have a look at the link but I have uploaded the public key to Bitbucket. Cos its that same key that works for me on ide. Plus I just tried adding that same key to bitbucket. It warns saying the key already exists. – kar Apr 19 '20 at 13:37
  • On the host where Jenkins is running, does this command works fine? `ssh -vT git@bitbucket.org` – Technext Apr 19 '20 at 14:16
  • I don't have a terminal on Jenkins. Tried donwloading that plugin and it keeps getting stalled after the install and unable to type anything on terminal... Looking into it. – kar Apr 19 '20 at 15:42
  • [Git Bash](https://git-scm.com/download/win) can help. You can download that and try. Besides, for test purpose, you can also create a `Freestyle project` in Jenkins and then under `Build` > `Execute shell`, try the command: `ssh -vT git@bitbucket.org` – Technext Apr 19 '20 at 15:51
  • I have git bash. Running this command there gives a bunch of debug statements. It does say Found key in /c/Users/username/.ssh/known_hosts:1 . But wonder if this statement points to issue with shell being disabled -> You can use git or hg to connect to Bitbucket. Shell access is disabled – kar Apr 19 '20 at 16:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212018/discussion-between-technext-and-kar). – Technext Apr 19 '20 at 16:17
  • 1
    Thanks for the hint! I used Putty in Win10 to generate the key pairs. In the menu there is "Conversions" > Export OpenSSH key and this gave me the desired format which worked in Jenkins right away: -----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-ED..91A TPSi...gH/Q -----END RSA PRIVATE KEY----- – Robi Wan Kenobi Jan 20 '22 at 17:47