【问题标题】:Cloudbuild template and env fileCloudbuild 模板和环境文件
【发布时间】:2020-03-19 18:24:18
【问题描述】:

我在 Symfony 上,但这不是很重要。我有一个 .env 文件,我想在 cloudbuild.yaml 中使用他的变量。没有办法避免重复???? ? 此外,我阅读了这个article,我看到作者使用了Yaml 合并功能with gitlab hidden key,它在文件很大时非常有用。我尝试使用它,但云构建不喜欢,似乎不可能像在 gitlab-ci.yaml 中那样使用自定义密钥。有什么想法吗?

  • 更新

在构建中,我们需要有环境变量和通用配置文件,以避免手动更改大量值。所以我想在 cloudbuild.yaml 中使用隐藏键,因为我需要使用 Yaml 合并功能来避免代码重复。 这是我没有优化的 cloudbuild.yaml 示例:

steps:
  - name: gcr.io/cloud-builders/docker
    args: ['build', '-t', 'gcr.io/$PROJECT_ID/image-pgsql', '-f', 'docker/postgresql/Dockerfile', '.']
  - name: gcr.io/cloud-builders/docker
    args: ['build', '-t', 'gcr.io/$PROJECT_ID/image-nginx', '--build-arg', 'VERSION=1.15.3', '-f', 'docker/nginx/Dockerfile', '.']

但我想要这个,或者类似的东西:

.build-template: &buildTemplate
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/${IMAGE_NAME}', '--build-arg', 'VERSION=${VERSION}', '-f', '${DOCKER_PATH}', '.']

steps:
  - name: 'gcr.io/cloud-builders/docker'
    <<: *buildTemplate
    env: ['IMAGE_NAME=pgsql', 'VERSION=12', 'DOCKER_PATH=docker/postgresql/Dockerfile']
  - name: 'gcr.io/cloud-builders/docker'
    <<: *buildTemplate
    env: ['IMAGE_NAME=nginx', 'VERSION=1.15.3', 'DOCKER_PATH=docker/nginx/Dockerfile']

当我尝试运行 cloud-build-local --dryrun=false . =>

Error loading config file: unknown field ".build-template" in cloudbuild.Build

【问题讨论】:

  • 下面的文章,它给你什么错误?您想在 Cloud Build 中实现隐藏键吗?您能否详细解释一下您要做什么以及您面临哪些错误?你能解释一下重复是什么意思吗?
  • @NibrasH 发布更新抱歉

标签: symfony yaml gitlab-ci google-cloud-build


【解决方案1】:

很遗憾,Google Cloud Build 在 Cloud build 中没有隐藏键的这一功能。我已代表您在Public Issue Tracker 创建了一个功能请求,您可以在其中跟踪与 Cloud Build 中隐藏键的功能请求相关的所有更新。

【讨论】:

    【解决方案2】:

    您必须遵循 cloudbuild.yaml 架构,该架构记录在 here。由于构建将直接从该 yaml 文件触发,因此无法添加其他字段并进行某种预处理以将不同的文件合并在一起。

    我们所说的唯一选项:

    1. 使用全局环境变量:

      options:
        env: [string, string, ...]
      steps: [...]
      
    2. 使用特定步骤的环境变量:

      steps:
        - name: string
          env: [string, string, ...]
      
    3. 使用替换(使用allow-loose):

       substitutions:
           _SUB_VALUE: world
       options:
           substitution_option: 'ALLOW_LOOSE'
       steps:
         - name: 'ubuntu'
           args: ['echo', 'hello ${_SUB_VALUE}']
      
    4. 在构建步骤中获取您的 [environment].env 文件:

       steps:
         - name: 'gcr.io/cloud-builders/gcloud'
           entrypoint: 'bash'
           args:
             - '-c'
             - |
               source ${BRANCH_NAME}.env
               echo $${_MY_VARIABLE_1}
               echo $${_MY_VARIABLE_2}
               ...
      

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2014-08-31
      • 2020-05-09
      相关资源
      最近更新 更多