简体   繁体   English

如何在管道中启动 GCP VM?

[英]How to start a GCP VM in a pipeline?

I am kinda having this weird not meant to use for case with GCP.我有点奇怪,不打算用于 GCP 的情况。 So there is a few things I need to do with Google Cloud Platform.所以我需要用 Google Cloud Platform 做一些事情。 We use an much stronger than at the office Ubuntu VM to build a yocto build.我们使用比办公室强大得多的 Ubuntu VM 来构建 yocto 版本。 I can somehow not figure out what the proper .yaml is to turn on a VM in google cloud.我无法弄清楚在谷歌云中打开虚拟机的正确 .yaml 是什么。 The pipeline should run from bitbucket and is supposed to the following things管道应该从 bitbucket 运行,并且应该执行以下操作

(pseudo code) (伪代码)

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

I am aware of google cloud build but we have some dependencies that would likely make this more or less inefficient, now I have a general idea how my yaml is suppose to look like but I could use some better pointers in this.我知道谷歌云构建,但我们有一些依赖关系可能会或多或少地降低效率,现在我大致了解了我的 yaml 应该是什么样子,但我可以在这方面使用一些更好的指针。 As in I am sure this is wrong.正如我确定这是错误的。

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'

I was wondering if someone has done something like this before and could help me out?我想知道是否有人以前做过这样的事情并且可以帮助我吗?

Assuming that your directory looks like this:假设您的目录如下所示:

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

Your yaml file should look like this:您的 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']

In this case we used the dir field in the build step to set a working directory to use when running the scripts.在这种情况下,我们在构建步骤中使用dir字段来设置运行脚本时要使用的工作目录。 Also, make sure that your Cloud Build service account has a Compute Admin role on IAM so you can start and stop your Compute Engine instance on your build steps.此外,请确保您的 Cloud Build 服务帐户在 IAM 上具有Compute Admin角色,以便您可以在构建步骤中启动和停止您的 Compute Engine 实例。

Service account name:服务帐号名称:

project-number@cloudbuild.gserviceaccount.com

I had a bit of a wrong understanding how pipelines in bitbucket exactly work, I figured out I could just download the google-cloud sdk and give commands through the pipeline.我对 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM