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

GitLab Pipeline: Works in YML, Fails in Extracted SH

我按照 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.

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

当 运行ning ssh-add 使用 source 或 .这样脚本 运行s 在同一个 shell 中,在你的情况下它将是:

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

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

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