【问题标题】:JSON value substitution in bitbucket pipelinebitbucket 管道中的 JSON 值替换
【发布时间】:2020-01-18 19:05:06
【问题描述】:

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

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

我的问题是,在运行 bitbucket 管道时如何将值“Branch.Name”设置为其他值?

【问题讨论】:

    标签: bitbucket-pipelines


    【解决方案1】:

    在检查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】:

      您可以使用标准 Bash/Shell 命令在文本文件中执行“查找和替换”:

      使用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
      

      在 Bash 字符串替换中使用 echo

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-06-16
        • 2018-05-26
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        • 1970-01-01
        • 2017-10-17
        相关资源
        最近更新 更多