Azure devops-使用rest api传递多维参数 [英] azure devops - use rest api to pass multi dimensional parameters

查看:107
本文介绍了Azure devops-使用rest api传递多维参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够成功运行以下内容.我想更改它以与传入的参数一起使用.我不在乎如何获取参数.我只是想能够以编程方式将其传入.我的问题是,通过rest api或gui传递的语法是什么.请参阅底部的我的尝试脚本.

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.

管道


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

在模板中用作


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

实施步骤



    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 }}'

并在Powershell中使用api

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

推荐答案

使用rest api覆盖yaml管道的参数时.请求正文中的参数值 templateParameters 应该为json字符串,这意味着您应在请求正文中将数组对象转换为json字符串.

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.

您可以按以下格式在请求正文中定义 templateParameters :

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

或如下所示:

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

这篇关于Azure devops-使用rest api传递多维参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆