在Jenkins中调用Git在Docker中构build脚本

我有一个Jekyll网站(GH页面)的构build脚本,需要从脚本内部调用需要Githubauthentication的git命令。 这是脚本:

#!/usr/bin/env bash rm -rf _site/ git clone git@github.com:RIT-EVT/RIT-EVT.github.io.git --branch master --depth 1 _site LIVE_VERSION_BUILD=`cat _site/version` LIVE_VERSION=${LIVE_VERSION_BUILD%.*} LIVE_BUILD=${LIVE_VERSION_BUILD##*.} PACKAGE_VERSION=`sed -nE 's/^\s*"version": "(.*?)",$/\1/p' package.json` if [[ "$LIVE_VERSION" == "$PACKAGE_VERSION" ]]; then LIVE_BUILD=`expr $LIVE_BUILD + 1` else LIVE_VERSION=${PACKAGE_VERSION} LIVE_BUILD=0 fi rm -rf _site/* jekyll build echo "$LIVE_VERSION.$LIVE_BUILD" > _site/version cd _site/ git add -A git commit -m "v$LIVE_VERSION.$LIVE_BUILD $(date)" git push cd .. 

我正在Docker容器中运行的一个Docker容器中运行Jenkins。 我通过添加Jenkins使用的相同的私钥信息来修改容器,以执行存储库的初始克隆。 但是,当我从脚本中调用一个git命令时,它说它是未经validation的:

 Started by user evt Building in workspace /var/jenkins_home/workspace/Website Deploy > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@github.com:RIT-EVT/RIT-EVT.github.io.git # timeout=10 Fetching upstream changes from git@github.com:RIT-EVT/RIT-EVT.github.io.git > git --version # timeout=10 using GIT_SSH to set credentials GitHub - ssh > git fetch --tags --progress git@github.com:RIT-EVT/RIT-EVT.github.io.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/develop^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/develop^{commit} # timeout=10 Checking out Revision 85084620e62b5b03f02c610e33880eeb94b12531 (refs/remotes/origin/develop) > git config core.sparsecheckout # timeout=10 > git checkout -f 85084620e62b5b03f02c610e33880eeb94b12531 > git rev-list 85084620e62b5b03f02c610e33880eeb94b12531 # timeout=10 [Website Deploy] $ /bin/bash -xe /tmp/hudson9119924045433544873.sh + rm -rf _site/ + git clone git@github.com:RIT-EVT/RIT-EVT.github.io.git --branch master --depth 1 _site Cloning into '_site'... 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 Finished: FAILURE 

所以关键信息显然适用于jenkins的git插件,但是无论出于什么原因,关键都不是被容器中的git二进制文件拾取的。 更奇怪的是我可以通过SSH进入容器运行的机器, docker exec -it jenkins bash进入容器,并运行相同的git命令,并且完美运行。

这让我觉得这可能是用户和权限的问题。 所以我试图找出用户jenkins运行的脚本。 这是我跑的非常小的脚本:

 echo ${USER} echo hello # Just a sanity test to make sure that echo works :p 

这是我得到的输出:

 misc. Jenkins stuff... [Website Deploy] $ /bin/sh -xe /tmp/hudson1343134234815638946.sh + echo + echo hello hello Finished: SUCCESS 

所以它似乎无法访问加载到ssh-agent的关键信息,因为脚本不是在加载密钥(?)的同一用户下运行的。

任何帮助将不胜感激 :)

更新:

我跑了whoami ,看看这是否会在脚本中jenkins ,结果给了我jenkins

 [Website Deploy] $ /bin/bash -xe /tmp/hudson2652666458388248519.sh + whoami jenkins Finished: SUCCESS 

所以我很难为什么git不能从ssh-agent获取私钥。

虽然这可能适用于您,但完全可以在Jenkins的Docker容器中调用git命令。 configuration构build作业时,只需使用SSH代理插件。 这不仅会提供Jenkins证书存储所需的SSH密钥,还会为您configuration和设置SSH代理。

我总是build议只使用SSH密钥,因为它们更安全,更难以妥协。

另外,SSH代理插件也可以在Jenkinspipe道脚本中调用。 只需将任何需要访问该密钥的命令包装在一个sshage块中即可。

例如:

 sshagent(credentials: ['jenkins-credential-id']) { sh 'git clone git@github.com:RIT-EVT/RIT-EVT.github.io.git' sh 'jekyll build' } 

我找出了什么错。 如果我使用docker exec -it jenkins bashlogin容器,运行eval `ssh-agent -s`来启动ssh-agent,并运行ssh-add <keyFile> ,我可以在shell中运行git clone

但是,如果退出shell并通过运行相同的docker命令重新login,则ssh-agent将不会启动,并且不会加载私钥,这导致我相信ssh-agent在实际运行时并未实际运行jenkins生成跑了。

所以,我转而使用https与git,并通过使用凭证绑定插件提供用户名和密码到回购的URL。

所以实质上,你不能从Jenkins构build脚本中调用git命令,并希望能够使用你的SSH密钥。