How to clone remote git repository through jenkins - linux

I have installed git in the Linux server(Server1) and created a remote git repository in that server1 Now I need to clone the git repository to server2 through Jenkins so I installed Jenkins in the server2, Now Jenkins was hosted in server2.
In Jenkins I have created a freestyle project, In the Repository URL section, I entered the URL of git repo like: git#<server1_ip>:/opt/dev/repo/pals/ui.git
For authentication, I have tried each credential one by one given below:
git repo username and password
git repo username and SSH Private key
Jenkins username and password
Jenkins username and SSH Private key
But I am facing the below error:
Repository URL
git#<server1_ip>:/opt/dev/repo/pals/ui.git
Failed to connect to repository : Command "git ls-remote -h -- git#<server1_ip>:/opt/dev/repo/pals/ui.git HEAD" returned status code 128: stdout: stderr: Permission denied, please try again. Permission denied, please try again. git#<server1_ip>: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

git#<server1_ip>:/opt/dev/repo/pals/ui.git
That is an SSH URL, which means you need to:
register the private key in Jenkins server2 using the Jenkins SSH Credential plugin (try a passphrase-less key, to avoid any issue with SSH agent, for testing)
make sure server1:~git/.ssh/authorized_key has the public key in it.
On server2, you can at least test ssh -i /path/to/private/key git#server1 to make sure that works first. Then test from Jenkins.

Related

Git clone with ssh works in Linux but not in Windows PowerShell

I have a repo at GitHub. When cloning it in Linux using ssh it works fine:
> git clone git#github.com:henrikppersson74/frokenjennnie.git
Cloning into 'frokenjennnie'...
Enter passphrase for key '/home/----/.ssh/id_rsa':
.
.
Reinitialized existing Git repository in /home/*
When doing the same thing in Windows PowerShell it doesn't work:
> git clone git#github.com:henrikppersson74/frokenjennnie.git
Cloning into 'frokenjennnie'...
git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have copied my private and public SSH keys from Linux to my Windows machine and they are stored in my ~/.ssh/ as id_rsa and id_rsa.pub.
It seems to work when I try to access github.com with ssh from PowerShell:
> ssh git#github.com
Enter passphrase for key 'C:\Users\-----/\.ssh\id_rsa':
PTY allocation request failed on channel 0
Hi henrikppersson74! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
This is the same answer I get when doing this in Linux. I guess this means the my SSH-key par is ok?
When working in Eclipse in Windows, using the "Git Repositories" view it works fine to clone the same repo.
Previously I created new SSH keys in the PowerShell and copied the public one to GitHub, but with the same result.
My ~/.ssh/config file lookes like this:
Host github.com
HostName github.com
IdentityFile ~\.ssh\id_rsa
User git
ForwardAgent yes
I am using Git version:
> git --version
git version 2.28.0.windows.1
Unfortunately I get no extra information from using the --verbose flag:
> git clone git#github.com:henrikppersson74/frokenjennnie.git --verbose
Cloning into 'frokenjennnie'...
git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
It doesn't help to add the key to the ssh-agent:
> ssh-add C:\Users\-----\.ssh\id_rsa
Enter passphrase for C:\Users\-----\.ssh\id_rsa:
Identity added: C:\Users\------\.ssh\id_rsa (C:\Users\-----\.ssh\id_rsa)
> git clone git#github.com:henrikppersson74/frokenjennnie.git --verbose
Cloning into 'frokenjennnie'...
git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Could my old keys be cashed somewhere?
Does anyone have any idea about why I am not able to clone my repo in Windows PowerShell? I would be so grateful for some help.
The problem was that my %HOME% environment variable was set to the wrong location. When I changed it back to C:\Users\<userid> it worked like charm. Apparently SSH first looks for a key in %HOME%\.ssh\, then for en entry in %HOME%\.ssh\config\ and last it uses the keys added to the ssh-agent.

Jenkins job fails with git "git clone" using ssh key

I am trying to run python test cases through Jenkins, it has the git clone command with ssh key.
Command : git clone -v ssh://user#host:29418/project folder_to_clone
Getting the error like :
Warning: Permanently added '[host]:29418,[100.64.42.4]:29418' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
But while running locally everything fine, On Jenkins only getting this issue.
You need to add create a shh key using ssh-key , add it to both jenkins credentials and the git repository you are trying to fetch the code from.

Gitlab: While cloning a repo via ssh it keeps asking for password

I have installed gitlab on LINUX(RHEL 6.7). While cloning the repo via ssh it keeps asking for the password for git, however it is working fine by using http:
$ git clone http://MGJV67#il06epclin1.am.mot-solutions.com/MGJV67/NewPROJ.git
Cloning into 'NewPROJ'...
warning: You appear to have cloned an empty repository.
mgjv67#mgjv67-430 MINGW64 /c/TCS/Gitlab/NewPROJ (master)
$ git clone git#il06epclin1.am.mot-solutions.com:MGJV67/NewPROJ.git
Cloning into 'NewPROJ'...
git#il06epclin1.am.mot-solutions.com's password:
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
While you can clone anonymously over http, you can only clone over SSH with a valid SSH identity.
Create an SSH key and add the public key to your Gitlab user account, or add it as a deploy key for your project in case you only need read-only access.

Not able to access git repo from jenkins

I have configured Jenkins on a Linux machine and my git repo is on an another Linux server. But when I try to give the URL of the repo to Jenkins I get the following error.
Failed to connect to repository : Command "git -c core.askpass=true ls-remote -h ssh://user#ip/~/export1 HEAD" returned status code 128:
stdout:
stderr: Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
What could be the cause for this?
I have seen that both the systems' rsa key is present in each other's .ssh/known_hosts folder.
I am able to pull or clone code from the repo to a folder in Jenkins system as well. So why is Jenkins not able to take it?
I have also tried the URL user#ip:/fullpath/to/repo
You need to make sure Jenkins is running as the right user (the one who has the keys in ~/.ssh/id_rsa(.pub)
That user might not be the same as the one used in the ssh url: user#ip means you are connecting to ip using an account which has your public key in ~user/.ssh/authorized_key.
So the Jenkins process must be run by an account which has the private and public key, whose public key is in ~user/.ssh/authorized_key on the git server side.
And that account should have done (only once) an ssh-keyscan -H ip >> ~account/.ssh/known_hosts before any ssh attempt, in order to record the git server ip as a known host.
Does it being a bare repo make any difference or change in the URL?
No. The .git at the end of the bare repo folder is optional when used as an url.
Further to #VonC's reply, you can also use the Credentials plugin to define a set of credentials on your Jenkins master that your Jenkins job uses to access your Git repo. This allows you to run Jenkins itself as a different user from there one that has access to the Git repo.
The main problem was the security of the systems. I hadnt checked the authentication mechanisms on my server. The password authentication to the git server was causing the problem because the jenkins machine tries to directly fire a ls-remote to the path. When you do the same thing on the terminal you will be prompted for a password and then itl accept. When I set the password authentication and UsePAM to no and enabled the RSA authentication, pubkey authentication and authorised key setting to yes in the sshd_config file, and restarted, it was able to access the repo and I dint get this error.

git clone using ssh failed in Windows due to permission issue

I have created a new GIT repository in my server at /home/myuser/.git/project.git.
I found ssh key for git from C:\Users\Toshiba\.ssh\github_rsa.pub & appended with server's authorized_keys file.
when i try to do git clone using ssh it fails as below.
$ git clone ssh://myuser#mysite.net:2888/home/myuser/.git/project.git
Cloning into 'project'...
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Could you please help me in resolving this issue.
If your public/private key doesn't have the standard name C:\Users\Toshiba\.ssh\id_rsa(.pub), but C:\Users\Toshiba\.ssh\github_rsa.pub, then you need an ssh config file
Host mysite
Hostname mysite.net
User myuser
Port 2888
IdentityFile C:\Users\Toshiba\.ssh\github_rsa.pub
That would allow you to do
git clone mysite:/home/myuser/.git/project.git
Test it first wih ssh -Tvvv mysite, and then ssh mysite ls.
Make sure the environment variable %HOME% is defined to C:\Users\Toshiba
You have another example in "SSH error on push to an existing project Permission denied (publickey)"

Resources