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

时间:2014-09-05 15:26:18

标签: ssh continuous-integration gitlab gitlab-ci gitlab-ci-runner

我有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似乎没有说代理人没有运行......

11 个答案:

答案 0 :(得分:38)

另见其他解决方案:

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

一般设计

  • 生成一对SSH密钥
  • 将私有项添加为项目的安全环境变量
  • 在GitLab-CI上为您的测试脚本提供私有的
  • 在每个私有依赖项上添加公共密钥作为部署密钥

生成一对公钥和私钥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
  • 点击“创建部署密钥”

答案 1 :(得分:17)

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

从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
    

    指示跑步者在构建之前获取所有子模块。

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

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

答案 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)

我有一个场景,我必须在3个不同的脚本中使用我的ssh密钥,所以我将ssh密钥放在一个shell脚本中,然后在其他3个脚本之前调用它。这最终无法正常工作,我认为由于{脚本脚本之间的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存储库结构的情况下解决此问题的一种方法是执行以下步骤:

1。获取ssh主机密钥

获取正在运行的服务器的ssh主机密钥。对于gitlab.com

  1. 运行ssh-keyscan gitlab.com > known_hosts
  2. 检查ssh-keygen -lf known_hosts与报告的here指纹是否一致。
  3. 复制known_hosts的内容并将其粘贴到存储库中名为SSH_KNOWN_HOSTS的变量上。

此步骤只需一次。

2。配置作业以使用ssh

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从另一个(私有)存储库中获取。

3。 [奖金DRY]

使用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:latestdocker: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