简体   繁体   中英

jenkins, using ssh in a shell script

update: 2/7 - See answer below

i can't seem to get git/ssh checkouts to work from within a script.

What works:

My jenkins job has an SSH key.

Jenkins checks out (via SSH) things from bitbucket with no problem via the GIT plugin.

What my Jenkins job does is this:

The jenkins job basically does 2 things:

  1. check out a git module with shell scripts (on a linux machine)

  2. Execute the git module with parameters passed (ie: Parameterized Build)

The script does (OR SHOULD) do all the work within the sandbox directory this involves checking out about 8 other GIT modules, selecting branches and such based upon values passed as part of the Paramerized Build.

What I know works and where I see problems

I have the SSH Agent present, and enabled. I have verified that the agent is present, by using the SET command in my script, I see the SSH_AGENT shell variables.

At the end of the jenkins job, I see Jenkins state that it is killing the SSH AGENT.

Jenkins uses the URL ssh://git@someserver.com/path/to/repo.git - as the URL, I am using the same URL.

To check out - Jenkins uses numerous commands to accomplish the basic checkout.

My script use rather basic: "git clone URL DIRECTORY"

When Jenkins does this, it says it is using the keys from the Agent script. I don't see this message when my script runs.

I have seen numerous thing suggest using a Groovy Script, or a pipeline job etc. Sorry - that will not work for me, I need the shell script to do this work. Reason: That shell script is used in the developer environment, our check out process involves numerous modules - and GIT sub modules don't work for me

My shell script executes via a "Build Step - Execute Shell"

My shell script SEES the agent, but for some reason I do not know why/how this does not get the SSH keys. Nor do I know how to test if the keys are still there? Maybe jenkins deletes them from the agent?

Does the Build/Execute Script support SSH credentials?

(EDIT 2/6 - add example log output showing the problem)

Jenkins Log output

[EnvInject] - Loading node environment variables.
Building remotely on myslave - tiopbld (tiop_linux) in workspace /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent]   Exec ssh-agent (binary ssh-agent on a remote machine)
$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-PRUTedOo0d7o/agent.16607
SSH_AGENT_PID=16609
[ssh-agent] Started.
$ ssh-add /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh@tmp/private_key_2048740533538871194.key
Identity added: /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh@tmp/private_key_2048740533538871194.key (/scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh@tmp/private_key_2048740533538871194.key)
[ssh-agent] Using credentials tiopbld
Cloning the remote Git repository
Cloning repository ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git
 > git init /scratch/jenkins/workspace/pybuilder-testing/shellscript_test.sh # timeout=10
Fetching upstream changes from ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git
 > git --version # timeout=10
using GIT_SSH to set credentials jenkins-tiop-myuser
 > git fetch --tags --progress ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git # timeout=10
Fetching upstream changes from ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git
using GIT_SSH to set credentials jenkins-tiop-myuser
 > git fetch --tags --progress ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 5255667cfed01bdfcecc558a2c63bd6679503fc7 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 5255667cfed01bdfcecc558a2c63bd6679503fc7
Commit message: "test commit from new linus machine"
First time build. Skipping changelog.
[shellscript_test.sh] $ /bin/sh -xe /tmp/jenkins2668814567868620035.sh
+ git clone ssh://git@bitbucket.myserver.com/pybuild/pythonbuilder.git OUTPUTDIR
Cloning into 'OUTPUTDIR'...
Permission denied (publickey).
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
$ ssh-agent -k
unsetenv SSH_AUTH_SOCK;
unsetenv SSH_AGENT_PID;
echo Agent pid 16609 killed;
[ssh-agent] Stopped.
Unable to connect to Elasticsearch service. Check Elasticsearch is running in the correct namespace
Finished: FAILURE

```

Always double check your SSH-KEY key names... make sure they are DISTINCTLY named ... For example - "jenkins-master-to-slave-key" and another "jenkins-slave-to-gitrepo"

In my case, the keynames where one letter different. (in the above) the key names where obscured for other reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM