Skip to content

Instantly share code, notes, and snippets.

@jbergen
Last active July 11, 2023 04:08
Show Gist options
  • Save jbergen/31327cdceda10f0b881b21813b10c279 to your computer and use it in GitHub Desktop.
Save jbergen/31327cdceda10f0b881b21813b10c279 to your computer and use it in GitHub Desktop.
Example code for programmatically creating a Cloud Run Job in Node
/**
* This code can help you determine how to create a job programmatically.
*
* Tip: use `runClient.getJob({ jobName });` to retrieve an existing job struct to see what it looks like.
* You can also look a the YAML tab in Job Details in the UI.
*/
// where the built job container image is stored
const IMAGE_URL = "path-to-built-image";
// the data you want to pass to the job. An array. Each item will be passed into a separate container/task.
const jobData = ['foo', 'bar'];
// the job struct
const job = {
template: {
// parallelism: 0,
taskCount: jobData.length,
template: {
containers: [{
args: jobData,
image: IMAGE_URL,
resources: {
limits: {
cpu: "1000m",
memory: "512Mi"
},
cpuIdle: false,
startupCpuBoost: false
},
env: [
{
name: "A container secret",
valueSource: {
secretKeyRef: {
secret: "SUPER_SECRET",
version: "latest"
}
},
values: "valueSource"
}
],
}],
timeout: {
seconds: "1800",
nanos: 0
},
serviceAccount: 'my-custom-service-account@you.iam.gserviceaccount.com', // optional
maxRetries: 3,
retries: "maxRetries"
}
}
};
const jobId = 'my-job-id';
const projectId = 'my-project-id';
const location = 'us-central1';
const parent = `projects/${projectId}/locations/${location}`;
// Instantiate & run request
const runClient = new JobsClient();
runClient.createJob({ parent, job, jobId });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment