Azure DevOps YAML管道参数无法通过REST API触发器工作 [英] Azure DevOps YAML Pipeline Parameters Not Working from REST API Trigger
问题描述
我正在尝试创建一个基于YAML的管道,该管道需要一个参数,然后触发该管道以从Azure DevOps REST API运行.我能够看到构建队列,但是该参数未从我的POST正文中覆盖.
I'm trying to create a YAML based pipeline that takes a parameter, then trigger the pipeline to run from a Azure DevOps REST API. I'm able to see the build gets queued, but the parameter was not overridden from my POST body.
我的模板my-template.yaml
.
parameters:
- name: testParam
type: string
default: 'N/A'
steps:
- script: echo ${{ parameters.testParam }}
我扩展模板的管道Yaml.
My pipeline yaml that extends the template.
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正文触发该管道.
Then I trigger this pipeline using the queue build
REST API: https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1
with a POST body as below.
{
"parameters": "{\"testParam\": \"hello world\"}",
"definition": {
"id": 50642
},
"sourceBranch": "refs/heads/master"
}
因此,我期望管道执行将回显hello world
而不是N/A
.不幸的是,我仍然在管道结果中看到N/A
.
So I'm expecting the pipeline execution will echo hello world
instead of N/A
. Unfortunately, I'm still seeing N/A
in the pipeline results.
有人知道发生了什么吗?我有什么想念吗?
Anyone has idea of what happened? Am I miss anything?
推荐答案
我遇到了完全相同的问题-管道采用了运行时参数,这些参数在通过UI运行时有效,但不能通过
I ran into the exact same problem - a pipeline taking runtime parameters that worked when run via the UI, but not via the Queue Build REST API.
我能够通过使用未记录的API来解决此问题,该API与运行管道时Az DevOps Pipelines UI调用的API完全相同:
I was able to solve this by using an undocumented API, the exact same one that the Az DevOps Pipelines UI calls when running a pipeline:
https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=5.1-preview
具有以下POST正文:
With the following POST body:
{
"stagesToSkip": [],
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/master"
}
}
},
"templateParameters": {
"testParam": "hello world"
},
"variables": {}
}
请注意,使用此API,您的运行时参数将作为实际JSON(而非字符串化JSON)并在键templateParameters
下提交.
Note that with this API, your runtime parameters are being submitted as actual JSON, not stringified JSON, and under the key templateParameters
.
同样,不要忘了包含一个可能期望进行此调用的标准标头:
As well, don't forget to include the standard headers one might expect for this call:
-
Content-Type: application/json
-
Accept: application/json
-
AUTHORIZATION: bearer $SYSTEM_ACCESSTOKEN
.
Content-Type: application/json
Accept: application/json
AUTHORIZATION: bearer $SYSTEM_ACCESSTOKEN
.
使用这种方法,无论是通过REST API调用管道还是在UI中手动调用管道,您始终可以访问${{ parameters.testParam }}
的值.
Using this approach, in the called pipeline, you will always be able to access the value of ${{ parameters.testParam }}
whether the pipeline is called via REST API or manually in the UI.
虽然正确无误,但通过REST API执行时,该值可作为$(testParam)
访问,但在UI中运行管道时不会填充该变量.
While you're correct that the value is accessible as $(testParam)
when executed via REST API, that variable is not populated when running the pipeline in the UI.
因此,我建议使用此未公开的API,因为被调用的管道可以使用${{ parameters.testParam }}
而不管它的调用方式.当然,它(在撰写本文时)是未记录的,所以...¯_(ツ)_/¯
As such, I'd recommend using this undocumented API, since the called pipeline can use ${{ parameters.testParam }}
without regard to how it's being called. Of course, it's (as of writing) undocumented, so.... ¯_(ツ)_/¯
同样,应该注意,您的管道必须按照@Josh Gust建议的格式设置:
As well, it should be noted that your pipeline must be formatted as @Josh Gust suggested:
my-template.yaml:
my-template.yaml:
parameters:
- name: testParam
type: string
default: 'N/A'
steps:
- script: echo ${{ parameters.testParam }}
azure-pipelines.yaml:
azure-pipelines.yaml:
parameters:
- name: testParam
type: string
default: 'N/A'
trigger:
- master
extends:
template: my-template.yaml
parameters:
testParam: ${{ parameters.testParam }}
这篇关于Azure DevOps YAML管道参数无法通过REST API触发器工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!