简体   繁体   中英

How to start a GCP VM in a pipeline?

I am kinda having this weird not meant to use for case with GCP. So there is a few things I need to do with Google Cloud Platform. We use an much stronger than at the office Ubuntu VM to build a yocto build. I can somehow not figure out what the proper .yaml is to turn on a VM in google cloud. The pipeline should run from bitbucket and is supposed to the following things

(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. 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:

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. 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.

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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