简体   繁体   中英

Cloud Build API call to check information regarding the latest build

I have been using the Cloud Build API in order to get the latest image information from Google Cloud Build that builds our Google Cloud Kubernetes Deployment Image into our Django Backend Application to trigger a new Job/Pod into our cluster.

Below is the code for to collect the info.

from google.cloud.devtools import cloudbuild_v1

def sample_list_builds():
    # Create a client
    client = cloudbuild_v1.CloudBuildClient()

    # Initialize request argument(s)
    request = cloudbuild_v1.ListBuildsRequest(
        project_id="project_id_value",
    )

    # Make the request
    page_result = client.list_builds(request=request)

    # Handle the response
    for response in page_result:
        print(response)

I just want to exit the loop when the first successful build is found, however I cannot find how to compare against Status.Success. It doesn't seem to be a string. What shall I compare this against?

images: "eu.gcr.io/.../.../...-dev:f2529...0ac00402"
project_id: "..."
logs_bucket: "gs://106...1.cloudbuild-logs.googleusercontent.com"
source_provenance {
}
build_trigger_id: "...-d5fd-47b7-8949-..."
options {
  substitution_option: ALLOW_LOOSE
  logging: LEGACY
  dynamic_substitutions: true
  pool {
  }
}
log_url: "https://console.cloud.google.com/cloud-build/builds/...-1106-44d5-a634-...?project=..."
substitutions {
  key: "BRANCH_NAME"
  value: "staging"
}
substitutions {
  key: "COMMIT_SHA"
  value: "..."
}
substitutions {
  key: "REF_NAME"
  value: "staging"
}
substitutions {
  key: "REPO_NAME"
  value: "videoo-app"
}
substitutions {
  key: "REVISION_ID"
  value: "....aa3f5276deda3c10ac00402"
}
substitutions {
  key: "SHORT_SHA"
  value: "f2529c2"
}
substitutions {
  key: "TRIGGER_BUILD_CONFIG_PATH"
}
substitutions {
  key: "TRIGGER_NAME"
  value: "rmgpgab-videoo-app-dev-europe-west1-...--storb"
}
substitutions {
  key: "_DEPLOY_REGION"
  value: "europe-west1"
}
substitutions {
  key: "_ENTRYPOINT"
  value: "gunicorn -b :$PORT videoo.wsgi"
}
substitutions {
  key: "_GCR_HOSTNAME"
  value: "eu.gcr.io"
}
substitutions {
  key: "_LABELS"
  value: "gcb-trigger-id=...-d5fd-47b7-8949-..."
}
substitutions {
  key: "_PLATFORM"
  value: "managed"
}
substitutions {
  key: "_SERVICE_NAME"
  value: "videoo-app-dev"
}
substitutions {
  key: "_TRIGGER_ID"
  value: "...-d5fd-47b7-8949-..."
}

The following code is not working as expected:

def sample_list_builds():
    # Create a client
    client = cloudbuild_v1.CloudBuildClient()

    # Initialize request argument(s)
    request = cloudbuild_v1.ListBuildsRequest(
        project_id=settings.PROJECT_ID,
    )

    # Make the request
    page_result = client.list_builds(request=request)

    # Handle the response
    for response in page_result:
        print(response.status)
        if response.status=="Status.SUCCESS":
            print(response.results['images']['name'])
            break

How can I compare the status field against Success case?

You should have the status and status_detail in the answer.

if you import the Build type

from google.cloud.devtools.cloudbuild_v1.types import Build

you should be able to use eg Build.Status.SUCCESS and compare that.

As stated here, Cloud Build publishes messages on a Google Pub/Sub topic when your build's state changes. It could also be helpful to take a look at Pull subscriptions.

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