1

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

  1. pipeline script

     variables:
        patchgroup: test
    
     jobs: 
     - template: patch-template.yml  
    
       parameters:    
         patchgroup: $(patchgroup)    
         sqlservers:      
           - sqlserver: name: ""
    
  2. patch template file

    parameters:
      sqlservers: {}
      patchgroup: ''
    
    jobs:
    - ${{ each sqlserver in parameters.sqlservers }}:
      - template: patch-tasks.yml
        parameters:
          sqlserver: ${{ sqlserver.name }}
          patchgroup: ${{ parameters.patchgroup }}
    
  3. 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 }}'"
    
  4. 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)}
    
2
  • Can you post the code where you try to "pass the parameters"? From what you posted, I don't see what're your passing to identify where it's going wrong. Commented Dec 15, 2020 at 2:15
  • Sorry I had tried a few things and had changed it to "variables" $json = '{"parameters": "{\"patchgroup\": \"xyxyxyxyx\"}","definition": {"id": "194"}}' Commented Dec 15, 2020 at 6:04

1 Answer 1

5

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.

enter image description here

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
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks I will give this a try. The issue I can see with this is I won't know how many I would want to pass in ... it could be 1 or more than 5. I might have to revisit this code and try to put in a loop.
Hi @Kez. I also considered this situation, so I further optimized the code. Please check my update. I set the parameters as object type. then you could enter any number of Parameters. This is similar to a loop. If it could meet your requirements, you may consider accepting it as answer. Thanks
Hi @Kez .Could you please check if the updated answer could meet your requirements. Feel free to let me know if the answer could give you some help.
Hi Kevin. this answer is great.. thanks... and it would definitely be a winner if I could only get it to run from the api. If I cut down the code it will run (ie: just the azure-pipelines.yml and remove template code) but otherwise as the code stands I cannot run it using the api. I have tried to figure out what bit of the code is causing the issue with but have had no success.
apologies. didn't use some of your code as you suggested. it works great. thanks so much
|

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.