Google Cloud Build Operators¶
The Google Cloud Build is a service that executes your builds on Google Cloud Platform infrastructure. Cloud Build can import source code from Google Cloud Storage, Cloud Source Repositories, execute a build to your specifications, and produce artifacts such as Docker containers or Java archives.
Prerequisite Tasks¶
CloudBuildCancelBuildOperator¶
Cancels a build in progress.
For parameter definition, take a look at
CloudBuildCancelBuildOperator
Using the operator¶
Cancel a build in progress with the
CloudBuildCancelBuildOperator
operator.
cancel_build = CloudBuildCancelBuildOperator(
task_id="cancel_build",
id_=cast(str, XComArg(create_build_without_wait, key="id")),
project_id=PROJECT_ID,
)
You can use Jinja templating with
project_id
, id_
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildCreateBuildOperator¶
Starts a build with the specified configuration. This generated build ID of the created build, as the result of this operator, is not idempotent.
For parameter definition, take a look at
CloudBuildCreateBuildOperator
Build configuration¶
In order to trigger a build, it is necessary to pass the build configuration.
create_build_from_storage_body = {
"source": {"storage_source": GCP_SOURCE_ARCHIVE_URL},
"steps": [{"name": "ubuntu", "args": ["echo", "Hello world"]}],
}
In addition, a build can refer to source stored in Google Cloud Source Repositories.
create_build_from_repo_body: dict[str, Any] = {
"source": {"repo_source": {"repo_name": GCP_SOURCE_REPOSITORY_NAME, "branch_name": "master"}},
"steps": [{"name": "ubuntu", "args": ["echo", "Hello world"]}],
}
Read Build Configuration Overview to understand all the fields you can include in a build config file.
Using the operator¶
Trigger a build is performed with the
CloudBuildCreateBuildOperator
operator.
create_build_from_storage = CloudBuildCreateBuildOperator(
task_id="create_build_from_storage", project_id=PROJECT_ID, build=create_build_from_storage_body
)
You can use Jinja templating with
project_id
, build
, gcp_conn_id
, impersonation_chain
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
create_build_from_storage_result = BashOperator(
bash_command=f"echo {cast(str, XComArg(create_build_from_storage, key='results'))}",
task_id="create_build_from_storage_result",
)
By default, after the build is created, it will wait for the build operation to complete. If there is no need to wait for complete, you can pass wait=False as example shown below.
create_build_without_wait = CloudBuildCreateBuildOperator(
task_id="create_build_without_wait",
project_id=PROJECT_ID,
build=create_build_from_repo_body,
wait=False,
)
CloudBuildCreateBuildTriggerOperator¶
Creates a new Cloud Build trigger. This generated build trigger ID of the created build trigger, as the result of this operator, is not idempotent.
For parameter definition, take a look at
CloudBuildCreateBuildTriggerOperator
Using the operator¶
Creates a new Cloud Build trigger with the
CloudBuildCreateBuildTriggerOperator
operator.
create_build_trigger = CloudBuildCreateBuildTriggerOperator(
task_id="create_build_trigger", project_id=PROJECT_ID, trigger=create_build_trigger_body
)
You can use Jinja templating with
project_id
, trigger
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildDeleteBuildTriggerOperator¶
Deletes a Cloud Build trigger by its project ID and trigger ID.
For parameter definition, take a look at
CloudBuildDeleteBuildTriggerOperator
Using the operator¶
Deletes a new Cloud Build trigger with the
CloudBuildDeleteBuildTriggerOperator
operator.
delete_build_trigger = CloudBuildDeleteBuildTriggerOperator(
task_id="delete_build_trigger",
project_id=PROJECT_ID,
trigger_id=build_trigger_id,
)
You can use Jinja templating with
project_id
, trigger_id
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildGetBuildOperator¶
Returns information about a previously requested build.
For parameter definition, take a look at
CloudBuildGetBuildOperator
Using the operator¶
Returns information about a previously requested build with the
CloudBuildGetBuildOperator
operator.
get_build = CloudBuildGetBuildOperator(
task_id="get_build",
id_=cast(str, XComArg(retry_build, key="id")),
project_id=PROJECT_ID,
)
You can use Jinja templating with
project_id
, id_
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildGetBuildTriggerOperator¶
Returns information about a Cloud Build trigger.
For parameter definition, take a look at
CloudBuildGetBuildTriggerOperator
Using the operator¶
Returns information about a Cloud Build trigger with the
CloudBuildGetBuildTriggerOperator
operator.
get_build_trigger = CloudBuildGetBuildTriggerOperator(
task_id="get_build_trigger",
project_id=PROJECT_ID,
trigger_id=build_trigger_id,
)
You can use Jinja templating with
project_id
, trigger_id
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildListBuildTriggersOperator¶
Lists all the existing Cloud Build triggers.
For parameter definition, take a look at
CloudBuildListBuildTriggersOperator
Using the operator¶
Lists all the existing Cloud Build triggers with the
CloudBuildListBuildTriggersOperator
operator.
list_build_triggers = CloudBuildListBuildTriggersOperator(
task_id="list_build_triggers", project_id=PROJECT_ID, location="global", page_size=5
)
You can use Jinja templating with
location
, project_id
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildListBuildsOperator¶
Lists previously requested builds.
For parameter definition, take a look at
CloudBuildListBuildsOperator
Using the operator¶
Lists previously requested builds with the
CloudBuildListBuildsOperator
operator.
list_builds = CloudBuildListBuildsOperator(
task_id="list_builds", project_id=PROJECT_ID, location="global"
)
You can use Jinja templating with
location
, project_id
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildRetryBuildOperator¶
Creates a new build based on the specified build. This method creates a new build using the original build request, which may or may not result in an identical build.
For parameter definition, take a look at
CloudBuildRetryBuildOperator
Using the operator¶
Creates a new build based on the specified build with the
CloudBuildRetryBuildOperator
operator.
retry_build = CloudBuildRetryBuildOperator(
task_id="retry_build",
id_=cast(str, XComArg(cancel_build, key="id")),
project_id=PROJECT_ID,
)
You can use Jinja templating with
project_id
, id_
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildRunBuildTriggerOperator¶
Runs a trigger at a particular source revision.
For parameter definition, take a look at
CloudBuildRunBuildTriggerOperator
Using the operator¶
Runs a trigger at a particular source revision with the
CloudBuildRunBuildTriggerOperator
operator.
run_build_trigger = CloudBuildRunBuildTriggerOperator(
task_id="run_build_trigger",
project_id=PROJECT_ID,
trigger_id=build_trigger_id,
source=create_build_from_repo_body["source"]["repo_source"],
)
You can use Jinja templating with
project_id
, trigger_id
, source
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.
CloudBuildUpdateBuildTriggerOperator¶
Updates a Cloud Build trigger by its project ID and trigger ID.
For parameter definition, take a look at
CloudBuildUpdateBuildTriggerOperator
Using the operator¶
Updates a Cloud Build trigger with the
CloudBuildUpdateBuildTriggerOperator
operator.
create_build_trigger = CloudBuildCreateBuildTriggerOperator(
task_id="create_build_trigger", project_id=PROJECT_ID, trigger=create_build_trigger_body
)
You can use Jinja templating with
project_id
, trigger_id
, trigger
, gcp_conn_id
parameters which allows you to dynamically determine values. The result is saved to XCom, which allows it
to be used by other operators.