Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Can I access the pubsub message in a Google Cloud Run Job triggered by a pubsub subscription

Hi there,

I would like to use pubsub to trigger in a push subscription a Run Job. This works without issues, through a http endpoint like this:
https://us-central1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/[projectid]/jobs/[jobna...

I have not found a way how to access the pubsub message from inside the job. Do I miss something?

As a workaround I found:

  1. Create a Run Service that listens to the http endpoint, the message is in the body of the html requst. Then trigger the jobs from there
  2. A cloud function is called from a push pubsub subscription, there the message is extracted and with the gcloud run jobs execute and add parameters that match the subscription
  3. Let a run job be triggered by scheduler in a short interval. In each run check a pull subscription for messages and start the jobs necessary with gcloud run jobs execute.

I would prefer to use a push pubsub subscription with a http endpoint for a job and access inside the job the message most. But I do not find an api for this. So I fall back for option 2.

Is this the best way to go or is there a way to retrieve the message inside a job I do not see in my setup?

Solved Solved
0 3 1,007
1 ACCEPTED SOLUTION

Hi Marc223, We have a private preview going on where you can get the content of the message in an environment variable in your container. Does that solution work for you? If yes, you can sign up here to try the private preview: https://docs.google.com/forms/d/e/1FAIpQLSd1pHHel6MVWV5FQI0nDQ97vuLcMzFYHximT6TTJlctQKFBhw/viewform

View solution in original post

3 REPLIES 3

Hi @Marc223,

Welcome to the Google Cloud community!

Pub/Sub with Cloud Run is a good starting point to access Pub/Sub messages inside a Cloud Run job triggered by a Pub/Sub push subscription. 

Here are the workarounds based on Google Cloud Documentation:

  1. Create triggers with Eventarc:
    • Leverage Eventarc to trigger your Cloud Run job upon receiving messages from a Pub/Sub topic.
    • Eventarc automatically delivers the message data to your Cloud Run service as part of the event payload.
  2. Cloud Scheduler with pull subscription:
    • Configure a Cloud Scheduler job to run periodically (e.g., every minute).
    • Within the Cloud Run job triggered by Cloud Scheduler, use the Pub/Sub API to pull messages from the subscription.
    • Process the retrieved messages as needed.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Hi Marc223, We have a private preview going on where you can get the content of the message in an environment variable in your container. Does that solution work for you? If yes, you can sign up here to try the private preview: https://docs.google.com/forms/d/e/1FAIpQLSd1pHHel6MVWV5FQI0nDQ97vuLcMzFYHximT6TTJlctQKFBhw/viewform

Hi Chieko and Knet,

Currently I have pubsub with push http subscripiton -> cloud function -> run job with args. Inside the job I get the args and have access to the message.

Yes I am interested in the private preview, so I can get rid of my cloud function. I fill out the form.
Thanks!