automatix
automatix

Reputation: 14552

How to access a remote Git repository in Jenkins using aliases?

short version

I'm using aliasing to connect my remote Git repositories. It works, but when I add such repository to Jenkins, it cannot access the repository and display the error:

Failed to connect to repository : Command "git -c core.askpass=true ls-remote -h [email protected]:myaccount/myrepository.git HEAD" returned status code 128: stdout: stderr: ssh: Could not resolve hostname github.com-foo: Name or service not known fatal: Could not read from remote repository.

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


detailed issue description

In order to commit/push code as different users I use aliasing:

C:\Users\myusername\.ssh\config

...

#github.com-foo account
Host github.com-foo
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_foo

#github.com-bar account
Host github.com-bar
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_bar

...

Then instead of

git clone [email protected]:myaccount/myrepository.git

I simply use

git clone [email protected]:myaccount/myrepository.git

The .ssh directory is accessible from my local VMs as a shared folder, so also use my credentials on the VMs as well.

Everything works.

Now I installed Jenkins on a VM and want to configure the connection to a remote repository stored on GitHub. What I did:

  1. Credentials: Jenkins -> Credentials -> [link] Global credentials (unrestricted) -> [radio button] Enter directly -> [textarea] Key -> added -> [button] Save.

  2. Git plugin: Jenkins -> Configuration -> [section] Git plugin -- Email and Name set up:

enter image description here

  1. Project Git settings: [project] -> Configuration -> [section] Source Code Management -> [section] Git -- Repository URL and credentials:

enter image description here

As on the screenshot shown, the connection is failing.

Why does it not work and how to get it working, in order to connect a remote Git repository in Jenkins using aliases?

Upvotes: 3

Views: 4441

Answers (1)

Jakuje
Jakuje

Reputation: 26006

You need to give jenkins the ssh configuration. The file

C:\Users\myusername\.ssh\config

is applicable for your user, but you need the alias also for the jenkins user, under which is jenkins running. Find home directory for jenkins user and add file .ssh\config there.

Then you also probably need to copy/create different ssh keys for jenkins in it's home directory, because, your are not accessible from jenkins user.

After this the file assess permissions should be set. It can be easily done by chown -R jenkins /var/lib/jenkins/.ssh.

In the global Jenkins settings the private key can then be set to "From the Jenkins master ~/.ssh":

enter image description here

Upvotes: 2

Related Questions