Create a trigger from a Pub/Sub topic
cr_buildtrigger_pubsub(
topic,
serviceAccountEmail = NULL,
projectId = cr_project_get()
)
The name of the Cloud Pub/Sub topic or a Topic object from topics_get
Service account that will make the push request.
The GCP project the topic is created within
When using a PubSub trigger, you can use data within your PubSub message in substitution variables within the build. The data from pubsub is available in the variable value: $(body.message.data.x)
when x is a field in the pubsub message.
Other BuildTrigger functions:
BuildTrigger()
,
GitHubEventsConfig()
,
cr_buildtrigger_copy()
,
cr_buildtrigger_delete()
,
cr_buildtrigger_edit()
,
cr_buildtrigger_get()
,
cr_buildtrigger_list()
,
cr_buildtrigger_repo()
,
cr_buildtrigger_run()
,
cr_buildtrigger_webhook()
,
cr_buildtrigger()
# create build object
cloudbuild <- system.file("cloudbuild/cloudbuild_substitutions.yml",
package = "googleCloudRunner"
)
the_build <- cr_build_make(cloudbuild)
# this build includes substitution variables that read from pubsub message var1
the_build
#> ==CloudBuildObject==
#> substitutions:
#> _VAR1 : $(body.message.data.var1)
#> steps:
#> - name: alpine
#> id: Hello Cloud Build
#> args:
#> - echo
#> - Hello Cloud Build
#> - name: rocker/r-base
#> id: Hello R
#> args:
#> - Rscript
#> - -e
#> - paste0('From PubSub message field1:', '${_VAR1}')
# using googlePubSubR to create pub/sub topic if needed
if (FALSE) {
library(googlePubsubR)
pubsub_auth()
topics_create("test-topic")
}
# create build trigger that will work from pub/subscription
pubsub_trigger <- cr_buildtrigger_pubsub("test-topic")
pubsub_trigger
#> ==CloudBuildTriggerPubSubConfig==
#> topic: projects/my-project/topics/test-topic
if (FALSE) {
cr_buildtrigger(the_build, name = "pubsub-triggered-subs", trigger = pubsub_trigger)
}
# make base64 encoded json for pubsub
library(jsonlite)
library(googlePubsubR)
#> ! GCP_PROJECT environment not found, please set it up before starting!
#> ! googlePubsubR: GCP_AUTH_FILE environment variable not found, please set it up before
#> authenticating
# the message with the var1 that will be passed into the Cloud Build via substitution
message <- toJSON(list(var1 = "hello mum"))
# turning into JSON and encoding
send_me <- msg_encode(message)
if (FALSE) {
# send a PubSub message with the encoded data message
topics_publish(PubsubMessage(send_me), "test-topic")
# did it work? After a while should see logs if it did
cr_buildtrigger_logs("pubsub-triggered-subs")
}