Create a PubSub Target object for Cloud Scheduler

cr_schedule_pubsub(
  topicName,
  PubsubMessage = NULL,
  data = NULL,
  attributes = NULL,
  projectId = cr_project_get()
)

Arguments

topicName

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

PubsubMessage

A PubsubMessage object generated via PubsubMessage. If used, then do not send in `data` or `attributes` arguments as will be redundant since this variable will hold the information.

data

The message payload for PubsubMessage. An R object that will be turned into JSON via [jsonlite] and then base64 encoded into the PubSub format.

attributes

Attributes for PubsubMessage.

projectId

The projectId for where the topic sits

Value

A PubsubTarget object for use within cr_schedule

Details

You can parametrise builds by sending in values within PubSub. To read the data in the message set a substitution variable that picks up the data. For example _VAR1=$(body.message.data.var1)

If your schedule to PubSub fails with a permission error, try turning the Cloud Scheduler API off and on again the Cloud Console, which will refresh the Google permissions.

Examples

cr_project_set("my-project")
#>  2022-01-20 21:36:22 > ProjectId set to my-project
#> [1] "my-project"
cr_bucket_set("my-bucket")
#>  2022-01-20 21:36:22 > Bucket set to my-bucket
#> [1] "my-bucket"
cloudbuild <- system.file("cloudbuild/cloudbuild.yaml",
  package = "googleCloudRunner"
)
bb <- cr_build_make(cloudbuild)
if (FALSE) {
# create a pubsub topic either in Google Console webUI or library(googlePubSubR)
library(googlePubsubR)
pubsub_auth()
topics_create("test-topic")
}

# create build trigger that will watch for messages to your created topic
pubsub_trigger <- cr_buildtrigger_pubsub("test-topic")
pubsub_trigger
#> ==CloudBuildTriggerPubSubConfig==
#> topic:  projects/my-project/topics/test-topic 
if (FALSE) {
# create the build trigger with in-line build
cr_buildtrigger(bb, name = "pubsub-triggered", trigger = pubsub_trigger)


# create scheduler that calls the pub/sub topic
cr_schedule("cloud-build-pubsub",
  "15 5 * * *",
  pubsubTarget = cr_schedule_pubsub("test-topic")
)
}

# builds can be also parametrised to respond to parameters within your pubsub topic
# this cloudbuild echos back the value sent in 'var1'
cloudbuild <- system.file("cloudbuild/cloudbuild_substitutions.yml",
  package = "googleCloudRunner"
)
the_build <- cr_build_make(cloudbuild)

# var1 is sent via Pubsub to the buildtrigger
message <- list(var1 = "hello mum")
send_me <- jsonlite::base64_enc(jsonlite::toJSON(message))

# create build trigger that will work from pub/subscription
pubsub_trigger <- cr_buildtrigger_pubsub("test-topic")
if (FALSE) {
cr_buildtrigger(the_build, name = "pubsub-triggered-subs", trigger = pubsub_trigger)

# create scheduler that calls the pub/sub topic with a parameter
cr_schedule("cloud-build-pubsub",
  "15 5 * * *",
  pubsubTarget = cr_schedule_pubsub("test-topic",
    data = send_me
  )
)
}