9

I'm having trouble getting Jenkins to clone a git repository on BitBucket via SSH. It's failing with the following message:

Building in workspace /var/lib/jenkins/workspace/test
[ssh-agent] Using credentials git (git@bitbucket.org:<user>/<repo>.git)
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Java/JNR ssh-agent
[ssh-agent] Started.
Checkout:test / /var/lib/jenkins/workspace/test - hudson.remoting.LocalChannel@2b619bca
Using strategy: Default
Fetching changes from 1 remote Git repository
Fetching upstream changes from origin
ERROR: Problem fetching from origin / origin - could be unavailable. Continuing anyway
hudson.plugins.git.GitException: Command "git fetch -t origin +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: ssh: connect to host bitbucket.org port 22: Connection refused
fatal: The remote end hung up unexpectedly

Steps taken

  • Create an SSH keypair
  • Add the public key as a deployment key for the repository on BitBucket
  • Install the SSH key and username (have tried both 'git' and my BB account name) in the Jenkins Credentials manager plugin
  • Attempt to clone the repository in the build using a URL in form of

    git@bitbucket.org:<user>/<repo>.git

I've also tried not using the credentials manager and manually installing the keys in /var/log/jenkins/.ssh/, but to no avail.

Any ideas what I'm doing wrong?

user2424511
  • 315
  • 1
  • 3
  • 10
  • Is the clone working from command line? It is possible that Jenkins is running as user "jenkins" which might be different from the user which you are trying to clone... Jenkins must be running as the user "user" for which you uploaded the keys... – Shunya Sep 18 '13 at 11:22
  • Actually, no - it turned out to be a firewall issue. Thanks for the help. – user2424511 Sep 20 '13 at 08:39
  • FWIW copying the keys from my user account to `/var/lib/jenkins/.ssh` helped me with similar issue – austinmarton Dec 19 '16 at 02:27

2 Answers2

8

As @user1562655 suggested out, the clone was failing due to another issue -- in this case the firewall was blocking outgoing ssh on port 22.

The fix was to use port 443 (as the server allows this), and a different BitBucket URL:

ssh://git@altssh.bitbucket.org:443/<user>/<repo>.git

…instead of

ssh://git@bitbucket.org:<user>/<repo>.git

More info on the scheme is available here:

UsetheSSHprotocolwithBitbucket-SSHonPort443

user2424511
  • 315
  • 1
  • 3
  • 10
  • 1
    Also, to clarify: the SSH credentials plugin /was/ working to supply the relevant key to the builder; there's no need to manually install it in jenkins` `~/.ssh`. – user2424511 Sep 23 '13 at 09:28
  • The Atlassian documentation is now located here: https://confluence.atlassian.com/bitbucket/troubleshoot-ssh-issues-271943403.html – meh Nov 27 '18 at 03:31
  • also... not `.git`... end it with a `/`. So example: `ssh://git@altssh.bitbucket.org:443///` – meh Nov 27 '18 at 03:31
  • @user2424511 The provided link has died – James Cazzetta Feb 05 '21 at 08:44
0

I had the same problem, and actually this solution helped me out:

vim ~/.ssh/config

Add these lines and save it.

Host github.com
    Hostname ssh.github.com
    Port 443

Host bitbucket.org
    Hostname  altssh.bitbucket.org
    Port  443

taken from https://gist.github.com/goddoe/5692bfc3cdc374d918a87882963edf36

Macilias
  • 3,279
  • 2
  • 31
  • 43