如何在管道中启动 GCP VM?

How to start a GCP VM in a pipeline?

我觉得这个奇怪的东西不适合用于 GCP 的情况。所以我需要用 Google Cloud Platform 做一些事情。我们使用比办公室 Ubuntu VM 强大得多的 VM 来构建 yocto 构建。我无法弄清楚在 google 云中打开 VM 的正确 .yaml 是什么。管道应该 运行 来自 bitbucket 并且应该有以下内容

(伪代码)

start up the vm in gcp && ssh builder@server 
cd  ./repo/build
start build && push build image to repo server
push logs to pipeline
shutdown

我知道 google 云构建,但我们有一些依赖项可能会或多或少地降低效率,现在我大致了解了我的 yaml 应该是什么样子,但我可以使用一些更好的指针。正如我确定这是错误的。

steps:
  - name: 'gcloud compute instances start build-server-turnoff-when-unused' 

  - name: buildstep
    script: /bin/bash build.sh

  - name: 'send logs'
    script: /bin/bash sendlogs.sh
    

  - name: gcloud compute instances stop build-server-turnoff-when-unused'

我想知道是否有人以前做过这样的事情并且可以帮助我?

假设您的目录如下所示:

.
./cloudbuild.yaml
./repo
./repo/build
./repo/build/build.sh
./repo/build/sendLogs.sh

您的 yaml 文件应如下所示:

steps:

#0 Start Instance
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'instances', 'start', 'INSTANCE', '--zone', 'ZONE']

#1 Build Step
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  dir: 'repo/build'
  args: ['./build.sh']

#2 Send Logs
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: 'bash'
  dir: 'repo/build'
  args: ['./sendLogs.sh']

#3 Stop Instance
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['compute', 'instances', 'stop', INSTANCE, '--zone', 'ZONE']

在这种情况下,我们在构建步骤中使用 dir 字段来设置在 运行 脚本时使用的工作目录。此外,请确保您的 Cloud Build 服务帐户在 IAM 上具有 Compute Admin 角色,以便您可以在构建步骤中启动和停止 Compute Engine 实例。

服务帐户名称:

project-number@cloudbuild.gserviceaccount.com

我对 bitbucket 中管道的确切工作方式有一点错误的理解,我发现我可以只下载 google-cloud sdk 并通过管道发出命令。

image: google/cloud-sdk:latest 

pipelines:
  default:
    - step:
        name: "Start build instance"
        script:
          - echo ${GCLOUD_JSON_KEY} > client-secret.json
          - gcloud auth activate-service-account --key-file client-secret.json
          - gcloud config set project $project-name
          - 'gcloud compute instances start build-serve --zone=america-west4-b'