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 begin
- If you haven't used Batch before, review Get started with Batch and enable Batch by completing the prerequisites for projects and users.
-
To get the permissions that you need to create a job, ask your administrator to grant you the following IAM roles:
-
Batch Job Editor (
roles/batch.jobsEditor
) on the project -
Service Account User (
roles/iam.serviceAccountUser
) on the job's service account, which by default is the default Compute Engine service account
For 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.
-
Batch Job Editor (
-
Each time you create a job, make sure the job has a valid network configuration.
- If you don't have any specific networking requirements for your workload or project and you haven't modified the default network for your project, no action is required.
- Otherwise, you need to configure networking when you create a job. Learn how to configure networking for a job before you create a basic job, so you can modify the examples below to meet your networking requirements.
-
Each time you create a job, make sure the job has a valid VM
operating system (OS) environment.
- If you don't have any specific VM OS image or boot disk requirements for your workload or project, no action is required.
- Otherwise, you need to prepare a valid VM OS environment option. Before creating a basic job, either allow the default configuration for the VM OS environment or learn how to customize the VM OS environment so you can modify the examples below to meet your requirements.
Create a basic job
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:
- If you want to use Batch to write jobs that run a container image, see create a container job.
- Otherwise, if you aren't sure if you want to use container images or if you are unfamiliar with containers, creating a script job is recommended.
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 job
You 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++.
Console
To 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
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 text box appears.
In the text box, 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 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.
gcloud
To 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.{ "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 theimageUri
subfield, but additional subfields might also be required. For more information, see thecontainer
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 thecpuMilli
field is not specified, the value is set to2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If thememoryMib
field is not specified, the value is set to2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between0
and10
. If themaxRetryCount
field is not specified, the value is set to0
, which means to not retry the task. For more information about themaxRetryCount
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 bys
—for example,3600s
for 1 hour. If themaxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about themaxRunDuration
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 between1
and the tasks per task group limit. If thetaskCount
field is not specified, the value is set to1
.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 between1
and the parallel tasks per job limit. If theparallelism
field is not specified, the value is set to1
.
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
API
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 theimageUri
subfield, but additional subfields might also be required. For more information, see thecontainer
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 thecpuMilli
field is not specified, the value is set to2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If thememoryMib
field is not specified, the value is set to2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between0
and10
. If themaxRetryCount
field is not specified, the value is set to0
, which means to not retry the task. For more information about themaxRetryCount
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 bys
—for example,3600s
for 1 hour. If themaxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about themaxRunDuration
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 between1
and the tasks per task group limit. If thetaskCount
field is not specified, the value is set to1
.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 between1
and the parallel tasks per job limit. If theparallelism
field is not specified, the value is set to1
.
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.
Go
Go
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
Java
For 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.js
For 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
Python
For 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 job
You can create a basic script job using the Google Cloud console, gcloud CLI, Batch API, Go, Java, Node.js, Python, or C++.
Console
To 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
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 text box appears.
In the text box, 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 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.
gcloud
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.{ "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 thetext
subfield or as the path to an accessible file using thefile
subfield. For more information, see thescript
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 thecpuMilli
field is not specified, the value is set to2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If thememoryMib
field is not specified, the value is set to2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between0
and10
. If themaxRetryCount
field is not specified, the value is set to0
, which means to not retry the task. For more information about themaxRetryCount
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 bys
—for example,3600s
for 1 hour. If themaxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about themaxRunDuration
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 between1
and the tasks per task group limit. If thetaskCount
field is not specified, the value is set to1
.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 between1
and the parallel tasks per job limit. If theparallelism
field is not specified, the value is set to1
.
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
API
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 thetext
subfield or as the path to an accessible file using thefile
subfield. For more information, see thescript
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 thecpuMilli
field is not specified, the value is set to2000
(2 vCPUs).MEMORY
: Optional. The amount of memory to allocate for each task in MB. If thememoryMib
field is not specified, the value is set to2000
(2 GB).MAX_RETRY_COUNT
: Optional. The maximum number of retries for a task. The value must be a whole number between0
and10
. If themaxRetryCount
field is not specified, the value is set to0
, which means to not retry the task. For more information about themaxRetryCount
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 bys
—for example,3600s
for 1 hour. If themaxRunDuration
field is not specified, the value is set to the maximum run time for a job. For more information about themaxRunDuration
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 between1
and the tasks per task group limit. If thetaskCount
field is not specified, the value is set to1
.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 between1
and the parallel tasks per job limit. If theparallelism
field is not specified, the value is set to1
.
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.
Go
Go
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
Java
For 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.js
For 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
Python
For 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 variables
Use 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 variables
By 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 is0
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, therequireHostsFile
field must be set totrue
.BATCH_TASK_RETRY_ATTEMPT
: the number of times that this task has already been attempted. The value is0
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 themaxRetryCount
field, which is0
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 variables
Optionally, 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 oftaskSpec
).Otherwise, if the variable has a separate value for all runnables, use one or more environments of specific runnables (
environment
subfield ofrunnables[]
).
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.
gcloud
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 runnable
To 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
Define and use an environment variable for each task
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 whichBATCH_TASK_INDEX
is equal to0
.TASK_VARIABLE_VALUE_1
: the value of the environment variable passed to the second task, for whichBATCH_TASK_INDEX
is equal to1
.TASK_VARIABLE_VALUE_2
: the value of the environment variable passed to the third task, for whichBATCH_TASK_INDEX
is equal to2
.
Run the following command:
gcloud batch jobs submit example-task-environment-variables-job \ --location us-central1 \ --config hello-world-task-environment-variables.json
API
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 runnable
To 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.
Define and use an environment variable for 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 whichBATCH_TASK_INDEX
is equal to0
.TASK_VARIABLE_VALUE_1
: the value of the environment variable passed to the second task, for whichBATCH_TASK_INDEX
is equal to1
.TASK_VARIABLE_VALUE_2
: the value of the environment variable passed to the third task, for whichBATCH_TASK_INDEX
is equal to2
.
What's next
- If you have issues creating or running a job, see Troubleshooting.
- View jobs and tasks.
- Learn about more job creation options.