获取GitLab CI以克隆私有存储库

问题描述 投票:33回答:8

我设置了GitLab和GitLab CI来托管和测试我的一些私人回购。对于我在这个系统下的作曲家模块,我设置了Satis来解析我的私人包。

显然这些私有包需要一个ssh密钥来克隆它们,我在终端中工作 - 我可以运行composer install并获取这些包,只要我在shell中添加了ssh-add的密钥。

但是,当在GitLab CI中运行我的测试时,如果项目具有任何这些依赖项,则测试将无法完成,因为我的GitLab实例需要身份验证才能获得deps(显然),并且测试无法说Host key verification failed

我的问题是如何设置它以便当跑步者运行测试时它可以在没有密码的情况下对gitlab进行身份验证?我已经尝试在我的跑步者~/.ssh文件夹中放入无密码的ssh-key,但是构建甚至不会添加密钥,“eval ssh-agent -s”后面跟ssh-add似乎失败说代理没有运行...

ssh continuous-integration gitlab gitlab-ci gitlab-ci-runner
8个回答
20
投票

我发布这个作为答案,因为其他人不完全清楚和/或详细的恕我直言

从GitLab 8.12+开始,假设子模块repo与请求它的服务器位于同一服务器中,您现在可以:

  1. 像往常一样用git子模块设置repo(git submodule add git@somewhere:folder/mysubmodule.git
  2. 修改您的.gitmodules文件,如下所示 [submodule "mysubmodule"] path = mysubmodule url = ../../group/mysubmodule.git 其中`../../group/mysubmodule.git'是从您的存储库到子模块的相对路径。
  3. 将以下行添加到gitlab-ci.yml variables: GIT_SUBMODULE_STRATEGY: recursive 指示运行器在构建之前获取所有子模块。

警告:如果你的跑步者似乎忽略了GIT_SUBMODULE_STRATEGY指令,你应该考虑updating it

(来源:https://docs.gitlab.com/ce/ci/git_submodules.html


40
投票

另见其他解决方案:


这里有一个完整的SSH密钥:

一般设计

  • 生成一对SSH密钥
  • 将私有一个添加为项目的安全环境变量
  • 使私有版本可用于GitLab-CI上的测试脚本
  • 将public one添加为每个私有依赖项的部署密钥

生成一对公钥和私钥SSH密钥

生成一对没有密码的公钥和私钥SSH密钥:

ssh-keygen -b 4096 -C "<name of your project>" -N "" -f /tmp/name_of_your_project.key

将私有SSH密钥添加到项目中

您需要将密钥作为安全环境变量添加到项目中,如下所示:

  • 浏览https://<gitlab_host>/<group>/<project_name>/variables
  • 点击“添加变量”
  • Key填充文本字段SSH_PRIVATE_KEY
  • 使用私有SSH密钥本身填充文本字段Value
  • 点击“保存更改”

将私有SSH密钥公开给您的测试脚本

为了使您的私钥可用于测试脚本,您需要将以下内容添加到.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密钥作为部署密钥添加到所有私有依赖项

您需要将公共SSH密钥作为部署密钥注册到所有私有依赖项,如下所示:

  • 浏览https://<gitlab_host>/<group>/<dependency_name>/deploy_keys
  • 点击“新部署密钥”
  • 用文本名称填写文本字段Title
  • 使用公共SSH密钥本身填充文本字段Key
  • 点击“创建部署密钥”

9
投票

如果您不想使用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 [email protected]:group/repo.git

4
投票

通过使用ssh-keyscan -H 'localgitlab.co.uk' >> ~gitlab_ci_runner/.ssh/known_hosts将密钥添加到已知主机来解决此问题


4
投票

我使用deploy tokens来解决这个问题,因为为测试运行器设置SSH密钥似乎有点长。

git clone http://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git

部署令牌是每个项目并且是只读的。


4
投票

currently accepted answer将Gitlab特定的要求嵌入到我的.gitmodules文件中。这会强制特定目录布局进行本地开发,并且会使移动到另一个版本控制平台变得复杂。

相反,我遵循了Juddling's answer的建议。这是一个更完整的答案。

我的.gitmodules文件包含以下内容:

[submodule "myproject"]
    url = [email protected]: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 "[email protected]:"
    - git submodule sync && git submodule update --init

由于我们正在从SSH身份验证映射到HTTPS,因此/:的尾随在git config行中是至关重要的。这让我误解了一段“非法端口号”错误。

我喜欢这个解决方案,因为它将Gitlab特定的需求嵌入到特定于Gitlab的文件中,其他所有文件都会忽略它。


0
投票

我有一个场景,我必须在3个不同的脚本中使用我的ssh密钥,因此我将ssh密钥放在一个shell脚本中,并在其他3个脚本之前调用它。这最终无法正常工作,我认为由于ssh-agent不会在shell脚本之间持久存在,或者是某种效果。实际上我只是将私钥输出到~/.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

它就像一个魅力!


0
投票

似乎终于有一个reasonable solution

简而言之,就像GitLab 8.12一样,你需要做的就是在.submodules中使用相对路径,而git submodule update --init只需要工作

© www.soinside.com 2019 - 2024. All rights reserved.