GitLab Pipeline: Works in YML, Fails in Extracted SH

。_饼干妹妹 提交于 2019-12-12 04:57:45

问题


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'

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

leaving only:

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

I then began getting:

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?


回答1:


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

or

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.



来源:https://stackoverflow.com/questions/46030051/gitlab-pipeline-works-in-yml-fails-in-extracted-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!