GitLab管道:在YML中工作,在提取的SH中失败

时间:2017-09-04 03:52:05

标签: shell gitlab gitlab-ci

我按照the GitLab Docs启用项目的CI以克隆其他私有依赖项。一旦它工作,我从.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,如下所示:

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.

如何在提取的脚本中复制原始行为?

1 个答案:

答案 0 :(得分:1)

运行ssh-add时要么使用source,要么使用。所以脚本在同一个shell中运行,在你的情况下它将是:

before_script:
  - chmod 700 ./setup.sh 
  - . ./setup.sh

before_script:
  - chmod 700 ./setup.sh 
  - source ./setup.sh

为了更好地解释为什么需要在与其他shell相同的shell中运行,请查看相关问题here的答案。