GitLab管道:在YML中工作,在提取的SH中失败 [英] GitLab Pipeline: Works in YML, Fails in Extracted SH
问题描述
我遵循了 GitLab文档,以使我的项目的配置项能够克隆其他私有依赖项.一旦工作,我从.gitlab-ci.yml
中提取:
I followed the GitLab Docs to enable my project's CI to clone other private dependencies. Once it was working, I extracted from .gitlab-ci.yml
:
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
放入一个单独的shell脚本setup.sh
中,如下所示:
into a separate shell script setup.sh
as follows:
which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
eval $(ssh-agent -s)
ssh-add <(echo "$SSH_PRIVATE_KEY")
mkdir -p ~/.ssh
[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
仅离开:
before_script:
- chmod 700 ./setup.sh
- ./setup.sh
然后我开始得到:
Cloning into '/root/Repositories/DependentProject'...
Warning: Permanently added 'gitlab.com,52.167.219.168' (ECDSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
如何在提取的脚本中复制原始行为?
How do I replicate the original behavior in the extracted script?
推荐答案
在运行ssh-add时,请使用source或.这样脚本就可以在同一外壳中运行,在您的情况下,它将是:
When running ssh-add either use source or . so that the script runs within the same shell, in your case it would be:
before_script:
- chmod 700 ./setup.sh
- . ./setup.sh
或
before_script:
- chmod 700 ./setup.sh
- source ./setup.sh
要获得更好的解释,为什么它需要与其余代码在同一外壳中运行,请查看有关相关问题的答案这里.
For a better explanation as to why this needs to run in the same shell as the rest take a look at this answer to a related question here.
这篇关于GitLab管道:在YML中工作,在提取的SH中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!