JSON value substitution in bitbucket pipeline

依然范特西╮ 提交于 2019-12-11 07:38:40

问题


I have a JSON file "appSettings.json" which has the following content:

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

My question is, how do I set the value "Branch.Name" to something else when running the bitbucket pipleline?


回答1:


figured it out after checking this question.

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



回答2:


You can use standard Bash/Shell commands to do a 'Find and Replace' inside a text file:

Use 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

Use echo with Bash string substitution

Use this if you need to replace with special characters like "/" and "\", which sed cannot handle.

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

More info and source: Find and Replace Inside a Text File from a Bash Command



来源:https://stackoverflow.com/questions/57982333/json-value-substitution-in-bitbucket-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!