Create a trigger from a Pub/Sub topic

cr_buildtrigger_pubsub(
  topic,
  serviceAccountEmail = NULL,
  projectId = cr_project_get()
)

Arguments

topic

The name of the Cloud Pub/Sub topic or a Topic object from topics_get

serviceAccountEmail

Service account that will make the push request.

projectId

The GCP project the topic is created within

Details

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.

Examples


# 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")
}