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

Shell GitLab管道:在YML中工作,在提取的SH中失败,shell,gitlab,gitlab-ci,Shell,Gitlab,Gitlab Ci,我遵循了这一点,以使我的项目的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 ~/.s

我遵循了这一点,以使我的项目的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'
只留下:

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.

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

在运行ssh时,添加use source或add。因此,脚本在同一个shell中运行,在您的情况下,它将是:

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

为了更好地解释为什么这需要和其他的运行在同一个shell中,请看一个相关问题的答案

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