0

To queue a build i use this ps script

 param ( [string]$definitionName = "", [string]$testFilter = "" ) $rootTfsUri = "rootUri" $collectionName = "CollectionName" $projectName = "ProjName" $tfsUri = $rootTfsUri + "/" + $collectionName + "/" + $projectName $buildDefinition = "WebTesting-Regress-" + $definitionName $buildDefinitionUri = "$tfsUri/_apis/build/definitions?api-version=3.1&name=$buildDefinition" # first get build definition id $buildResponse = Invoke-WebRequest -Uri $buildDefinitionUri -UseDefaultCredentials -Method Get -Verbose -UseBasicParsing -ContentType "application/json" $buildResponseAsJson = $buildResponse.Content | convertfrom-json $buildDefinitionId = $buildResponseAsJson.value.id # Now queue this build definition $requestContentString = @" { "definition": { "id" : "$buildDefinitionId" }, "variables": { "testFilter": { "value": "$testFilter" } } } "@ $buildUri = "$tfsUri/_apis/build/builds?api-version=3.1" $buildResponse = Invoke-WebRequest -Uri $buildUri -UseDefaultCredentials -Method Post -Verbose -UseBasicParsing -ContentType "application/json" -Body $requestContentString $buildNumber = ($buildResponse.Content | ConvertFrom-Json).buildNumber 

it works good but i need to change build variable before my test started, how can i do this? i've tried to send it with id in body but it didn't works

VariablesExample

5
  • the body of a build also has a "parameters"-section, there you set your variable Commented Aug 16, 2019 at 14:40
  • the variable you need to change must be changable at queuetime Commented Aug 16, 2019 at 14:41
  • can you explain more detailed please Commented Aug 19, 2019 at 12:19
  • learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/… the build has a "parameters"-property that stores the variables that are changeable on queue-time among other parameters. look at completed builds to get an idea how this field has to be formatted Commented Aug 19, 2019 at 13:07
  • yeah, i saw an parameters in my uri that countains my variables but i still can't understand how to change them Commented Aug 19, 2019 at 13:50

2 Answers 2

1

You could use Logging Commands to set value during your build pipeline. ##vso[task.setvariable variable=testvar The first task can set a variable, and following tasks in the same phase are able to use the variable. The variable is exposed to the following tasks as an environment variable.

Define and modify your variables in a script

To define or modify a variable from a script, use the task.setvariable logging command. Note that the updated variable value is scoped to the job being executed, and does not flow across jobs or stages. Variable names are transformed to uppercase, and the characters "." and " " are replaced by "_".

For example, Agent.WorkFolder becomes AGENT_WORKFOLDER. On Windows, you access this as %AGENT_WORKFOLDER% or $env:AGENT_WORKFOLDER. On Linux and macOS, you use $AGENT_WORKFOLDER.

More details please take a look at this tutorial Define and modify your variables in a script You could also look at this blog: Use Azure DevOps Variables Inline powershell in build and release pipelines

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

1 Comment

To add to your correct answer: On how to use it, use the command in PowerShell Write-Host "##vso[task.setvariable variable=VariableName]VariableValue" and I also add a task to see ALL of my environmental variables in the logs. I add a Batch Script Task with the path "C:\Windows\System32\cmd.exe" and arguments "/c set". I don't think I can see the variable I just created task.setvariable, but you can try.
0
$definition = Invoke-RestMethod -Method Get -Uri "https://tfs-app/tfs/[COLLECTIONNAME]/[PROJECTNAME]/_apis/build/definitions/BUILDID?api-version=2.0" -UseDefaultCredentials $definition.variables.[VARIABLENAME].value = [NEWVALUE] $json = $definition | ConvertTo-Json -Compress -Depth 100 $updatedef = Invoke-RestMethod -Method Put -Uri "https://tfs-app/tfs/[COLLECTIONNAME]/[PROJECTNAME]/_apis/build/definitions/BUILDID?api-version=2.0" -UseDefaultCredentials -Body $json -ContentType "application/json; charset=utf-8" 

Now just queue your build.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.