You can use a Cloud Build trigger to automatically start a build and deploy a workflow from a Git repository. You can configure the trigger to deploy your workflow on any change to the source repository, or deploy the workflow only when the change matches specific criteria.
This approach can help you manage your deployment lifecycle. For example, you can deploy changes to a workflow in a staging environment, run tests against that environment, and then incrementally roll out these changes to the production environment.
Before you begin
These instructions assume you have the
Cloud Build Editor role
(roles/cloudbuild.builds.editor
) in your Google Cloud project so that you
can create triggers. You also need a workflow in a source repository such as
GitHub or Bitbucket.
Console
Enable the Cloud Build and Workflows APIs.
Grant the Workflows Admin role (
roles/workflows.admin
) to the Cloud Build service account:In the Google Cloud console, go to the IAM page.
Select your project.
In the row for the Cloud Build service account (
PROJECT_NUMBER@cloudbuild.gserviceaccount.com
), click Edit principal.Click
Add another role.In the Role list, select the Workflows Admin role.
Click Save.
Grant the Service Account User role (
roles/iam.serviceAccountUser
) on the Compute Engine default service account to the Cloud Build service account. When you have enabled the Compute Engine API, the Compute Engine default service account isPROJECT_NUMBER-compute@developer.gserviceaccount.com
.In the Google Cloud console, go to the Service Accounts page.
Select your project.
Click the email address of the Compute Engine default service account (
PROJECT_NUMBER-compute@developer.gserviceaccount.com
).Click the Permissions tab.
Click the
Grant access button.To add a new principal, enter the email address of your service account (
SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
).In the Select a role list, select the Service Accounts > Service Account User role.
Click Save.
gcloud
Enable the Cloud Build and Workflows APIs.
gcloud services enable cloudbuild.googleapis.com \ workflows.googleapis.com
Grant the Workflows Admin role (
roles/workflows.admin
) to the Cloud Build service account:PROJECT_NUMBER=$(gcloud projects describe PROJECT_ID --format='value(projectNumber)') gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role=roles/workflows.admin
Replace
PROJECT_ID
with the ID of your Google Cloud project.Grant the Service Account User role (
roles/iam.serviceAccountUser
) on the Compute Engine default service account to the Cloud Build service account. When you have enabled the Compute Engine API, the Compute Engine default service account isPROJECT_NUMBER-compute@developer.gserviceaccount.com
.gcloud iam service-accounts add-iam-policy-binding \ $PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --member=serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role=roles/iam.serviceAccountUser
Connect to your source repository
You must connect Cloud Build to your source repository so that Cloud Build can automate builds in response to events that happen in the repository.
Complete the following steps to connect to GitHub or Bitbucket:
In the Google Cloud console, go to the Cloud Build Triggers page:
If necessary, select your project and click Open.
From the Region list, select the region where you would like to create your trigger.
Click Connect repository.
Select the source repository where you've stored your source code.
For example: GitHub (Cloud Build GitHub App)
Click Continue.
Authenticate to your source repository with your username and password.
If you are signing into GitHub, you are asked to authorise the Google Cloud Build GitHub App to access your GitHub account to proceed.
From the list of available repositories, select the desired repository, and then click OK.
For external repositories such as GitHub and Bitbucket, you must have owner-level permissions for the Google Cloud project in which you're working.
Read the disclaimer and select the checkbox next to it to indicate that you consent to the terms.
Click Connect.
To continue creating a build trigger to automate builds for the source code in the repository, click Create a trigger. Otherwise, click Done.
Create a Cloud Build configuration file
A build configuration file defines the fields that are needed when using a build trigger to start a build. Create the config file in your project root directory and write it using either YAML or JSON.
For example, the following config file deploys and runs a test workflow, and then uses a script to check the output. If the test passes, the workflow is deployed:
The $BRANCH_NAME
and $SHORT_SHA
substitution variables
are populated by Cloud Build when a build is triggered from a Git
repository. They represent the name of your branch, and the first seven
characters of the commit ID associated with your build, respectively.
The $_WORKFLOW_NAME
substitution variable allows you to re-use a config file
with different variable values. You can specify its value when you create the
build trigger.
For more information, see Create a build configuration file.
Create a build trigger
You can automate the deployment of your workflow by creating a Cloud Build trigger.
To create a build trigger for the config file in the previous section:
In the Google Cloud console, go to the Cloud Build Triggers page:
Click Create trigger.
In the Name field, enter a name for your trigger.
For Event, select the event to invoke your trigger.
For example: Push to a branch
For Source, select your repository, and the branch or tag name that will start your trigger. You can use a regular expression to specify a match to a branch or tag.
For example:
GoogleCloudPlatform/workflows-demos
(repository) and^main$|^staging$
(matches themain
andstaging
branches)Expand the Show included and ignored files filters section and specify your workflow as an included file so that when it is changed, a build is invoked.
For example:
gitops/workflow.yaml
For Configuration, select Cloud Build configuration file (YAML or JSON) as the type, and Repository as the location.
In the Cloud Build configuration file location field, specify the location of your file.
For example:
gitops/cloudbuild.yaml
Optionally, to add a substitution variable, click Add variable and specify a key and value combination.
For example:
_WORKFLOW_NAME
(variable) andworkflows-gitops
(value)To save your build trigger, click Create.
When any changes are pushed to a workflow in the specified branch of the Git repository, it will automatically trigger Cloud Build to deploy the workflow.
For more information, see Create and manage build triggers.
Test the build trigger
You can test the build trigger and config file from the previous sections.
In the
staging
branch of the Git repository, editworkflow.yaml
, and changeHello World
toBye World
:Commit and push the change to the
staging
branch.git add workflow.yaml git commit -m "Update workflow.yaml in staging" git push
The Cloud Build trigger runs and initiates a build.
To confirm the success of the build, in the Google Cloud console, go to the Build history page:
After a build completes, Cloud Build provides an overall status for the build and for each individual build step. For more information, see View build results.
To confirm that a staging workflow is deployed, in the Google Cloud console, go to the Workflows page:
You should see a workflow named
workflows-gitops-staging
listed.To deploy the staging workflow to production, merge the
staging
branch to themain
branch:git checkout main git merge staging git push
Note that because
test-main.sh
is expectingHello World
in the output of the workflow, the build will fail:To successfully deploy a production workflow, in the
staging
branch, editworkflow.yaml
again and change the string back toHello World
.Commit and push the change to the
staging
branch and then merge thestaging
branch to themain
branch.To confirm that a production workflow is deployed, in the Google Cloud console, go to the Workflows page:
You should see a workflow named
workflows-gitops-main
listed.
What's next
- Learn more about Cloud Build.
- Learn how to troubleshoot build errors.
- Learn how to troubleshoot Workflows.