2017-09-04 103 views
0

我跟着the GitLab Docs使我的项目的CI克隆其他私人依赖项。一旦它的工作,我从.gitlab-ci.yml提取:GitLab管道:在YML中工作,在提取失败SH

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

当运行ssh-add使用源或。从而使脚本相同的外壳内运行,在你的情况将是:

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

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

对于为什么这需要在同一个外壳的其余部分运行一个更好的解释看看这个答案的相关问题here