如何从 circleci ssh 进入 GCP 计算引擎实例?

How do I ssh into a GCP compute engine instance from circleci?

我正在使用 circleci 2.0,我正在尝试让 circleci 通过 ssh 进入 gcp 实例并使用此命令启动部署脚本

sudo /opt/google-cloud-sdk/bin/gcloud compute ssh instance-1 
--command=/home/deploy_staging.sh --zone=us-east1-b

在我的本地机器上执行此操作很好,但是当我尝试在 circleci 上执行此操作时,出现此错误:

WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Too long with no output (exceeded 10m0s)

显然是 ssh 问题。我阅读了一些关于将 ssh 密钥放入 gcp 实例中的内容,并且我已经将本地计算机的 ssh 密钥放在那里,但这仍然不起作用。

根据 official CircleCI documentation,您首先需要在 CircleCI 应用程序中为服务器添加 SSH 密钥 - 这可以在项目设置和 SSH 权限部分下完成。

gcloud \
  --quiet \
  --project="${PROJECT}" \
  compute ssh "${INSTANCE_NAME}" \
  --zone "${ZONE}" \
  --strict-host-key-checking=no \
  --command "echo works"

--quiet 就是您所需要的,如果没有,它将生成一个密钥。我正在通过服务帐户使用它。

WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/google_compute_engine.
Your public key has been saved in /root/.ssh/google_compute_engine.pub.
The key fingerprint is:
SHA256:un2aZmExTGVD0KvebEVqAujrlXoAb0u7jO3Z5boCWaA root@581dc589b7fa
The key's randomart image is:
+---[RSA 2048]----+
|        .==      |
|  .     ....     |
| . . . o   .     |
|E . o . + . .    |
|   *   .S+ o     |
|  o *  o= o .    |
|   + =+o.* .     |
|   +==.=+.=      |
|  .oO+===+       |
+----[SHA256]-----+
WARNING: Using OS Login user [sa_102839341411404994442] instead of default user [root]