tester81
tester81

Reputation: 595

Azure devops - update json file - powershell script

I have created powershell script to update json file with variables. Json file is located in Azure devops repo, json file name var.json. I am going to use this solution in azure devops, so I built pipeline and set test variable in variables tab in azure devops:

enter image description here

In my script I have param and variables blocks, presented below:

param(
    [Parameter (Mandatory=$true)]
    [String] $FileRes
)
#env variable
$Path = $Env:BUILD_SOURCESDIRECTORY

# Download variables from Json file
$JsonBase = @()
$JsonPath = "$Path\Var.json"
$JsonBase = Get-Content $JsonPath | out-string | ConvertFrom-Json

$JsonBase.FileNames[0].value = $FileRes

in my script I use commands: $JsonBase | ConvertTo-Json | Set-Content -Path $JsonPath to direct output to json file.

Json file structure:

{
    "FileNames":  [
                      {
                          "value":  "AAAbbbccc123",
                          "value1":  "www",
                          "value3":  "swd",
                          "value4":  "xvb"
                      }
                  ]
}

Pipeline's status at the end is ok, all steps are green, but var.json file is not updated as I wanted. There is still old value --> "value": "AAAbbbccc123"

Upvotes: 3

Views: 5751

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 18978

In fact, it has been replaced, but you need to see this change in the output repos.

For more clearly, you could use private agent to run this build. Then go the corresponding local repos and check the Var.json file after the build finished:

enter image description here

In your script, you are Set-Content into the file which exists under the $(Build.SourcesDirectory)\Var.json, not the one which stored in VSTS repos. So, to check whether it is replaced successfully, please go your output repos, the one in agent.

Sometimes, if what you used is hosted agent, you may could not view the detailed output repos since the host image will be recycled by the server after the pipeline finished.

At this time, you can add another script in it to print the JSON file content out, then you could check whether it is replaced successfully:

$content= Get-Content -Path $JsonPath
Write-Host $content

enter image description here


In addition, please make a little change into your script:

$JsonBase.FileNames[0].value = "$(FileRes)"

Here please use $(FileRes) instead of $FileRes, since you specified the value in the Variables tab. And do not forget the double quote "".

Update:

To sync the output repos change back into VSTS repos, try follow:

enter image description here

(1) The first command line task:

git config --global user.email "[email protected]"
git config --global user.name "Merlin"

cd $(Build.SourcesDirectory)
git init

(2) In powershell task, execute set-content script.

(3) In second command line task, do git push to push the changes:

git add Var.json
git commit -m "aaaa"
git remote rm origin
git remote add origin https://[email protected]/xxx/xxx/_git/xxxx
git push -u origin HEAD:master

enter image description here

In addition, to run git script successfully in pipeline. Beside enable “Allow script to access........” you also should follow this permission setting.

Upvotes: 2

Related Questions