10

I have ran this example using this job

Everything worked well.

Now I am trying to see if there is a way to pass parameters to jobs running on Cloud Run.

I understand I can use the command to create jobs with a --message-body argument like this:

 gcloud scheduler jobs create http JOB_NAME \
  --location REGION \
  --schedule="*/3 * * * *" \
  --uri="https://REGION-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/PROJECT_ID/jobs/CLOUD_RUN_JOB_NAME:run" \
  --http-method POST \
  --oauth-service-account-email  PROJECT-compute@developer.gserviceaccount.com
  --message-body="This is the body"

However while checking the documentation for Cloud Run jobs here.

I don't see parameters being mentioned anywhere. The idea is that depending on a JSON that contains the parameters we can run different kind of jobs (it's a same job that changes its operation based on the parameters)

Rogelio Monter
  • 1,084
  • 7
  • 18
carlos palma
  • 722
  • 3
  • 12
  • 29
  • 1
    I have not tested this but I believe you can pass parameters via `message-body`. For example, create a JSON object, convert to a string and then base64 encode for the body. On the receiving side, you would grab the POST message body, base64decode ... – John Hanley Aug 31 '22 at 22:07
  • 4
    You can't (and I begged the Cloud Run Job PM to implement something, and, obviously, the other alpha testers had the same issue and something will happen :) ). My current solution is to wrap the current batch job in a web server and, as @JohnHanley said, provide a body, parse it, and invoque your batch with that input. – guillaume blaquiere Sep 01 '22 at 12:39
  • Hello Guillaume, how would one go about wrapping the job in a web server? – carlos palma Sep 01 '22 at 13:35
  • 1
    I suggest that you create a feature request to pass parameters from Cloud scheduler to Cloud Run Job. Please use this link for your [reference](https://cloud.google.com/support/docs/issue-trackers#trackers-list). – Sarah Remo Sep 02 '22 at 02:52
  • 1
    Hello @guillaumeblaquiere can you post the comment as an answer so I can accept it? – carlos palma Sep 13 '22 at 18:27
  • @guillaumeblaquiere sorry but what do you mean by “invoque your batch”?, how can I invoke the job with that parsed JSON?, do I need to create a scheduler with that message body?, or does the `job execute` API support a body? – Alejandro Barone May 02 '23 at 02:31

3 Answers3

8

With Cloud Run Jobs, you can't provide parameters (entry points, args or env vars). I discussed that point with the Cloud Run Job PM to implement something, and, obviously, the other alpha testers had the same issue and something will happen :).

My current solution is to wrap the current batch job in a web server. Package that webserver in a container and deploy it on Cloud Run. Then, provide the correct body, with your parameters, parse it, and invoke your batch with those inputs

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
4

You can pass arguments via the args flag.

They provide an example in this codelabs course.

gcloud beta run jobs create screenshot \
    --image=$REGION-docker.pkg.dev/$PROJECT_ID/containers/screenshot:v1 \
    --args="https://example.com" \
    --args="https://cloud.google.com" \
    --tasks=2 \
    --task-timeout=5m \
    --region=$REGION \
    --set-env-vars=BUCKET_NAME=screenshot-$PROJECT_ID \
    --service-account=screenshot-sa@$PROJECT_ID.iam.gserviceaccount.com

This passes an array of args when creating the job and access the right argument by using the CLOUD_RUN_TASK_INDEX variable.

thclark
  • 4,784
  • 3
  • 39
  • 65
David Spiess
  • 600
  • 1
  • 11
  • 21
  • 3
    I don't think this is what's being asked for. This creates a job with arguments, it doesn't allow those arguments to be varied each time you execute that job. – thclark Mar 17 '23 at 11:34
  • I do think it solves the problem. You pass an array of args when creating the job and access the right argument by using the CLOUD_RUN_TASK_INDEX variable. – David Spiess Mar 21 '23 at 08:31
  • Forgive me David, I'd not understood that the task index variable could be specified at runtime. Whilst not fully dynamic, this can meet a number of peoples uses. Downvote->upvote :) – thclark Mar 21 '23 at 08:40
  • how to pass the `CLOUD_RUN_TASK_INDEX` variable during runtime @DavidSpiess? – AnandShiva Jun 19 '23 at 20:29
  • 1
    `CLOUD_RUN_TASK_INDEX` is available as environment variable @AnandShiva – David Spiess Jun 20 '23 at 07:56
0

[UPDATE] Aug 08 2023, this feature (If I'm reading right) is now in preview

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
  • I cannot believe that, as of Aug 16 2023, we have rockets that can go to space and land back autonomously, AI that can analyze land surfaces surfaces and come up with various attack plans based on enemy unit types and their position… but for some reason, sending arguments to a background task is in beta. What the hell Google. – rburhum Aug 16 '23 at 18:44