简体   繁体   English

Azure 管道在构建过程中替换 json 键值

[英]Azure pipeline replace json key value during build process

I hope somebody can help me to solve this issue that is driving me crazy.我希望有人能帮助我解决这个让我发疯的问题。

I have a basic testing infra on which I am learning.我有一个正在学习的基本测试基础设施。

I have a basic web app that runs a Microsoft (tutorial) website.我有一个运行 Microsoft(教程)网站的基本 web 应用程序。

In my project I have a json file on which I used to update manually the version of the code to match always the tag on GitHub. Just to be clear, this is a basic version.json file with a version:在我的项目中,我有一个 json 文件,我曾在该文件上手动更新代码版本以始终匹配 GitHub 上的标签。需要说明的是,这是一个基本version.json 。json 文件的版本为:

{
    "app": {
      "name": "TestingInfra",
      "service": "TestInfraConfiguration",
      "version": "vX.X.X"
    }
}

When I am ready to deploy, I create a tag in GitHub and manually update the version number to match GitHub repo and I push.当我准备好部署时,我在 GitHub 中创建了一个标签,并手动更新version号以匹配 GitHub 存储库,然后我推送。

At this stage it was a bit annoying to do this manually, so I wanted to automate the sed of that value in the json to automatically pick the tag from my repo.在这个阶段手动执行此操作有点烦人,所以我想自动化 json 中该值的 sed 以自动从我的 repo 中选择标签。

In my pipeline yaml file as first step I have this configuration在我的管道 yaml 文件中,作为第一步,我有这个配置

- powershell: |
      # Write your PowerShell commands here.
      
      Write-Host "Update Build.BuildNumber"
      cd $(System.DefaultWorkingDirectory)
      $Latesttag = $(git describe --tags $(git rev-list --tags --max-count=1))
      Write-Host "The latest git tag is $Latesttag "
      Write-Host
      "##vso[build.updatebuildNumber]$Latesttag"
  - bash: |
      version=$Latesttag
      sed -i "s|$version|$version|g" ./version.json
      cat ./version.json

In the PowerShell task, I am overriding the BuildNumber of the agent with the Tag from my GitHub. And this works just perfectly.在 PowerShell 任务中,我用我的 GitHub 中的标签覆盖了代理的 BuildNumber。这非常有效。 What I would like to do, is to use the same $Latesttag from the Powershell, and replace the json file version in the version.json我想做的是使用 Powershell 中相同的$Latesttag ,并替换version.json中的 json 文件version

The pipeline does not fail like this, but also does not update the version placeholder.管道不会像这样失败,但也不会更新版本占位符。

Please can anyone help to understand how to achieve this?请任何人帮助了解如何实现这一目标?

And please if something is not clear, just ask for more informations, I will be glad to give as much details as I can如果有什么不清楚的地方,请询问更多信息,我很乐意提供尽可能多的细节

See if this gets you closer to the desired behavior.看看这是否能让您更接近期望的行为。 Note comments in the code explaining the changes.请注意代码中解释更改的注释。

- powershell: |
    Write-Host "Update Build.BuildNumber"
    cd $(System.DefaultWorkingDirectory)

    # Pretend the latest tag is v2.0.0
    $Latesttag = "v2.0.0"
    Write-Host "The latest git tag is $Latesttag "

    # *** Note change in this command compared to original post
    Write-Host "##vso[task.setvariable variable=LATEST_TAG]$Latesttag"

- bash: |
    # Pretend that you want to update v1.0.0 in version.json 
    # to the latest tag (v2.0.0) derived in the powershell step
    cat > ./version.json << EOF
    {
      "app": {
        "name": "TestingInfra",
        "service": "TestInfraConfiguration",
        "version": "v1.0.0"
      }
    }
    EOF
    echo "version.json - BEFORE CHANGE:"
    cat ./version.json

     # Do replace
    sed -i "s|v1.0.0|$LATEST_TAG|g" ./version.json
    
    # Verify change
    echo "version.json - AFTER CHANGE:"
    cat ./version.json

Output: Output:

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.201.1
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash /home/vsts/work/_temp/dheudhwj345-737-shej34.sh
version.json - BEFORE CHANGE:
{
  "app": {
    "name": "TestingInfra",
    "service": "TestInfraConfiguration",
    "version": "v1.0.0"
  }
}
version.json - AFTER CHANGE:
{
  "app": {
    "name": "TestingInfra",
    "service": "TestInfraConfiguration",
    "version": "v2.0.0"
  }
}
Finishing: Bash

You can use File Transform Azure .您可以使用文件转换 Azure

I did something like the below in my pipelines.yml我在我的pipelines.yml中做了类似下面的事情

variables
  buildNumber: '$(Build.BuildNumber)'
  ... other code
  steps:
      - task: FileTransform@1
        inputs:
          folderPath: '$(System.DefaultWorkingDirectory)/drop'
          fileType: 'json'
          targetFiles: 'assets/json/version.json'

I have a file in assets/json/version.json like below我在assets/json/version.json中有一个文件,如下所示

{
    "buildNumber" : ""
}

After steps from pipelines, It will replace the build number variable with correct number从管道执行步骤后,它将用正确的编号替换内部版本号变量

After Build构建后

{
    "buildNumber" : "2022.4.13.2"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM