Using the execute shell command in Jenkins to run

2020-07-22 09:36发布

问题:

I need to run a bash script that periodically deletes old git branches. I am having trouble finding a way to connect to the git repo via the execute shell option.

Currently I am using cygwin in order to run git commands. Here is what I have in execute shell:

#!c:\cygwin64\bin\bash --login
git ls-remote git@10.1.1.126:/external-web/collette-com.git 

This command is throwing the following error.

[Delete Branches] $ c:\cygwin64\bin\bash --login 

C:\Users\tbraga\AppData\Local\Temp\hudson5750784484659728632.sh
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Build step 'Execute shell' marked build as failure

I have tried running this command in the command line and am prompted for a password. Could this be the issue?

I have the git plugin configured within Jenkins and the connection works perfectly when using Source Code Management Git.

Any suggestions on how to make this connections work in the execute shell field would be greatly appreciated.

回答1:

I solved this problem by passing my credentials to my execute shell script through the Credentials Binding Plugin in Jenkins



回答2:

It's simple enough to create an SSH key associated with your user.

Try here : https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html

Put keys under %userprofile%/.ssh and try running it again.

You can also use the same credentials used in your Jenkins configuration



回答3:

I use SSH keys for auth to Github and had this same issue. My Jenkins configuration has EC2 slaves, so the default SSH key on the machine wasn't correct for Github.

I fixed it with the SSH Agent Plugin. In the job, enable the "SSH Agent" setting and choose the stored SSH key for Github authentication. It should be the same one selected for the Git-SCM configuration used to clone the repo.