0
votes

I have successfully been able to run the below. I would like to alter this to work with passed in params. I don't mind how I get the params in. I just want to be able to programatically pass them in.My question is what is the syntax to pass via the rest api or the gui. See my attempt script at the bottom.

pipeline

    jobs: 
    - template: patch-template.yml  
      parameters:    
        liststuff:  
          - HostName: A
            Prop: XXXX
          - HostName: B
            Prop: YYYY

being used in a template as

    parameters:
      - name: liststuff
        type: object
        default: []
    jobs:
      - '${{ each item in parameters.liststuff }}':
          - template: patch-tasks.yml
            parameters:
              prop: '${{ item.Prop }}'
              hostname: '${{ item.hostname }}'

and steps implemented


    jobs:
       - job: 
         displayName: 'Job_${{ parameters.prop }}'
         steps:
         - task: PowerShell@2
           inputs:
             targetType: 'inline'
             script: |
               Write-Host "prop '${{ parameters.prop }}'"
               Write-Host "host '${{ parameters.hostname }}'"

Issue bit: changed pipeline to work with passed in params. I don't mind how I get the params in. I just want to be able to programatically pass them in.


    parameters:
    - name: InstanceArgs 
      type: object
      default: []   
    jobs: 
    - template: patch-template.yml  
      parameters:    
        liststuff: '${{ parameters.InstanceArgs }}'

and utilising api with powershell


    $url="https://dev.azure.com/{comp}/{project}/_apis/pipelines/{id}/runs?api-version=5.1-preview"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $JSON = @'
    {
        "templateParameters": {
            "listsuff": [{
                "hostname": "D",
                "prop": "ZZZZ"
            }]
        }
    }
    '@
    Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

1
Hi did you get a chance to try out below answer. did it work out?Levi Lu-MSFT
Sorry not as yet. I still want to try it. I have gone a different route. but I will test this this week. thanks for you responseKez

1 Answers

0
votes

When you using rest api to override the parameters of the yaml pipeline. The parameter's value in the request body templateParameters should be json string, which means you should convert the array object into json string in the request body.

You can define the templateParameters in request body in below format:

...
$JSON = @'
    {
        "templateParameters":{
           "InstanceArgs":"[{\"prop\":\"aaa\",\"hostname\":\"bbb\"}]"
        }
    }
    '@
Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

Or like below:

$JSON = @{
            templateParameters= @{
               InstanceArgs='[{"prop":"yyyy","hostname":"xxx"}]'
            }
         }
Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body (convertto-json $JSON) -ContentType application/json