首页 > 解决方案 > 传递项目 ID 时 cloudbuild.yaml 文件中的正则表达式错误

问题描述

我正在尝试使用云构建运行数据流作业

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  args:
  dataflow
  jobs
  run
  google-template-job
  --gcs-location=gs://dataflow-templates/latest/PubSub_Subscription_to_BigQuery
  --parameters=inputSubscription='projects/$PROJECT_ID/subscriptions/messages'
  --parameters=outputTableSpec="$PROJECT_ID:beam_samples.streaming_beam"
  --staging-location=gs://cloudbuild-dataflow-testproject123456789-313307/tmp'
  --region=us-central1

每次触发构建时,都会出现以下错误

ERROR: (gcloud.dataflow.jobs.run) INVALID_ARGUMENT: The template parameters are invalid.
- '@type': type.googleapis.com/google.dataflow.v1beta3.InvalidTemplateParameters
  parameterViolations:
  - description: 'Unmatched regex: ^projects\/[^\n\r\/]+\/subscriptions\/[^\n\r\/]+$'
    parameter: inputSubscription
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/gcloud" failed: step exited with non-zero status: 1

我的项目 ID 中有一个“-”,所以如果我用项目 ID 的值替换 $PROJECT_ID,我仍然会遇到同样的错误,是否有任何解决方法。我无法控制停止正则表达式检查,因为它是谷歌提供的模板。

我如何克服这个

标签: regexgoogle-cloud-dataflowgoogle-cloud-pubsubgoogle-cloud-buildcloudbuild.yaml

解决方案


知道了。这只是一个命令解释器问题。如果你放单引号,你会阻止对内部字符串的任何评估。

在你的情况下

--parameters=inputSubscription='projects/$PROJECT_ID/subscriptions/messages'

该值'projects/$PROJECT_ID/subscriptions/messages'按原样采用,因此项目 ID 包含大写和下划线,这违反了正则表达式模式。

更改为双引号,它应该很好用!

--parameters=inputSubscription="projects/$PROJECT_ID/subscriptions/messages"

推荐阅读