我正在尝试创建一个基于YAML的管道,该管道需要一个参数,然后触发该管道以从Azure DevOps REST API运行。我能够看到构建队列,但是未从我的POST主体覆盖该参数。
我的模板my-template.yaml
。
parameters:
- name: testParam
type: string
default: 'N/A'
steps:
- script: echo ${{ parameters.testParam }}
我扩展模板的管道Yaml。
trigger:
- master
extends:
template: my-template.yaml
然后,我使用queue build
REST API:https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1
并通过POST正文触发此管道。
{
"parameters": "{\"testParam\": \"hello world\"}",
"definition": {
"id": 50642
},
"sourceBranch": "refs/heads/master"
}
因此,我期望管道执行将回显hello world
而不是N/A
。不幸的是,我仍然在管道结果中看到N/A
。
有人知道发生了什么吗?我有什么想念吗?
答案 0 :(得分:8)
我遇到了完全相同的问题-管道采用了运行时参数,该参数在通过UI(而非Queue Build REST API)运行时有效。
我能够通过使用未记录的API来解决此问题,该API与运行管道时Az DevOps Pipelines UI调用的API完全相同:
https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=5.1-preview
具有以下POST正文:
{
"stagesToSkip": [],
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/master"
}
}
},
"templateParameters": {
"testParam": "hello world"
},
"variables": {}
}
请注意,使用此API,您的运行时参数将作为实际的JSON(而非字符串化的JSON)并在键templateParameters
下提交。
同样,不要忘了包含一个可能期望调用的标准标头:
Content-Type: application/json
Accept: application/json
AUTHORIZATION: bearer $SYSTEM_ACCESSTOKEN
。使用这种方法,无论是通过REST API调用管道还是在UI中手动调用管道,您都将始终能够访问${{ parameters.testParam }}
的值。
虽然您正确地知道通过REST API执行时可以通过$(testParam)
来访问该值,但是在UI中运行管道时不会填充该变量。
因此,我建议使用此未公开的API,因为被调用的管道可以使用${{ parameters.testParam }}
,而无需考虑其调用方式。当然,它(在撰写本文时)是未记录的,所以...¯_(ツ)_ /¯
同样,应该注意,您的管道必须按照@Josh Gust建议的格式设置:
my-template.yaml:
parameters:
- name: testParam
type: string
default: 'N/A'
steps:
- script: echo ${{ parameters.testParam }}
azure-pipelines.yaml:
parameters:
- name: testParam
type: string
default: 'N/A'
trigger:
- master
extends:
template: my-template.yaml
parameters:
testParam: ${{ parameters.testParam }}
答案 1 :(得分:1)
花2到3个小时后得到解决方案:
https://dev.azure.com/{organization}/{project}/_apis/pipelines/2/runs?api-version=6.0-preview.1
Where 2= {pipelineId}
标题:
Authorization: Personal access token. Use any value for the user name and the token as the password.
Type: basic
Content-Type : application/json
Accept : application/json
现在我正在使用:Postman测试此API,所以分享发布主要屏幕截图:
在身体部分中:
{"previewRun":false,"stagesToSkip": [],"resources": {"repositories": {"self": {"refName": "refs/heads/master"}}},"templateParameters": {"testParam": "rawat Rob" },"variables": {}}
previewRun :{If true, don't actually create a new run. Instead, return the final YAML document after parsing templates.}
它对我有用,并且大约需要5到7次测试
答案 2 :(得分:0)
在这种情况下,不必像parameters
一样,我将yaml合并为如下所示。
# File: azure-pipelines.yml
trigger:
- master
steps:
- script: echo $(testParam)
请注意$(testParam)
和${{ parameters.testParam }}
之间的区别。
然后我从REST API触发它,并且效果很好。
答案 3 :(得分:0)
您没有将参数从管道发送到模板。
看看 documentation 如何做到这一点。我尚未测试过,但我认为,如果将参数正确连接到模板,则可以使用模板获得期望的结果。
基本上,您的模板应如下所示:
# File: simple-param.yml
parameters:
- name: yesNo # name of the parameter; required
type: boolean # data type of the parameter; required
default: false
steps:
- script: echo ${{ parameters.yesNo }}
您的管道应为:
# File: azure-pipelines.yml
trigger:
- master
extends:
template: simple-param.yml
parameters:
yesNo: false # set to a non-boolean value to have the build fail
通知 parameters: yesNo: false
也, Runtime Parameters Documentation 可能建议您将管道参数定义为显式参数。
答案 4 :(得分:0)
似乎是Azure DevOps Rest API中的问题:https://developercommunity.visualstudio.com/content/problem/1000544/parameters-to-api-rest-build-queue-method.html
我遇到了同样的问题,并注意到将runtime参数作为变量引入管道中。因此,在Yaml中使用$(MyParam)代替$ {{parameters.MyParam}}可以解决此问题。