我有GitLab& GitLab CI设置主持并测试我的一些私人回购。对于我在这个系统下的作曲家模块,我已经设置了Satis来解析我的私人包。
显然这些私有软件包需要一个ssh密钥来克隆它们,我在终端中工作了 - 我可以运行composer install并获取这些软件包,只要我在ssh-add
中添加了密钥。外壳
然而,当在GitLab CI中运行我的测试时,如果项目具有任何这些依赖项,则测试将无法完成,因为我的GitLab实例需要身份验证才能获得deps(显然),并且测试失败说Host key verification failed
我的问题是如何设置它以便当跑步者运行测试时它可以在没有密码的情况下对gitlab进行身份验证?我已经尝试在我的跑步者~/.ssh
文件夹中放入一个无密码的ssh-key,但是这个版本甚至不会添加密钥," eval ssh-agent -s
"然后是ssh-add似乎没有说代理人没有运行......
答案 0 :(得分:38)
另见其他解决方案:
这里有一个完整的SSH密钥:
生成一对没有密码的公钥和私钥SSH密钥:
ssh-keygen -b 4096 -C "<name of your project>" -N "" -f /tmp/name_of_your_project.key
您需要将密钥作为安全环境变量添加到项目中 以下内容:
https://<gitlab_host>/<group>/<project_name>/variables
Key
SSH_PRIVATE_KEY
Value
为了使您的私钥可用于您的测试脚本,您需要添加
以下是您的.gitlab-ci.yml
文件:
before_script:
# install ssh-agent
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
# run ssh-agent
- eval $(ssh-agent -s)
# add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY")
# disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks)
# WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config
- mkdir -p ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
Code Snippet comes from GitLab documentation
您需要将公共SSH密钥注册为部署密钥到您的所有私有密钥 依赖关系如下:
https://<gitlab_host>/<group>/<dependency_name>/deploy_keys
Title
中填入项目名称Key
答案 1 :(得分:17)
我发布这个作为答案,因为其他人并不完全清楚和/或详细恕我直言
从GitLab 8.12+开始,假设子模块repo与请求它的服务器位于同一服务器中,您现在可以:
像往常一样使用git子模块设置repo(git submodule add git@somewhere:folder/mysubmodule.git
)
修改.gitmodules
文件,如下所示
[submodule "mysubmodule"]
path = mysubmodule
url = ../../group/mysubmodule.git
其中`../../group/mysubmodule.git'是从存储库到子模块的相对路径。
将以下行添加到gitlab-ci.yml
variables:
GIT_SUBMODULE_STRATEGY: recursive
指示跑步者在构建之前获取所有子模块。
警告:如果你的跑步者似乎忽略了GIT_SUBMODULE_STRATEGY
指令,你应该考虑updating it。
答案 2 :(得分:8)
如果您不想使用ssh键或子模块,可以覆盖git配置中的repo以使用作业令牌进行身份验证(在gitlab-ci.yml
中):
before_script:
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/group/repo.git".insteadOf git@gitlab.example.com:group/repo.git
答案 3 :(得分:4)
通过将密钥添加到ssh-keyscan -H 'localgitlab.co.uk' >> ~gitlab_ci_runner/.ssh/known_hosts
答案 4 :(得分:3)
我用deploy tokens解决了这个问题,因为为测试运行程序设置SSH密钥似乎有点麻烦。
git clone http://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git
部署令牌是每个项目的只读信息。
答案 5 :(得分:3)
currently accepted answer将Gitlab特定的要求嵌入到我的.gitmodules
文件中。这将强制为本地开发设置特定的目录布局,并使向另一个版本控制平台的迁移变得复杂。
相反,我遵循了Juddling's answer中的建议。这是一个更完整的答案。
我的.gitmodules
文件具有以下内容:
[submodule "myproject"]
url = git@git.myhost.com:mygroup/myproject.git
在我的gitlab-ci.yml
中,我有以下内容:
build:
stage: build
before_script:
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@git.myhost.com/".insteadOf "git@git.myhost.com:"
- git submodule sync && git submodule update --init
在/
行中,尾随:
和git config
至关重要,因为我们正在从SSH身份验证映射到HTTPS。这使我绊了一段时间,出现“ 非法端口号”错误。
我喜欢此解决方案,因为它将特定于Gitlab的需求嵌入到特定于Gitlab的文件中,而其他所有内容都将忽略它。
答案 6 :(得分:0)
似乎终于有reasonable solution。
简而言之,就GitLab 8.12而言,您需要做的只是使用.submodules
中的相对路径,而git submodule update --init
只会工作
答案 7 :(得分:0)
ssh-agent
不存在,或者是这种效果。实际上我只是将私钥输出到~/.ssh/id_rsa
文件中,这肯定会持久存储到其他脚本中。
.gitlab-ci.yml
script:
- ci/init_ssh.sh
- git push # or whatever you need ssh for
CI / init_ssh.sh
# only run in docker:
[[ ! -e /.dockerenv ]] && exit 0
mkdir -p ~/.ssh
echo "$GITLAB_RUNNER_SSH_KEY" > ~/.ssh/id_rsa
chmod 400 ~/.ssh/id_rsa
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > /.ssh/config
它就像一个魅力!
答案 8 :(得分:0)
在不更改git存储库结构的情况下解决此问题的一种方法是执行以下步骤:
获取正在运行的服务器的ssh主机密钥。对于gitlab.com
:
ssh-keyscan gitlab.com > known_hosts
ssh-keygen -lf known_hosts
与报告的here指纹是否一致。known_hosts
的内容并将其粘贴到存储库中名为SSH_KNOWN_HOSTS
的变量上。此步骤只需一次。
before_script:
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "git@gitlab.com:"
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
如果您尝试执行"ssh://git@gitlab.com"
或git clone git@gitlab.com:
,则pip install -e git+ssh://git@gitlab.com/...
位可能会有所不同;根据您的需要进行调整。
这时,您的CI可以使用ssh从另一个(私有)存储库中获取。
使用this trick进行通用编写:
.enable_ssh: &enable_ssh |-
git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "ssh://git@gitlab.com"
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
并在需要它的工作上启用它
test:
stage: test
before_script:
- *enable_ssh
script:
- ...
答案 9 :(得分:0)
将其添加到.gitlab-ci.yml对我有用。 (如此处所述:https://docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html#dependent-repositories)
before_script:
echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
(我尝试按照上述答案之一中的说明设置SSH_PRIVATE_KEY,它将无法正常工作)
答案 10 :(得分:0)
如果您使用的是基于 alpine 的图像(可能是 docker:latest
或 docker:dind
),您的 before_script
可能如下所示:
before_script:
- apk add --no-cache openssh-client git
- mkdir -p /.ssh && touch /.ssh/known_hosts
- ssh-keyscan gitlab.com >> /.ssh/known_hosts
- echo $SSH_KEY | base64 -d >> /.ssh/id_rsa && chmod 600 /.ssh/id_rsa
- git clone git@git.myhost.com:mygroup/myproject.git