简体   繁体   English

app.yaml env_variables 在云构建步骤中作为 envs 访问

[英]app.yaml env_variables accessible as envs during cloud build steps

as described in title, some frameworks like Next.js or Nuxt.js require some env vars defined in app.yaml (only accessible at runtime) to be accessible during build step, mainly npm run build如标题中所述,一些框架如 Next.js 或 Nuxt.js 需要在app.yaml中定义的一些环境变量(只能在运行时访问)才能在构建步骤期间访问,主要是npm run build

the workaround I'm using at the moment is to define the same env vars in 2 different places app.yaml and cloud build trigger env vars: It is not ideal at all我目前使用的解决方法是在 2 个不同的地方定义相同的环境变量app.yaml和云构建触发器环境变量:它根本不理想

Agree, to maintain the same value in 2 places is the best way to lost the consistency and to create bugs.同意,在 2 个地方保持相同的值是失去一致性和产生错误的最好方法。 Thus, the best solution is to define only once these variables.因此,最好的解决方案是只定义一次这些变量。

Put them where you want:把它们放在你想要的地方:

  • In a file (.env file for example)在文件中(例如 .env 文件)
  • In your trigger configuration在您的触发器配置中
  • In the app.yaml file在 app.yaml 文件中

But only once.但只有一次。 Then create a step in your Cloud Build job that parse the configuration variable file and extract the values to put them at the correct location before continuing the build process.然后在您的 Cloud Build 作业中创建一个步骤来解析配置变量文件并提取值以将它们放在正确的位置,然后再继续构建过程。

grep , cut and sed works well for this. grepcutsed效果很好。 You can provide file examples in you need to have code example for this substitution.您可以提供文件示例,因为您需要具有此替换的代码示例。


Edit 1编辑 1

According to your comment, there is few things to know.根据您的评论,几乎没有什么可知道的。 Cloud Build is great.. But Cloud Build is also boring... Cloud Build 很棒……但是 Cloud Build 也很无聊……

The env var management is a perfect example. env var 管理就是一个很好的例子。 The short answer is: it's not possible to reuse an env var define in the step N in the step N+x.简短的回答是:不可能在步骤 N+x 中重用步骤 N 中定义的环境变量。

To solve this, you need to do ugly things, like this要解决这个问题,你需要做一些丑陋的事情,比如这样

steps:
  - name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args:
      - -c
      - |
        MY_ENV_VAR="cool"
        echo $${MY_ENV_VAR} > env-var-save.file
  - name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args:
      - -c
      - |
        MY_ENV_VAR=$$(cat env-var-save.file)
        echo $${MY_ENV_VAR}

Because only the /workspace directory is common between each step, you have to save the env vars in file and to read them afterwards in the step that you want.因为只有/workspace目录在每个步骤之间是通用的,所以您必须将环境变量保存在文件中,然后在您想要的步骤中读取它们。

About the app.yaml file, you can do something like this关于app.yaml文件,你可以这样做

app.yaml file content example app.yaml文件内容示例

runtime: go114
service: go-serverless-oracle

env_variables:
  ORACLE_IP: "##ORACLE_IP##"

Example of Cloud Build step, get the value from substituions variable in Cloud Build. Cloud Build 步骤示例,从 Cloud Build 中的 substituions 变量获取值。

  - name: 'gcr.io/cloud-builders/gcloud'
    entrypoint: 'bash'
    args:
      - -c
      - |
        sed -i -e 's/##ORACLE_IP##/$_ORACLE_IP/g' app.yaml
        cat app.yaml
substitutions:
  _ORACLE_IP: 127.0.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM