3

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"

10
  • What happens if you change it to "test" | ConvertTo-Json | Set-Content -Path $JsonPath? Commented Nov 15, 2019 at 9:41
  • In this situation, script will overwrite json file content, content of json file will be "test". Commented Nov 15, 2019 at 9:48
  • Good, so the line that sets the file content works and is being executed. That can only mean one thing, the line that changes the data is not being executed, or does not do what you think it does. When I try with the sample code you provide here, it works. And that means your real code is different from your question here. Commented Nov 15, 2019 at 9:55
  • Problem is that I tested this code as well, on my laptop lacally, it worked. It doesnt work on azure devops. Commented Nov 15, 2019 at 9:58
  • @Tomalak - Did You tested Your code on azure devops or on locally? Commented Nov 15, 2019 at 10:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

In order to push json file changes to devops Repo I decided to add separate task in my pipeline, powershell task. in Inline script section I ve pasted presented code: git config --global user.email "[email protected]" git config --global user.name "xxx" git add var.json git commit -m "Json file update" git push. I used option: "Allow scripts to access OAuth token.", but facing errors due. This git code block is correct or should I add something?
@tester81 Looks very make sense. Is it completely succeed?
still is something wrong, this time with this git code block
@tester81 There’re something missing in your script block. Have update my answer with this, please check my updated answer.
I added all required permissions, used link - learn.microsoft.com/en-us/azure/devops/pipelines/scripts/…. I have created CMD tasks as You suggested, 1 and 2nd task completed succesfully, but 3rd ended with the issue --> "fatal: could not read Username for 'dev.azure.com': terminal prompts disabled". My first task contains lines: git config --global user.email "[email protected]" git config --global user.name "Robot Agent" cd $(Build.SourcesDirectory) git init
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.