繁体   English   中英

jenkins,在shell脚本中使用ssh

[英]jenkins, using ssh in a shell script

更新:2/7-参见下面的答案

我似乎无法让git / ssh检出从脚本内工作。

什么有效:

我的詹金斯工作有SSH密钥。

詹金斯(Jenkins)通过GIT插件从bitbucket中检出了东西(通过SSH),没有问题。

我的詹金斯工作是这样的:

詹金斯的工作主要做两件事:

  1. 检出带有shell脚本的git模块(在Linux机器上)

  2. 使用传递的参数执行git模块(即:Parameterized Build)

该脚本确实(或应该)完成了沙箱目录中的所有工作,这涉及签出约8个其他GIT模块,根据作为Paramerized Build的一部分传递的值来选择分支等。

我所知道的工作原理和发现问题的地方

我有SSH代理存在并已启用。 通过使用脚本中的SET命令,我已经验证了该代理是否存在,可以看到SSH_AGENT shell变量。

在jenkins工作结束时,我看到Jenkins声明它正在杀死SSH代理。

Jenkins使用URL ssh://git@someserver.com/path/to/repo.git-作为URL,我使用的是同一URL。

签出-Jenkins使用许多命令来完成基本的签出。

我的脚本使用了相当基本的内容:“ git clone URL DIRECTORY”

Jenkins执行此操作时,表示它正在使用Agent脚本中的密钥。 我的脚本运行时没有看到此消息。

我已经看到很多建议使用Groovy脚本或管道作业等的东西。对不起-对我来说不起作用,我需要Shell脚本来完成这项工作。 原因:该Shell脚本用于开发人员环境,我们的检出过程涉及许多模块-GIT子模块对我不起作用

我的Shell脚本通过“构建步骤-执行Shell”执行

我的shell脚本SEES该代理,但是由于某些原因,我不知道为什么/如何无法获得SSH密钥。 我也不知道如何测试钥匙是否还在那里? 也许詹金斯将其从代理中删除了?

生成/执行脚本是否支持SSH凭据?

(编辑2/6-添加显示问题的示例日志输出)

Jenkins日志输出

[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

```

始终仔细检查您的SSH-KEY密钥名称...确保它们的名称明显不同...例如-“ jenkins-master-to-slave-key”和另一个“ jenkins-slave-to-gitrepo”

在我的情况下,键名一个字母不同。 (在上面)由于其他原因而被遮盖的键名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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