Build and create a Python job in Cloud Run

Learn how to create a simple Cloud Run job, then deploy from source,which automatically packages your code into a container image, uploads thecontainer image to Artifact Registry, and then deploys to Cloud Run. You canuse other languages in addition to the ones shown.

Before you begin

  1. 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.
  2. Install the Google Cloud CLI.

    Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.
  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  5. Create or select a Google Cloud 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.create permission.Learn how to grant roles.
    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.
    • Create a Google Cloud project:

      gcloud projects createPROJECT_ID

      ReplacePROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set projectPROJECT_ID

      ReplacePROJECT_ID with your Google Cloud project name.

  6. 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.

  7. Verify that billing is enabled for your Google Cloud project.

  8. Install the Google Cloud CLI.

    Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.
  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  11. Create or select a Google Cloud 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.create permission.Learn how to grant roles.
    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.
    • Create a Google Cloud project:

      gcloud projects createPROJECT_ID

      ReplacePROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set projectPROJECT_ID

      ReplacePROJECT_ID with your Google Cloud project name.

  12. 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.

  13. Verify that billing is enabled for your Google Cloud project.

  14. 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.enable permission.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.

  15. 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:

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:

  1. Create a new directory namedjobs and change directoryinto it:

    mkdirjobscdjobs
  2. Create amain.py file 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 process

    Cloud Run jobs allows users to specify the number of tasks the job isto execute. This sample code shows how to use the built-inCLOUD_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 theCLOUD_RUN_TASK_INDEXenvironment variable. The built-inCLOUD_RUN_TASK_COUNT environment 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-inCLOUD_RUN_TASK_ATTEMPT environment 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.

  3. Create a text file namedProcfile with 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--regionREGION

ReplaceREGION with the region you used when you created and deployedthe job, for exampleeurope-west1.

Success: You created and executed a job using the Python runtime in Cloud Run.

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:

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Locate the job you want to delete in the jobs list, and clickits checkbox to select it.

  3. ClickDelete. This terminates all the job executions in progress andall running container instances.

gcloud

To delete a job, run the following command:

gcloudrunjobsdeleteJOB_NAME

ReplaceJOB_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:

    Caution: Deleting a project has the following effects:
    • Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as anappspot.com URL, delete selected resources inside the project instead of deleting the whole project.

    Delete a Google Cloud project:

    gcloud projects deletePROJECT_ID

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.