Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent cloud build from running builds in parallel?

We are using cloud build for continuous deployment on GCP. When pushing commits to fast (e.g. on development) the triggered builds are running in parallel. Sometimes those interfere which one another. For example when two app engine deployments are running at the same time.

Is there a way or best practise to force builds which are triggered from the same build trigger to run one after another?

Regards, Carsten

like image 413
Carsten Rietz Avatar asked Apr 29 '19 14:04

Carsten Rietz


People also ask

What is the best approach to speed up the installation process of application dependency in a docker?

Using a cached Docker image. The easiest way to increase the speed of your Docker image build is by specifying a cached image that can be used for subsequent builds.

Which is the best file format to define Cloud Build structure?

A build config file defines the fields that are needed for Cloud Build to perform your tasks. You'll need a build config file if you're starting builds using the gcloud command-line tool or build triggers. You can write the build config file using the YAML or the JSON syntax.


1 Answers

I've done this by adding an initial step on my cloudbuild.yaml file. What it does is:

  • Gets all the ongoing build id via gcloud builds list --ongoing --format='value(id)' --filter="substitutions.TRIGGER_NAME=$TRIGGER_NAME".
  • Loop through it but skip the first one, the list is sorted by latest created time so it won't stop the latest build which is the first index
  • Run gcloud builds cancel ${on_going_build[i]} to cancel the build

Please see the cloudbuild.yaml below

steps:
  - id: "Stop Other Ongoing Build"
    name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args:
    - -c
    - |
      on_going_build=($(gcloud builds list --ongoing --format='value(id)' --filter="substitutions.TRIGGER_NAME=$TRIGGER_NAME" | xargs))
      for (( i=0; i<${#on_going_build[@]}; i++ )); do
        if [ "$i" -gt "0" ]; then # skip current
          echo "Cancelling build ${on_going_build[i]}"

          gcloud builds cancel ${on_going_build[i]}
        fi
      done
like image 159
Marc Joseph Datario Avatar answered Sep 17 '22 12:09

Marc Joseph Datario