Build and create a Python job in Cloud Run
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Install the Google Cloud CLI.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
Create or select a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Create a Google Cloud project:
gcloud projects createPROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating.Select the Google Cloud project that you created:
gcloud config set projectPROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
If you're using an existing project for this guide,verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Install the Google Cloud CLI.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
Create or select a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Create a Google Cloud project:
gcloud projects createPROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating.Select the Google Cloud project that you created:
gcloud config set projectPROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
If you're using an existing project for this guide,verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Run Admin API and Cloud Build APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.gcloudservicesenablerun.googleapis.com
cloudbuild.googleapis.com After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.
- ReviewCloud Run pricing or estimate costswith thepricing calculator.
Required roles
To get the permissions that you need to complete this quickstart, ask your administrator to grant you the following IAM roles:
- Cloud Run Source Developer (
roles/run.sourceDeveloper) on the project - Service Account User (
roles/iam.serviceAccountUser) on the service identity - Logs Viewer (
roles/logging.viewer) on the project
For more information about granting roles, seeManage access to projects, folders, and organizations.
You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
Grant the Cloud Build service account access to your project
Cloud Build automatically uses theCompute Engine defaultservice account as the defaultCloud Build service account to build your source code andCloud Run resource, unless you override this behavior.
For Cloud Build to build your sources, grant the Cloud Build serviceaccount theCloud RunBuilder(roles/run.builder) role on your project:
gcloudprojectsadd-iam-policy-bindingPROJECT_ID\--member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS\--role=roles/run.builder
ReplacePROJECT_ID with your Google Cloudproject ID andSERVICE_ACCOUNT_EMAIL_ADDRESS with theemail address of the Cloud Build service account. If you're using theCompute Engine default service account as the Cloud Build service account, thenuse the following format for the service account email address:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
ReplacePROJECT_NUMBER with your Google Cloudproject number.
For detailed instructions on how to find your project ID, and project number,seeCreatingand managing projects.
Granting the Cloud Run builder role takes a couple of minutes topropagate.
Writing the sample job
To write a job in Python:
Create a new directory named
jobsand change directoryinto it:mkdirjobscdjobsCreate a
main.pyfile for the actual job code. Copy the following sample linesinto it:importjsonimportosimportrandomimportsysimporttime# Retrieve Job-defined env varsTASK_INDEX=os.getenv("CLOUD_RUN_TASK_INDEX",0)TASK_ATTEMPT=os.getenv("CLOUD_RUN_TASK_ATTEMPT",0)# Retrieve User-defined env varsSLEEP_MS=os.getenv("SLEEP_MS",0)FAIL_RATE=os.getenv("FAIL_RATE",0)# Define main scriptdefmain(sleep_ms=0,fail_rate=0):"""Program that simulates work using the sleep method and random failures. Args: sleep_ms: number of milliseconds to sleep fail_rate: rate of simulated errors """print(f"Starting Task #{TASK_INDEX}, Attempt #{TASK_ATTEMPT}...")# Simulate work by waiting for a specific amount of timetime.sleep(float(sleep_ms)/1000)# Convert to seconds# Simulate errorsrandom_failure(float(fail_rate))print(f"Completed Task #{TASK_INDEX}.")defrandom_failure(rate):"""Throws an error based on fail rate Args: rate: a float between 0 and 1 """ifrate <0orrate >1:# Return without retrying the Job Taskprint(f"Invalid FAIL_RATE env var value:{rate}. "+"Must be a float between 0 and 1 inclusive.")returnrandom_failure=random.random()ifrandom_failure <rate:raiseException("Task failed.")# Start scriptif__name__=="__main__":try:main(SLEEP_MS,FAIL_RATE)exceptExceptionaserr:message=(f"Task #{TASK_INDEX}, "+f"Attempt #{TASK_ATTEMPT} failed:{str(err)}")print(json.dumps({"message":message,"severity":"ERROR"}))sys.exit(1)# Retry Job Task by exiting the processCloud Run jobs allows users to specify the number of tasks the job isto execute. This sample code shows how to use the built-in
CLOUD_RUN_TASK_INDEXenvironment variable. Each task represents one running copy of the container.Note that tasks are usually executed in parallel. Using multiple tasks is usefulif each task can independently process a subset of your data.Each task is aware of its index, stored in the
CLOUD_RUN_TASK_INDEXenvironment variable. The built-inCLOUD_RUN_TASK_COUNTenvironment variablecontains the number of tasks supplied at job execution time via the--tasksparameter.The code shown also shows how to retry tasks, using the built-in
CLOUD_RUN_TASK_ATTEMPTenvironment variable, which contains the number oftimes this task has been retried, starting at 0 for the first attempt andincrementing by 1 for every successive retry, up to--max-retries.The code also lets you generate failures as a way to test retriesand to generate error logs so you can see what those look like.
Create a text file named
Procfilewith no file extension, containing thefollowing:web:python3main.py
Your code is complete and ready to be packaged in a container.
Build jobs container, send it to Artifact Registry and deploy to Cloud Run
This quickstart uses deploy from source, which builds the container, uploads itto Artifact Registry, and deploys the job to Cloud Run:
gcloudrunjobsdeployjob-quickstart\--source.\--tasks50\--set-env-varsSLEEP_MS=10000\--set-env-varsFAIL_RATE=0.1\--max-retries5\--regionREGION\--project=PROJECT_ID
wherePROJECT_ID is your project ID andREGION is yourregion, for example,europe-west1. Note that you canchange the various parameters to whatever values you want to use for yourtesting purposes.SLEEP_MS simulates work andFAIL_RATE causesX% of tasksto fail so you can experiment with parallelism and retrying failing tasks.
Execute a job in Cloud Run
To execute the job you just created:
gcloudrunjobsexecutejob-quickstart--regionREGIONReplaceREGION with the region you used when you created and deployedthe job, for exampleeurope-west1.
Clean up
To avoid additional charges to your Google Cloud account, delete all the resourcesyou deployed with this quickstart.
Delete your repository
Cloud Run only charges for the time your job executes.However, you might still becharged for storing the container image inArtifact Registry. To delete Artifact Registry repositories,follow the steps inDeleterepositories in the Artifact Registrydocumentation.
Delete your job
Cloud Run jobs only incur cost when a job task is executing.To delete your Cloud Run job, follow one of these steps:
Console
To delete a job:
In the Google Cloud console, go to Cloud Run:
Locate the job you want to delete in the jobs list, and clickits checkbox to select it.
ClickDelete. This terminates all the job executions in progress andall running container instances.
gcloud
To delete a job, run the following command:
gcloudrunjobsdeleteJOB_NAMEReplaceJOB_NAME with the name of the job.
Delete your test project
Deleting your Google Cloud project stops billing for all resources in thatproject. To release all Google Cloud resources in your project, follow these steps:
What's next
For more information on building a container from code source and pushing toa repository, see:
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.