Google 使用 Compute Engine 进行云构建

Google cloud build with Compute Engine

我想使用 Cloud Build 并在提交时触发自动 fetch updated repo 和 运行 sudo supervisorctl restart Compute Engine 个实例。

Cloud Build 设置页面上,有一个连接 Compute Engine 的选项,但到目前为止我只找到了示例,包括 Kubernetes 引擎App 引擎 here.

有可能实现吗?这是进行更新的正确方法吗?或者我应该用 startup-script?

重新启动实例

云构建者社区的 Github 中有一个存储库,可能正是您要找的。

如上述 link 中所述,它确实通过以下步骤将 Cloud Build 连接到 Compute Engine:

  1. 将在您的 Container Builder 工作区中创建一个临时 SSH 密钥

  2. 将使用您配置的标志启动一个实例

  3. 工作区将被复制到远程实例

  4. 您的命令将 运行 在该实例的工作区内

  5. 工作区将被复制回您的 Container Builder 工作区

您需要创建具有创建和销毁计算引擎权限的适当 IAM 角色:

export PROJECT=$(gcloud info --format='value(config.project)')
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT --format 'value(projectNumber)')
export CB_SA_EMAIL=$PROJECT_NUMBER@cloudbuild.gserviceaccount.com
gcloud services enable cloudbuild.googleapis.com
gcloud services enable compute.googleapis.com
gcloud projects add-iam-policy-binding $PROJECT --member=serviceAccount:$CB_SA_EMAIL --role='roles/iam.serviceAccountUser' --role='roles/compute.instanceAdmin.v1' --role='roles/iam.serviceAccountActor'

然后您可以使用类似于此的内容配置您的构建步骤:

steps:
- name: gcr.io/$PROJECT_ID/remote-builder
  env:
    - COMMAND=sudo supervisorctl restart

您还可以在 Github 存储库的 examples 部分找到更多信息。