Stay organized with collections Save and categorize content based on your preferences.
This document explains the basics for Batch job creation: how to create and run a job that is based on a script or container image and use predefined and custom variables. To learn more about creating and running jobs, see Job creation and execution overview.
Before you beginTo get the permissions that you need to create a job, ask your administrator to grant you the following IAM roles:
roles/batch.jobsEditor
) on the projectroles/iam.serviceAccountUser
) on the job's service account, which by default is the default Compute Engine service accountFor more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
For information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs
REST resource. To summarize, a job consists of an array of one or more tasks that all run one or more runnables, which are the executable script(s) and/or container(s) for your job. To cover the basics, this section explains how to create an example job with just one runnable, either a script or a container image:
The example job for both types of jobs contains a task group with an array of 4 tasks. Each task prints a message and its index to the standard output and Cloud Logging. The definition for this job specifies a parallelism of 2, which indicates that the job should run on 2 VMs to allow 2 tasks to run at a time.
Create a basic container jobYou can select or create a container image to provide the code and dependencies for your job to run from any compute environment. For more information, see Working with container images and Running containers on VM instances.
You can create a basic container job using the Google Cloud console, gcloud CLI, Batch API, Go, Java, Node.js, Python, or C++.
ConsoleTo create a basic container job using the Google Cloud console, do the following:
In the Google Cloud console, go to the Job list page.
Click add_box Create. The Create batch job page opens. In the left pane, the Job details page is selected.
Configure the Job details page:
Optional: In the Job name field, customize the job name.
For example, enter example-basic-job
.
Configure the Task details section:
In the New runnable window, add at least one script or container for this job to run.
For example, to add one container, do the following:
Select Container image URL (default).
In the Container image URL field, enter the URL for a container image that you want to run for each task in this job.
For example, to use the busybox
Docker container image, enter the following URL:
gcr.io/google-containers/busybox
Optional: To override the container image's ENTRYPOINT
command, enter a command in the Entry point field.
For example, enter the following:
/bin/sh
Optional: To override the container image's CMD
command, do the following:
Select the Override container image's CMD command checkbox. A field appears.
In the field, enter one or more commands, separating each command with a new line.
For example, enter the following commands:
-c
echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.
Click Done.
In the Task count field, enter the number of tasks for this job. The value must be a whole number between 1
and the tasks per task group limit.
For example, enter 4
.
In the Parallelism field, enter the number of tasks to run concurrently. The number cannot be larger than the total number of tasks and must be a whole number between 1
and the parallel tasks per job limit.
For example, enter 2
.
Configure the Resource specifications page:
In the left pane, click Resource specifications. The Resource specifications page opens.
In the VM provisioning model section, select one of the following options for the provisioning model for this job's VMs:
If your job can withstand preemption and you want discounted VMs, select Spot.
Otherwise, select Standard.
For example, select Standard (default).
Select the location for this job:
In the Region field, select a region.
For example, select us-central1 (Iowa)
(default).
In the Zone field, do one of the following:
If you want to restrict this job to run in a specific zone only, select a zone.
Otherwise, select any.
For example, select any (default).
Select one of the following machine families:
For common workloads, click General purpose.
For performance-intensive workloads, click Compute optimized.
For memory-intensive workloads, click Memory optimized.
For accelerator-optimized workloads, click GPUs. For more information, see Create and run a job that uses GPUs.
For example, click General purpose (default).
In the Series field, select a machine series for this job's VMs.
For example, if you selected General purpose for the machine family, select E2 (default).
In the Machine type field, select a machine type for this job's VMs.
For example, if you selected E2 for the machine series, select e2-medium (2 vCPU, 4 GB memory) (default).
Configure the amount of VM resources required for each task:
In the Cores field, enter the amount of vCPUs per task.
For example, enter 1
(default).
In the Memory field, enter the amount of RAM in GB per task.
For example, enter 0.5
(default).
Optional: To review the job configuration, in the left pane, click Preview.
Click Create.
The Job details page displays the job that you created.
gcloudTo create a basic container job using the gcloud CLI, do the following:
Create a JSON file that specifies your job's configuration details. For example, to create a basic container job, create a JSON file with the following contents. For more information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs
REST resource.
gcloud batch jobs submit
command.
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
CONTAINER
}
}
],
"computeResource": {
"cpuMilli": CORES,
"memoryMib": MEMORY
},
"maxRetryCount": MAX_RETRY_COUNT,
"maxRunDuration": "MAX_RUN_DURATION"
},
"taskCount": TASK_COUNT,
"parallelism": PARALLELISM
}
]
}
Replace the following:
CONTAINER
: the container that each task runs. At minimum, a container must specify an image in the imageUri
subfield, but additional subfields might also be required. For more information, see the container
subfields and the example container job in this section.CORES
: Optional. The amount of cores—specifically vCPUs, which usually represent half a physical core—to allocate for each task in milliCPU units. If the cpuMilli
field is not specified, the value is set to 2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If the memoryMib
field is not specified, the value is set to 2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between 0
and 10
. If the maxRetryCount
field is not specified, the value is set to 0
, which means to not retry the task. For more information about the maxRetryCount
field, see Automate task retries.MAX_RUN_DURATION
: Optional. The maximum time a task is allowed to run before being retried or failing, formatted as a value in seconds followed by s
—for example, 3600s
for 1 hour. If the maxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about the maxRunDuration
field, see Limit run times for tasks and runnables using timeouts.TASK_COUNT
: Optional. The number of tasks for the job. The value must be a whole number between 1
and the tasks per task group limit. If the taskCount
field is not specified, the value is set to 1
.PARALLELISM
: Optional. The number of tasks the job runs concurrently. The number cannot be larger than the number of tasks and must be a whole number between 1
and the parallel tasks per job limit. If the parallelism
field is not specified, the value is set to 1
.Create a job by using the gcloud batch jobs submit
command.
gcloud batch jobs submit JOB_NAME \
--location LOCATION \
--config JSON_CONFIGURATION_FILE
Replace the following:
JOB_NAME
: the name of the job.LOCATION
: the location of the job.JSON_CONFIGURATION_FILE
: the path for a JSON file with the job's configuration details.For example, to create a job that runs tasks using the busybox
Docker container image:
Create a JSON file in the current directory named hello-world-container.json
with the following contents:
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
"imageUri": "gcr.io/google-containers/busybox",
"entrypoint": "/bin/sh",
"commands": [
"-c",
"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
]
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
},
"maxRetryCount": 2,
"maxRunDuration": "3600s"
},
"taskCount": 4,
"parallelism": 2
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
},
"labels": {
"department": "finance",
"env": "testing"
},
"logsPolicy": {
"destination": "CLOUD_LOGGING"
}
}
Run the following command:
gcloud batch jobs submit example-container-job \
--location us-central1 \
--config hello-world-container.json
To create a basic container job using the Batch API, use the jobs.create
method. For more information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs
REST resource.
POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
CONTAINER
}
}
],
"computeResource": {
"cpuMilli": CORES,
"memoryMib": MEMORY
},
"maxRetryCount": MAX_RETRY_COUNT,
"maxRunDuration": "MAX_RUN_DURATION"
},
"taskCount": TASK_COUNT,
"parallelism": PARALLELISM
}
]
}
Replace the following:
PROJECT_ID
: the project ID of your project.LOCATION
: the location of the job.JOB_NAME
: the name of the job.CONTAINER
: the container that each task runs. At minimum, a container must specify an image in the imageUri
subfield, but additional subfields might also be required. For more information, see the container
subfields and the example container job in this section.CORES
: Optional. The amount of cores—specifically vCPUs, which usually represent half a physical core—to allocate for each task in milliCPU units. If the cpuMilli
field is not specified, the value is set to 2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If the memoryMib
field is not specified, the value is set to 2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between 0
and 10
. If the maxRetryCount
field is not specified, the value is set to 0
, which means to not retry the task. For more information about the maxRetryCount
field, see Automate task retries.MAX_RUN_DURATION
: Optional. The maximum time a task is allowed to run before being retried or failing, formatted as a value in seconds followed by s
—for example, 3600s
for 1 hour. If the maxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about the maxRunDuration
field, see Limit run times for tasks and runnables using timeouts.TASK_COUNT
: Optional. The number of tasks for the job, which must be a whole number between 1
and the tasks per task group limit. If the taskCount
field is not specified, the value is set to 1
.PARALLELISM
: Optional. The number of tasks the job runs concurrently. The number cannot be larger than the number of tasks and must be a whole number between 1
and the parallel tasks per job limit. If the parallelism
field is not specified, the value is set to 1
.For example, to create a job that runs tasks using the busybox
Docker container image, use the following request:
POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/jobs?job_id=example-container-job
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
"imageUri": "gcr.io/google-containers/busybox",
"entrypoint": "/bin/sh",
"commands": [
"-c",
"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
]
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
},
"maxRetryCount": 2,
"maxRunDuration": "3600s"
},
"taskCount": 4,
"parallelism": 2
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
},
"labels": {
"department": "finance",
"env": "testing"
},
"logsPolicy": {
"destination": "CLOUD_LOGGING"
}
}
where PROJECT_ID
is the project ID of your project.
For more information, see the Batch Go API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java JavaFor more information, see the Batch Java API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js Node.jsFor more information, see the Batch Node.js API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python PythonFor more information, see the Batch Python API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C++ C++For more information, see the Batch C++ API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Create a basic script jobYou can create a basic script job using the Google Cloud console, gcloud CLI, Batch API, Go, Java, Node.js, Python, or C++.
ConsoleTo create a basic script job using the Google Cloud console, do the following:
In the Google Cloud console, go to the Job list page.
Click add_box Create. The Create batch job page opens. In the left pane, the Job details page is selected.
Configure the Job details page:
Optional: In the Job name field, customize the job name.
For example, enter example-basic-job
.
Configure the Task details section:
In the New runnable window, add at least one script or container for this job to run.
For example, to add one script, do the following:
Select Script. A field appears.
In the field, enter a script that you want to run for each task in this job.
For example, enter the following script:
echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.
Click Done.
In the Task count field, enter the number of tasks for this job. The value must be a whole number between 1
and the tasks per task group limit.
For example, enter 4
.
In the Parallelism field, enter the number of tasks to run concurrently. The number cannot be larger than the total number of tasks and must be a whole number between 1
and the parallel tasks per job limit.
For example, enter 2
.
Configure the Resource specifications page:
In the left pane, click Resource specifications. The Resource specifications page opens.
In the VM provisioning model section, select one of the following options for the provisioning model for this job's VMs:
If your job can withstand preemption and you want discounted VMs, select Spot.
Otherwise, select Standard.
For example, select Standard (default).
Select the location for this job:
In the Region field, select a region.
For example, select us-central1 (Iowa)
(default).
In the Zone field, do one of the following:
If you want to restrict this job to run in a specific zone only, select a zone.
Otherwise, select any.
For example, select any (default).
Select one of the following machine families:
For common workloads, click General purpose.
For performance-intensive workloads, click Compute optimized.
For memory-intensive workloads, click Memory optimized.
For accelerator-optimized workloads, click GPUs. For more information, see Create and run a job that uses GPUs.
For example, click General purpose (default).
In the Series field, select a machine series for this job's VMs.
For example, if you selected General purpose for the machine family, select E2 (default).
In the Machine type field, select a machine type for this job's VMs.
For example, if you selected E2 for the machine series, select e2-medium (2 vCPU, 4 GB memory) (default).
Configure the amount of VM resources required for each task:
In the Cores field, enter the amount of vCPUs per task.
For example, enter 1
(default).
In the Memory field, enter the amount of RAM in GB per task.
For example, enter 0.5
(default).
Optional: To review the job configuration, in the left pane, click Preview.
Click Create.
The Job details page displays the job that you created.
To create a basic script job using the gcloud CLI, do the following:
Create a JSON file that specifies your job's configuration details. For example, to create a basic script job, create a JSON file with the following contents. For more information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs
REST resource.
gcloud batch jobs submit
command.
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
SCRIPT
}
}
],
"computeResource": {
"cpuMilli": CORES,
"memoryMib": MEMORY
},
"maxRetryCount": MAX_RETRY_COUNT,
"maxRunDuration": "MAX_RUN_DURATION"
},
"taskCount": TASK_COUNT,
"parallelism": PARALLELISM
}
]
}
Replace the following:
SCRIPT
: the script that each task runs. A script must be defined either as text using the text
subfield or as the path to an accessible file using the path
subfield. For more information, see the script
subfields and the example script job in this section.CORES
: Optional. The amount of cores—specifically vCPUs, which usually represent half a physical core—to allocate for each task in milliCPU units. If the cpuMilli
field is not specified, the value is set to 2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If the memoryMib
field is not specified, the value is set to 2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between 0
and 10
. If the maxRetryCount
field is not specified, the value is set to 0
, which means to not retry the task. For more information about the maxRetryCount
field, see Automate task retries.MAX_RUN_DURATION
: Optional. The maximum time a task is allowed to run before being retried or failing, formatted as a value in seconds followed by s
—for example, 3600s
for 1 hour. If the maxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about the maxRunDuration
field, see Limit run times for tasks and runnables using timeouts.TASK_COUNT
: Optional. The number of tasks for the job. The value must be a whole number between 1
and the tasks per task group limit. If the taskCount
field is not specified, the value is set to 1
.PARALLELISM
: Optional. The number of tasks the job runs concurrently. The number cannot be larger than the number of tasks and must be a whole number between 1
and the parallel tasks per job limit. If the parallelism
field is not specified, the value is set to 1
.Create a job by using the gcloud batch jobs submit
command.
gcloud batch jobs submit JOB_NAME \
--location LOCATION \
--config JSON_CONFIGURATION_FILE
Replace the following:
JOB_NAME
: the name of the job.LOCATION
: the location of the job.JSON_CONFIGURATION_FILE
: the path for a JSON file with the job's configuration details.For example, to create a job that runs tasks using a script:
Create a JSON file in the current directory named hello-world-script.json
with the following contents:
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
"text": "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
},
"maxRetryCount": 2,
"maxRunDuration": "3600s"
},
"taskCount": 4,
"parallelism": 2
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
},
"labels": {
"department": "finance",
"env": "testing"
},
"logsPolicy": {
"destination": "CLOUD_LOGGING"
}
}
Run the following command:
gcloud batch jobs submit example-script-job \
--location us-central1 \
--config hello-world-script.json
To create a basic script job using the Batch API, use the jobs.create
method. For more information about all the fields you can specify for a job, see the reference documentation for the projects.locations.jobs
REST resource.
POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
SCRIPT
}
}
],
"computeResource": {
"cpuMilli": CORES,
"memoryMib": MEMORY
},
"maxRetryCount": MAX_RETRY_COUNT,
"maxRunDuration": "MAX_RUN_DURATION"
},
"taskCount": TASK_COUNT,
"parallelism": PARALLELISM
}
]
}
Replace the following:
PROJECT_ID
: the project ID of your project.LOCATION
: the location of the job.JOB_NAME
: the name of the job.SCRIPT
: the script that each task runs. A script must be defined either as text using the text
subfield or as the path to an accessible file using the path
subfield. For more information, see the script
subfields and the example script job in this section.CORES
: Optional. The amount of cores—specifically vCPUs, which usually represent half a physical core—to allocate for each task in milliCPU units. If the cpuMilli
field is not specified, the value is set to 2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If the memoryMib
field is not specified, the value is set to 2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between 0
and 10
. If the maxRetryCount
field is not specified, the value is set to 0
, which means to not retry the task. For more information about the maxRetryCount
field, see Automate task retries.MAX_RUN_DURATION
: Optional. The maximum time a task is allowed to run before being retried or failing, formatted as a value in seconds followed by s
—for example, 3600s
for 1 hour. If the maxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about the maxRunDuration
field, see Limit run times for tasks and runnables using timeouts.TASK_COUNT
: Optional. The number of tasks for the job. The value must be a whole number between 1
and the tasks per task group limit. If the taskCount
field is not specified, the value is set to 1
.PARALLELISM
: Optional. The number of tasks the job runs concurrently. The number cannot be larger than the number of tasks and must be a whole number between 1
and the parallel tasks per job limit. If the parallelism
field is not specified, the value is set to 1
.For example, to create a job that runs tasks using a script, use the following request:
POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/jobs?job_id=example-script-job
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
"text": "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
},
"maxRetryCount": 2,
"maxRunDuration": "3600s"
},
"taskCount": 4,
"parallelism": 2
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
},
"labels": {
"department": "finance",
"env": "testing"
},
"logsPolicy": {
"destination": "CLOUD_LOGGING"
}
}
where PROJECT_ID
is the project ID of your project.
For more information, see the Batch Go API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java JavaFor more information, see the Batch Java API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js Node.jsFor more information, see the Batch Node.js API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python PythonFor more information, see the Batch Python API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C++ C++For more information, see the Batch C++ API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Use environment variablesUse environment variables when you write a container image or script that you want a job to run. You can use any of the environment variables that are predefined for all Batch jobs and any custom environment variables that you define while creating the job.
Use predefined environment variablesBy default, the runnables in your job can use the following predefined environment variables:
BATCH_TASK_COUNT
: the total number of tasks in this task group.BATCH_TASK_INDEX
: the index number of this task in the task group. The index of the first task is 0
and is incremented for each additional task.BATCH_HOSTS_FILE
: the path to a file listing all the running VM instances in this task group. To use this environment variable, the requireHostsFile
field must be set to true
.BATCH_TASK_RETRY_ATTEMPT
: the number of times that this task has already been attempted. The value is 0
during the first attempt of a task and is incremented for each following retry. The total number of retries allowed for a task is determined by the value of the maxRetryCount
field, which is 0
if undefined. For more information about retries, see Automate task retries.For an example of how to use predefined environment variables, see the previous example runnables in Create a basic job in this document.
Define and use custom environment variablesOptionally, you can define one or more custom environment variables in a job.
You define each variable in a specific environment based on the desired scope of its data:
For a variable that has the same value for all tasks, use one of the following:
If the variable has the same value for all runnables, use the environment of all runnables (environment
subfield of taskSpec
).
Otherwise, if the variable has a separate value for all runnables, use one or more environments of specific runnables (environment
subfield of runnables[]
).
Otherwise, for an array variable that has a separate value for each task, use the environment of all tasks (taskEnvironment
).
In the selected environment, you define the name and value(s) of each variable by using one of the following environment subfields:
To define the variable directly in the job configuration JSON file, use the standard variables (variables
) subfield, as shown in this section. This option is recommended for data that you don't want to encrypt.
To define the variable using encrypted data, you can use Secret Manager or Cloud Key Management Service:
To use the encrypted contents of an existing Secret Manager secret, use the secret variables (secretVariables
) subfield. For more information about using secrets in a job, see Protect sensitive data using Secret Manager.
To use the encrypted contents of an existing Cloud Key Management Service key, use the encrypted variables (encryptedVariables
) subfield. For more information about Cloud KMS keys, see the documentation for Cloud Key Management Service.
You can define and use custom environment variables for your job using the gcloud CLI or Batch API. The following examples explain how to create two jobs that define and use standard variables. The first example job has a variable for a specific runnable. The second example job has an array variable, which has a different value for each task.
gcloudIf you want to define a job that passes an environment variable to a runnable that each task runs, see the example for how to Define and use an environment variable for a runnable. Otherwise, if you want to define a job that passes a list of environment variables to different tasks based on the task index, see the example for how to Define and use an environment variable for each task.
Define and use an environment variable for a runnableTo create a job that passes environment variables to a runnable using the gcloud CLI, use the gcloud batch jobs submit
command and specify the environment variables in the job's configuration file.
For example, to create a script job that defines an environment variable and passes it to the scripts of 3 tasks, make the following request:
Create a JSON file in the current directory named hello-world-environment-variables.json
with the following contents:
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
"text": "echo Hello ${VARIABLE_NAME}! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
},
"environment": {
"variables": {
"VARIABLE_NAME": "VARIABLE_VALUE"
}
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
}
},
"taskCount": 3,
"parallelism": 1
}
],
"allocationPolicy": {
"instances": [
{
"policy": {
"machineType": "e2-standard-4"
}
}
]
}
}
Replace the following:
VARIABLE_NAME
: the name of the environment variable passed to each task. By convention, environment variable names are capitalized.VARIABLE_VALUE
: Optional. The value of the environment variable passed to each task.Run the following command:
gcloud batch jobs submit example-environment-variables-job \
--location us-central1 \
--config hello-world-environment-variables.json
To create a job that passes environment variables to a task based on task index using the gcloud CLI, use the gcloud batch jobs submit
command and specify the taskEnvironments
array field in the job's configuration file.
For example, to create a job that includes an array of 3 environment variables with matching names and different values, and passes the environment variables to the scripts of the tasks which indices match the environment variables' indices in the array:
Create a JSON file in the current directory named hello-world-task-environment-variables.json
with the following contents:
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
"text": "echo Hello ${TASK_VARIABLE_NAME}! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
},
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
}
},
"taskCount": 3,
"taskEnvironments": [
{
"variables": {
"TASK_VARIABLE_NAME": "TASK_VARIABLE_VALUE_0"
}
},
{
"variables": {
"TASK_VARIABLE_NAME": "TASK_VARIABLE_VALUE_1"
}
},
{
"variables": {
"TASK_VARIABLE_NAME": "TASK_VARIABLE_VALUE_2"
}
}
]
}
],
"allocationPolicy": {
"instances": [
{
"policy": {
"machineType": "e2-standard-4"
}
}
]
}
}
Replace the following:
TASK_VARIABLE_NAME
: the name of the task environment variables passed to the tasks with matching indices. By convention, environment variable names are capitalized.TASK_VARIABLE_VALUE_0
: the value of the environment variable passed to the first task, for which BATCH_TASK_INDEX
is equal to 0
.TASK_VARIABLE_VALUE_1
: the value of the environment variable passed to the second task, for which BATCH_TASK_INDEX
is equal to 1
.TASK_VARIABLE_VALUE_2
: the value of the environment variable passed to the third task, for which BATCH_TASK_INDEX
is equal to 2
.taskEnvironments
array field, the value of the taskCount
field must then equal the length of the taskEnvironments
array. The maximum length of the taskEnvironments
array is 200.Run the following command:
gcloud batch jobs submit example-task-environment-variables-job \
--location us-central1 \
--config hello-world-task-environment-variables.json
If you want to define a job that passes an environment variable to a runnable that each task runs, see the example for how to Define and use an environment variable for a runnable. Otherwise, if you want to define a job that passes a list of environment variables to different tasks based on the task index, see the example for how to Define and use an environment variable for each task.
Define and use an environment variable for a runnableTo create a job that passes environment variables to a runnable using Batch API, use the gcloud batch jobs submit
command and specify the environment variables in the environment
field.
For example, to create a job that includes an environment variable and passes it to the scripts of 3 tasks, make the following request:
POST https://batch.googleapis.com/v1/projects/<var>PROJECT_ID</var>/locations/us-central1/jobs?job_id=example-environment-variables-job
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
"text": "echo Hello ${VARIABLE_NAME}! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
},
"environment": {
"variables": {
"VARIABLE_NAME": "VARIABLE_VALUE"
}
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
}
},
"taskCount": 3,
"parallelism": 1
}
],
"allocationPolicy": {
"instances": [
{
"policy": {
"machineType": "e2-standard-4"
}
}
]
}
}
Replace the following:
PROJECT_ID
: the project ID of your project.VARIABLE_NAME
: the name of the environment variable passed to each task. By convention, environment variable names are capitalized.VARIABLE_VALUE
: the value of the environment variable passed to each task.To create a job that passes environment variables to a task based on task index using Batch API, use the jobs.create
method and specify the environment variables in the taskEnvironments
array field.
For example, to create a job that includes an array of 3 environment variables with matching names and different values, and passes the environment variables to the scripts of 3 tasks based on their indices, make the following request:
POST https://batch.googleapis.com/v1/projects/<var>PROJECT_ID</var>/locations/us-central1/jobs?job_id=example-task-environment-variables-job
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"script": {
"text": "echo Hello ${TASK_VARIABLE_NAME}! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
},
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
}
},
"taskCount": 3,
"taskEnvironments": [
{
"variables": {
"TASK_VARIABLE_NAME": "TASK_VARIABLE_VALUE_0"
}
},
{
"variables": {
"TASK_VARIABLE_NAME": "TASK_VARIABLE_VALUE_1"
}
},
{
"variables": {
"TASK_VARIABLE_NAME": "TASK_VARIABLE_VALUE_2"
}
}
]
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
}
}
Replace the following:
PROJECT_ID
: the project ID of your project.TASK_VARIABLE_NAME
: the name of the environment variables passed to the tasks with matching indices. By convention, environment variable names are capitalized.TASK_VARIABLE_VALUE_0
: the value of the environment variable passed to the first task, for which BATCH_TASK_INDEX
is equal to 0
.TASK_VARIABLE_VALUE_1
: the value of the environment variable passed to the second task, for which BATCH_TASK_INDEX
is equal to 1
.TASK_VARIABLE_VALUE_2
: the value of the environment variable passed to the third task, for which BATCH_TASK_INDEX
is equal to 2
.taskEnvironments
array field, the taskCount
field's value must then equal the length of the taskEnvironments
array. The maximum length of the taskEnvironments
array is 200. What's next
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["Batch jobs are composed of task groups, each containing tasks that execute either scripts or container images."],["You can create batch jobs via the console, `gcloud` CLI, or API, with options to configure details like task count, parallelism, VM provisioning, and resource specifications."],["Predefined environment variables such as `BATCH_TASK_COUNT`, `BATCH_TASK_INDEX`, and `BATCH_TASK_RETRY_ATTEMPT` are automatically available to tasks, with `BATCH_HOSTS_FILE` being optional."],["Custom variables can be set for all tasks or specific runnables, or even have different values for each task, using the `variables`, `secretVariables`, or `encryptedVariables` subfields in the job configuration."],["Using gcloud or the API you can define either runnable variables or task-specific variables."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4