JSON 位桶管道中的值替换

JSON value substitution in bitbucket pipeline

我有一个 JSON 文件 "appSettings.json",其中包含以下内容:

{
  "Branch": {
    "Name": "test"
  },
}

我的问题是,当 运行 bitbucket pipleline 时,如何将值 "Branch.Name" 设置为其他值?

看了才知道。

- apt-get update
- apt-get install -y jq # install jq
- tmp=$(mktemp)
- jq '.Branch.Name = "prod"' appsettings.json > "$tmp" && mv "$tmp" appsettings.json

您可以使用标准 Bash/Shell 命令在文本文件中执行 'Find and Replace':

使用sed

sed -i -e 's/"test"/"prod"/g' appSettings.json

# Delete the unwanted backup file, created by sed
if [ -e appSettings.json-e ]
then
    rm appSettings.json-e
fi

使用 echo 和 Bash 字符串替换

如果您需要替换为 sed 无法处理的特殊字符,例如“/”和“\”,请使用此选项。

file_contents=$(< appSettings.json )
echo "${file_contents//\"test\"/\"prod\"}" > appSettings.json

更多信息和来源:Find and Replace Inside a Text File from a Bash Command