简体   繁体   中英

Google cloud build with Compute Engine

I want to use Cloud Build with a trigger on commit to automatically fetch updated repo and run sudo supervisorctl restart on a Compute Engine instance.

On the Cloud Build settings page, there is an option to connect Compute Engine , but so far I only found examples including Kubernetes Engine and App Engine here .

Is it possible to accomplish? Is it the right way to make updates? Or should I instead restart the instance(s) with a startup-script ?

There's a repo in Github from the cloud-builders-community that may be what you are looking for.

As specified in the aforementioned link, it does connect cloud Build to Compute Engine with the following steps:

  1. A temporary SSH key will be created in your Container Builder workspace

  2. A instance will be launched with your configured flags

  3. The workpace will be copied to the remote instance

  4. Your command will be run inside that instance's workspace

  5. The workspace will be copied back to your Container Builder workspace

You will need to create an appropriate IAM role with create and destroy Compute Engine permissions:

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'

And then you can configure your build step with something similar to this:

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

You can also find more information in the examples section of the Github repo.

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