google-cloud-platformgoogle-cloud-rungoogle-cloud-run-jobs

How to pass environment variables when executing a Google Cloud Run Job using Node.js or Python client?


I’m trying to execute a Google Cloud Run job and pass environment variables to it, similar to how I would using the gcloud CLI:

gcloud run jobs execute <test-job> --update-env-vars key1=value1,key2=value2

I want to achieve the same functionality using either the Node.js or Python client libraries for Google Cloud Run.

Here’s the auto-generated code snippet for running a job using the Node.js client:

/**
// const overrides = {};

// Imports the Run library
const {JobsClient} = require('@google-cloud/run').v2;

// Instantiates a client
const runClient = new JobsClient();

async function callRunJob() {
  // Construct request
  const request = {
    name,
    // overrides,
  };

  // Run request
  const [operation] = await runClient.runJob(request);
  const [response] = await operation.promise();
  console.log(response);
}

callRunJob();

Reference: RunJob method documentation

How can I modify this code to pass environment variables to the job execution, similar to using --update-env-vars in the gcloud CLI? I’m looking for solutions in either Node.js or Python.


Solution

  • you must pass the override parameters like this

    
      const request = {
        name: job,
        overrides: {
          containerOverrides: {
            env: [
                {
                    name: <envVar.name>,
                    value: <envVar.value>,
                 }],
          },
        },
      };