我有一个 Jekyll 网站(GH 页面)的构建脚本,需要从脚本内部调用需要 Github 身份验证的 git 命令。这是脚本:

#!/usr/bin/env bash 
 
rm -rf _site/ 
 
git clone <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbbcb2af9bbcb2afb3aeb9f5b8b4b6" rel="noreferrer noopener nofollow">[email protected]</a>: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 Hub pull 出的 Docker 容器中运行 Jenkins。我通过添加 Jenkins 使用的相同私钥信息来修改容器,以执行存储库的初始克隆。但是,当我从脚本调用 git 命令时,它说它未经身份验证:

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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8aede3fecaede3fee2ffe8a4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>:RIT-EVT/RIT-EVT.github.io.git # timeout=10 
Fetching upstream changes from <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9beb0ad99beb0adb1acbbf7bab6b4" rel="noreferrer noopener nofollow">[email protected]</a>:RIT-EVT/RIT-EVT.github.io.git 
 > git --version # timeout=10 
using GIT_SSH to set credentials GitHub - ssh 
 > git fetch --tags --progress <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8a8499ad8a849985988fc38e8280" rel="noreferrer noopener nofollow">[email protected]</a>: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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89f918cb89f918c908d9ad69b9795" rel="noreferrer noopener nofollow">[email protected]</a>: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 二进制文件都没有获取该 key 。更奇怪的是,我可以通过 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 中的 key 信息,因为该脚本不是在加载 key 的同一用户下运行(?)

任何帮助将不胜感激:)

更新:

我运行了 whoami 来看看这是否适用于脚本,它给了我 jenkins 作为结果:

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

所以我很困惑为什么 git 无法从 ssh-agent 获取私钥。

请您参考如下方法:

我知道出了什么问题了。如果我使用 docker exec -it jenkins bash 登录容器,跑eval `ssh-agent -s`启动 ssh-agent,然后运行 ​​ssh-add <keyFile> ,我可以运行 git clone就在壳里。

但是,如果退出 shell 并通过运行相同的 docker 命令重新登录,ssh-agent 将不会启动,也不会加载私钥,这让我相信 ssh-agent 实际上并不是当 Jenkins 构建运行时运行。

所以,我改用 https使用 git 并使用凭据绑定(bind)插件将用户名和密码提供到存储库的 URL 中。

所以本质上,您不能从 Jenkins 构建脚本调用 git 命令并期望能够使用您的 SSH key 。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!