【问题标题】:jenkins, using ssh in a shell scriptjenkins,在 shell 脚本中使用 ssh
【发布时间】:2018-07-16 01:02:54
【问题描述】:

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

我似乎无法在脚本中使用 git/ssh 签出。

什么有效:

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

Jenkins 通过 GIT 插件检查(通过 SSH)bitbucket 中的内容,没有问题。

我的 Jenkins 工作是这样的:

jenkins 的工作基本上做了两件事:

  1. 查看带有 shell 脚本的 git 模块(在 linux 机器上)

  2. 执行带参数的git模块(即:Parameterized 构建)

脚本确实(或应该)在沙盒目录中完成所有工作,这包括检查大约 8 个其他 GIT 模块,根据作为参数化构建的一部分传递的值选择分支等。

我所知道的有效方法以及我发现问题的地方

我有 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 执行此操作时,它表示它正在使用代理脚本中的密钥。 我的脚本运行时没有看到此消息。

我看到很多建议使用 Groovy 脚本或管道作业等。抱歉 - 这对我不起作用,我需要 shell 脚本来完成这项工作。原因:那个shell脚本是在开发环境中使用的,我们的check out过程涉及到很多模块——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

```

【问题讨论】:

  • 这里需要密码保护的私钥吗?
  • 否 - 没有密码。
  • 注意:刚刚添加了显示问题的日志输出。

标签: git jenkins jenkins-plugins


【解决方案1】:

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

就我而言,一个字母不同的键名。 (在上面)由于其他原因而被隐藏的键名。

【讨论】:

  • 好收获。 +1。
  • 您能否介绍一下凭据是如何设置的?谢谢!
猜你喜欢
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2017-10-11
  • 2018-01-31
  • 2021-05-19
  • 2019-05-20
  • 2012-04-06
相关资源
最近更新 更多