在 Cloud Run 中构建并创建 Shell 作业
准备工作
- 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.
- 
    
    
      In the Google Cloud console, on the project selector page, select or create 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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
    
    
      In the Google Cloud console, on the project selector page, select or create 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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    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.启用 Cloud Run Admin API 后,系统会自动创建 Compute Engine 默认服务账号。 
- 为 Cloud Build 服务账号授予以下 IAM 角色。
    
点击可查看 Cloud Build 服务账号需要的角色除非您替换此行为,否则 Cloud Build 会自动使用 Compute Engine 默认服务账号作为默认 Cloud Build 服务账号来构建源代码和 Cloud Run 资源。为了让 Cloud Build 能够构建来源,请让管理员向项目的 Compute Engine 默认服务账号授予 Cloud Run Builder ( roles/run.builder):gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder 将 PROJECT_NUMBER替换为您的 Google Cloud项目编号,将PROJECT_ID替换为您的 Google Cloud项目 ID。如需详细了解如何查找项目 ID 和项目编号,请参阅创建和管理项目。向 Compute Engine 默认服务账号授予 Cloud Run Builder 角色需要几分钟时间才能传播。 
- 查看 Cloud Run 价格或使用价格计算器估算费用。
编写示例作业
如需编写运行 Shell 脚本的 Cloud Run 作业,请执行以下操作:
- 创建名为 - jobs的新目录,并转到此目录中:- mkdir jobs cd jobs
- 使用以下内容创建 Dockerfile 文件: 
- 在同一目录中,为实际作业代码创建一个 - script.sh文件。将以下示例行复制到其中:- Cloud Run 作业允许用户指定作业要执行的任务数量。此示例代码演示了如何使用内置 - CLOUD_RUN_TASK_INDEX环境变量。每个任务代表容器的一个正在运行的副本。 请注意,任务通常并行执行。如果每个任务都可以独立处理一部分数据,则使用多个任务非常有用。- 每个任务都知道其存储在 - CLOUD_RUN_TASK_INDEX环境变量中的索引。内置- CLOUD_RUN_TASK_COUNT环境变量包含在执行作业时通过- --tasks参数提供的任务数量。- 此处所示的代码还展示了如何使用内置 - CLOUD_RUN_TASK_ATTEMPT环境变量重试任务,该变量包含此任务重试的次数,从 0 开始(表示第一次尝试),然后每次连续重试时递增 1,上限为- --max-retries。- 此外,通过代码,您还可以生成失败来测试重试并生成错误日志,从而查看失败情况。 
您的代码已完成,可以封装在容器中。
构建作业容器,将其发送到 Artifact Registry 并部署到 Cloud Run
重要提示:本快速入门假定您在快速入门中使用的项目中拥有所有者或编辑者角色。否则,请参阅 Cloud Run Source Developer 角色,了解从源代码部署 Cloud Run 资源所需的权限。
本快速入门使用“从源代码部署”,以构建容器,将其上传到 Artifact Registry,然后将作业部署到 Cloud Run:
gcloud run jobs deploy job-quickstart \ --source . \ --tasks 50 \ --set-env-vars SLEEP_MS=10000 \ --set-env-vars FAIL_RATE=0.1 \ --max-retries 5 \ --region REGION \ --project=PROJECT_ID
其中,PROJECT_ID 是您的项目 ID,REGION 是您的区域,例如 europe-west1。请注意,您可以将各种参数更改为要用于测试的任何值。SLEEP_MS 模拟工作,FAIL_RATE 导致 X% 的任务失败,因此您可以试验并行情况并重试失败任务。
在 Cloud Run 中执行作业
如需执行刚刚创建的作业,请运行以下命令:
gcloud run jobs execute job-quickstart --region REGION
将 REGION 替换为您在创建和部署作业时使用的区域,例如 europe-west1。
清理
为避免您的 Google Cloud 账号产生额外费用,请删除您在本快速入门中部署的所有资源。
删除代码库
Cloud Run 仅针对作业的执行时间收费。不过,您可能仍需支付将容器映像存储在 Artifact Registry 中而产生的相关费用。如需删除 Artifact Registry 制品库,请按照 Artifact Registry 文档中的删除制品库中的步骤操作。
删除作业
Cloud Run 作业仅在作业任务执行时产生费用。 如需删除 Cloud Run 作业,请按以下步骤之一操作:
控制台
要删除任务,请执行以下操作:
- 在 Google Cloud 控制台中,前往 Cloud Run: 
- 在作业列表中找到要删除的作业,然后点击该作业对应的复选框以将其选中。 
- 点击删除。这会终止所有正在进行的作业执行和所有正在运行的容器实例。 
gcloud
如需删除作业,请运行以下命令:
gcloud run jobs delete JOB_NAME
将 JOB_NAME 替换为作业的名称。
删除测试项目
删除 Google Cloud 项目后,系统即会停止对该项目中的所有资源计费。如需释放项目中的所有 Google Cloud 资源,请按以下步骤操作:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
后续步骤
如需详细了解如何使用代码源构建容器并推送到仓库,请参阅: