通过shell实现yaml目标变量替换

yaml的模板build_template.yaml如下

1
2
3
repos:
- url: "${REPO_URL}"
branch: "${BRANCH}"

REPO_URLBRANCH定义在环境变量中或写入config.ini文件

1
2
3
# cat config.ini
REPO_URL="https://github.com/etcd-io/etcd.git"
BRANCH="master'

替换脚本如下

1
2
3
configContent=$(cat config.ini)
yamlTemplate=$(cat build_template.yaml)
printf "$configContent\ncat << EOF\n${yamlTemplate}\nEOF" |bash > ./build.yaml

生成的build.yaml文件内容如下:

1
2
3
repos:
- url: "https://github.com/etcd-io/etcd.git"
branch: "master"