R/cloudbuild_schedule.R
cr_schedule_pubsub.Rd
Create a PubSub Target object for Cloud Scheduler
cr_schedule_pubsub(
topicName,
PubsubMessage = NULL,
data = NULL,
attributes = NULL,
projectId = cr_project_get()
)
The name of the Cloud Pub/Sub topic or a Topic object from topics_get
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.
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 for PubsubMessage.
The projectId for where the topic sits
A PubsubTarget object for use within cr_schedule
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.
Other Cloud Scheduler functions:
HttpTarget()
,
Job()
,
PubsubTarget()
,
cr_build_schedule_http()
,
cr_run_schedule_http()
,
cr_schedule_delete()
,
cr_schedule_get()
,
cr_schedule_list()
,
cr_schedule_pause()
,
cr_schedule_run()
,
cr_schedule()
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
)
)
}