Reputation: 113
I am having trouble understanding what I need to do... but
I have a build pipeline... below are my base scripts for testing.
I want to be able to add/update parameters via the api and powershell. I am not certain if this is the best way to achieve this but it appears to work well when I manually add my parameters. When I pass a parameter it doesn't stick. Any assistance would be appreciated... even if I should post my query somewhere else.
Thanks
pipeline script
variables:
patchgroup: test
jobs:
- template: patch-template.yml
parameters:
patchgroup: $(patchgroup)
sqlservers:
- sqlserver: name: ""
patch template file
parameters:
sqlservers: {}
patchgroup: ''
jobs:
- ${{ each sqlserver in parameters.sqlservers }}:
- template: patch-tasks.yml
parameters:
sqlserver: ${{ sqlserver.name }}
patchgroup: ${{ parameters.patchgroup }}
patch tasks parameters: sqlserver: '' patchgroup: ''
jobs:
- job:
displayName: '${{ parameters.sqlserver }}--set-up-stuff'
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Write-Host "Patchgroup '${{ parameters.patchgroup }}'"
Write-Host "sqlserver '${{ parameters.sqlserver }}'"
powershell script
$defurl = "$collectionurl/$project/_apis/build/builds?api-version=5.0"
$json = '{"variables": "{\"patchgroup\": \"xyxyxyxyx\"}","definition": {"id": "194"}}'
#$json = '{"parameters":"{\"sqlservers\": \"\"{\"sqlserver\": \"servername\"}\"\"}","definition":{"id":"194"}}'
$updatedef = Invoke-RestMethod -Uri $defurl -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Upvotes: 1
Views: 3415
Reputation: 35514
According to your code, I have done some tests.
I notice that the value of your parameter is not set when you run the pipeline but needs to be directly hard-coded into the Yaml file.
So when you run the pipeline, you cannot pass parameters to the yaml source code via Rest API.
To solve this issue, you could try to use parameter to pass the parameter value.
Update:
You could check my new example:
patch-tasks.yml
jobs:
- job:
displayName: '${{ parameters.sqlserver }}--set-up-stuff'
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Write-Host "Patchgroup '${{ parameters.patchgroup }}'"
Write-Host "sqlserver '${{ parameters.sqlserver }}'"
patch-template.yml
parameters:
- name: sqlservers
type: object
default: []
- name: patchgroup
type: string
default: ''
jobs:
- ${{ each sqlserver in parameters.sqlservers }}:
- template: patch-tasks.yml
parameters:
sqlserver: ${{ sqlserver }}
patchgroup: ${{ parameters.patchgroup }}
pipeline script
trigger: none
parameters:
- name: InstanceArgs
type: object
default: []
variables:
patchgroup: test
jobs:
- template: patch-template.yml
parameters:
patchgroup: $(patchgroup)
sqlservers: ${{ parameters.InstanceArgs }}
When you run the pipeline, you could see the input box.
In this case, you can use Rest api to pass in parameter values when running the pipeline. You could change to use this Rest API: Runs - Run Pipeline
PowerShell Script sample:
$token = "PAT"
$url="https://dev.azure.com/{OrganizationNmae}/{ProjectName}/_apis/pipelines/{PipelineId}/runs?api-version=5.1-preview"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/BranchName"
}
}
},
"templateParameters": {
"InstanceArgs":"[1,2,3]"
},
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
Upvotes: 5