在 Google Container Builder 中使用 SSH 密钥

Using SSH keys with Google Container Builder

我们目前在 Google 容器引擎上的 Rails 网站上托管我们的 Ruby。我正在将我们的部署从第 3 方 CI 迁移到新的 Google Container Builder。唯一阻碍我的是从私有 Github 存储库安装我们的 gem。我已经创建了一个存储在云存储中的 SSH 密钥,我使用 gsutil 命令加载它。我已经确保密钥在那里并且具有正确的文件权限(使用简单的 busybox ls 构建步骤)但是我不知道如何让 Bundler/git 稍后使用它。我尝试了一些使用此 answer(例如 GIT_SSH_COMMAND)中的环境变量的解决方案,但均未成功。

我通过添加 SSH 密钥并使用 ssh-add 在我们当前的 CI 上工作,这样 git 以后可以找到它。然而,基础 Google 图像似乎没有使用 ssh-agent。

有什么建议(或技巧 :) 来解决这个问题?

我会为你列出两个选项。

1) 由于您在云存储中获得了 ssh 密钥,因此您可以使用 gsutil cloud-builder 将其从那里拉到您的构建上下文中。自述文件包含将文件从 GCS 复制到构建工作区的示例。

请注意,您可能必须 set up the Builder Service Account to have read access 包含凭据的 GCS 对象。

一旦您在工作区中获得了凭据,您应该就可以用它们做您想做的事了。

2) 如果您 connect your GitHub repository to a Cloud Source Repository,则可以直接从那里提取源代码,前提是您的构建者服务帐户可以访问 CSR。 (如果在同一个项目中,则默认具有读取权限。)

完全披露:我是 Container Builder 背后工程团队的 Google 员工。

好的,在 Google 团队的帮助和反复试验后,我发现以下策略有效。

在我的 cloudbuild.yaml 中,我添加了以下构建步骤:

- name: ruby:2.2 args: ['bash', './devops/cloud_bundle_install.sh']

其中 ./devops/cloud_bundle_install.sh 如下:

eval `ssh-agent` mkdir -p /root/.ssh ssh-keyscan github.com >> ~/.ssh/known_hosts mv /workspace/<KEY_DOWNLOADED_VIA_GSUTIL> ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-add /workspace/.ssh/id_rsa bundle package --all --all-platforms

更简单的版本:

cloudbuild.yaml:

  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    args: ['gsutil', 'cp', '-r', 'gs://artifacts.<PROJECT-ID>.appspot.com/.ssh', '/builder/home/']
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: /bin/bash
    args: ['-c', 'chmod 400 /builder/home/.ssh/build']
  - name: 'gcr.io/cloud-builders/gcloud'
    args: [ 'beta', 'compute', 'ssh', '--zone=<ZONE>', 'build@<INSTANCE-ID>', '--ssh-key-file=/builder/home/.ssh/build', '--', 'ls -al'