Trigger Google Cloud Build with Google Cloud Scheduler periodically
Asked Answered
K

3

10

Is it somehow possible to trigger a Google Cloud Build with Google Cloud Scheduler periodically?

I can't find anything related to it on the Internet.

Killie answered 27/8, 2019 at 19:50 Comment(0)
K
8

In Cloud Scheduler we perform a HTTP request on the project's build trigger: https://cloud.google.com/cloud-build/docs/api/reference/rest/v1/projects.triggers/run

For authentication we use a service account.

Killie answered 30/8, 2019 at 14:34 Comment(0)
B
14

The first option is to create a schedule to trigger a build:

gcloud scheduler jobs create http ${PROJECT_ID}-run-trigger \
    --schedule='0 12 * * *' \
    --uri=https://cloudbuild.googleapis.com/v1/projects/${PROJECT_ID}/triggers/${TRIGGER_ID}:run \
    --message-body='{\"branchName\": \"${BRANCH_NAME}\"}' \
    --oauth-service-account-email=${PROJECT_ID}@appspot.gserviceaccount.com \
    --oauth-token-scope=https://www.googleapis.com/auth/cloud-platform

Note that you can almost run this from within a cloud build. PROJECT_ID is the name of the project and BRANCH_NAME is the name of the branch (development, master, etc.). Both are available from within your cloud build pipeline. TRIGGER_ID can be fetched with the following command:

gcloud beta builds triggers list --format json

Additional to branchName, you can also specify other attributes in the message body, giving you more flexibility:

  • commitSha
  • dir
  • invertRegex
  • projectId
  • repoName
  • substitutions
  • tagName

The second option is to submit a cloudbuild on a schedule:

gcloud scheduler jobs create http ${PROJECT_ID}-run-build \
    --schedule='0 12 * * *' \
    --uri=https://cloudbuild.googleapis.com/v1/projects/${PROJECT_ID}/builds \
    --message-body-from-file=cloudbuild.json \
    --message-body="{\"branchName\": \"${BRANCH_NAME}\"} \
    --oauth-service-account-email=${PROJECT_ID}@appspot.gserviceaccount.com \
    --oauth-token-scope=https://www.googleapis.com/auth/cloud-platform

Your cloudbuild.json can look something like this:

{
    "timeout": "60s",
    "steps": [
        {
            "name": "gcr.io/cloud-builders/gcloud",
            "entrypoint": "bash",
            "args": [
                "-c",
                "echo "Hello"
            ]
        },
        {
            "name": "gcr.io/cloud-builders/gcloud",
            "entrypoint": "bash",
            "args": [
                "-c",
                "echo "World"
            ]
        }
    ],
    "substitutions": {
        "BRANCH_NAME": "$BRANCH_NAME"
    }
}
Bourke answered 14/2, 2020 at 11:31 Comment(2)
This is great but I needed to add quotes around the message body. --message-body="{\"branchName\": \"${BRANCH_NAME}\"}"Aery
thanks ... In my case it was --message-body='{ "branchName": "main" }'Bramble
K
8

In Cloud Scheduler we perform a HTTP request on the project's build trigger: https://cloud.google.com/cloud-build/docs/api/reference/rest/v1/projects.triggers/run

For authentication we use a service account.

Killie answered 30/8, 2019 at 14:34 Comment(0)
F
2

You can do that by making the Cloud Scheduler job target the Cloud Build projects.builds.create API to manually start your builds. With this you can pass a Build instance through the request body to specify your build.

Keep in mind that you will need to authenticate your request, check the Using authentication with HTTP Targets documentation for more information on how to do that.

Fulmar answered 28/8, 2019 at 9:25 Comment(1)
A better way is probably to run an existing build trigger: cloud.google.com/cloud-build/docs/api/reference/rest/v1/…Killie

© 2022 - 2024 — McMap. All rights reserved.