Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Bitbucket repo over SSH after install on macOS view homebrew

I'm having a frustrating time getting this set up. Currently my Bitbucket account is having a problem connecting over HTTPS and so I'm trying to connect to my repo using SSH but have encountered problems.

This is my first time using Jenkins so bear with me. I installed Jenkins using homebrew which was very simple.

  1. I let Jenkins install the recommended plugins which included git and ssh plugins and created a password for the admin user.
  2. I generated a new SSH key on my machine using the method described here, added it as an Access Key to my Bitbucket repo, and confirmed that it worked by cloning from the terminal.
  3. I added the SSH keys to Jenkins and it picked up the public key from my ~/.ssh folder
  4. I created a new Freestyle project in Jenkins and added new git repository by supplying the [email protected]:myrepo.git URL.
  5. I instructed it to use my SSH key from the dropdown as credentials.

My efforts were rewarded with this error:

Failed to connect to repository : Command "git ls-remote -h [email protected]:myrepo.git HEAD" returned status code 128: stdout: stderr: Permission denied (publickey). fatal: Could not read from remote repository.

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

Some Possible Leads

I've heard it said that Jenkins creates a new user on your machine and that the reason the SSH key might not work is because the jenkins user does not have access to it. I have not found any evidence of it creating a user and have even heard that it does not create a user, maybe this was old behaviour.

I've tried switching to this Jenkins users as suggested here but to no avail.

Any help would be appreciated.

Edit: One additional thing I've heard (somewhere) is that it might work better if your SSH keys were created without a passphrase. Sounds like it would be less secure in that case so I'm not sure if I should try that.

like image 830
TylerJames Avatar asked Jan 30 '26 20:01

TylerJames


1 Answers

Okay I just found out what was going on.

Even though I added my SSH key to Jenkins and it was accessible via the Credentials dropdown in the Source Code Management section of my project the ssh-agent on my machine (which Jenkins uses) didn't know about my keys and I needed to add them.

Solution:

On the machine where Jenkins is installed:

  1. Open up a terminal
  2. Enter: ssh-add -l
  3. If you don't see your keys listed (mine said "The agent has no identities.") then you'll need to add them. Enter: ssh-add <path-to-key> (the default path is usually something like ~/.ssh/id_rsa)
  4. If you have a passphrase for your key you'll need to enter it here.
  5. Now go back to your Jenkins project, add your repo URL, then select your credentials and it should work

Hope that helps someone avoid wasting their time like I did.

like image 167
TylerJames Avatar answered Feb 01 '26 11:02

TylerJames