在Docker中的Jenkins构建脚本中调用Git
我有一个用于Jekyll网站(GH页面)的构建脚本,它需要从脚本内部调用需要Github身份验证的git命令。以下是脚本:在Docker中的Jenkins构建脚本中调用Git,git,bash,docker,jenkins,ssh,Git,Bash,Docker,Jenkins,Ssh,我有一个用于Jekyll网站(GH页面)的构建脚本,它需要从脚本内部调用需要Github身份验证的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_VERS
#!/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容器内运行Jenkins,该容器是我从Docker中心提取的。我修改了容器,添加了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 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代理中的密钥信息,因为脚本不是在加载密钥的同一用户下运行的(?)
任何帮助都将不胜感激:)
更新:
我运行了whoami
以查看脚本中是否会出现这种情况,结果是jenkins
:
[Website Deploy] $ /bin/bash -xe /tmp/hudson2652666458388248519.sh
+ whoami
jenkins
Finished: SUCCESS
所以我很困惑为什么git不能从ssh代理获取私钥 我发现了问题所在。如果我使用docker exec-it jenkins bash
登录容器,运行eval`ssh agent-s`
启动ssh代理,然后运行ssh add
,我就可以在shell中运行git clone
但是,如果退出shell并通过运行相同的docker命令重新登录,ssh代理将不会启动,也不会加载私钥,这让我相信在Jenkins构建运行时ssh代理实际上没有运行
因此,我转而使用git的https
,并通过使用凭证绑定插件向repo的URL提供用户名和密码
因此,本质上,您不能从Jenkins构建脚本调用git命令并期望能够使用SSH密钥 虽然这可能对您有效,但完全可以从Jenkins上的Docker容器中调用git命令。配置构建作业时,只需使用SSH代理插件。这将不仅提供您从Jenkins凭据存储中需要的SSH密钥,还将为您配置和设置SSH代理
我总是建议只使用SSH密钥,因为它们更安全,更难泄露
此外,还可以在Jenkins管道脚本中调用SSH代理插件。只需将需要访问密钥的任何命令包装为sshagent块
例如:
sshagent(credentials: ['jenkins-credential-id']) {
sh 'git clone git@github.com:RIT-EVT/RIT-EVT.github.io.git'
sh 'jekyll build'
}
由于echo${USER}似乎没有帮助,您可以尝试用whoami
命令替换它,您应该获得jenkins正在运行的用户名。此外,我不确定从bash脚本调用git是否会与jenkins插件交互。这取决于插件是否将密钥存储在用户的.ssh目录中以及如何存储。如果你使用了-i标志并给出了你想要使用的密钥的路径,你应该是免费的。我不需要它与jenkins插件交互。这就是为什么我还将私钥加载到容器本身的ssh密钥存储中,因为git通常就是这样获取私钥的,但我不明白为什么它不能访问ssh-agent。您可以使用您的ssh密钥和ssh-agent。请在这里查看答案:查看更多上下文会很有用。你在哪里写的?步调一致?