【问题标题】:Import a config file in Google Cloud Build yaml config to assign values在 Google Cloud Build yaml 配置中导入配置文件以分配值
【发布时间】:2020-09-17 10:44:28
【问题描述】:
之前,我有一个包含 docker build 和 docker run 命令的 makefile。我还有一个由 makefile 导入的配置文件,它有一些变量值。然后,我使用 build-arg 标志将值分配给 dockerfile 和我的代码中使用的变量。
现在我想切换到 Google Cloud Build 并使用 yaml 配置。我知道 yaml 配置文件的结构,我仍然可以使用 build-arg 标志。但是,我仍然可以像以前一样导入我的配置文件并分配变量值吗?
【问题讨论】:
标签:
makefile
dockerfile
config
google-cloud-build
【解决方案1】:
您分享了一些有关您当前流程的信息,但我会尽力为您提供一些提示
首先,您可以导入文件。我不知道它在哪里,但如果它在 Google 云存储上,你可以这样做
step:
- name: gcr.io/cloud-builders/gcloud:latest
entrypoint: "gsutil"
args: ["cp","gs://yourbuket/config.file", "/workspace/config.file"]
然后,这就是 Cloud Build 的无聊之处,您无法在代码中(我的意思是动态地)和所有步骤中定义全局环境变量。你必须一步一步做,比如这样
step:
....
- name: gcr.io/cloud-builders/gcloud:latest
entrypoint: "bash"
args:
- "-c"
- |
# Add it in the environment
export MY_VAR=$(grep MY_VAR /workspace/config.file | cut -d'=' -f2)
这里我假设配置文件格式是key=value
如果您需要在 Docker Build 中添加它,您可以使用 -e 参数执行相同的操作。对于你的代码,我不知道你是怎么通过的。