lit*_*yes 8 azure-devops azure-pipelines azure-devops-rest-api
我正在尝试创建一个基于 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 buildREST API触发这个管道:https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1POST 主体如下。
{
    "parameters": "{\"testParam\": \"hello world\"}",
    "definition": {
        "id": 50642
    },
    "sourceBranch": "refs/heads/master"
}
所以我期待管道执行会回显hello world而不是N/A. 不幸的是,我仍然N/A在管道中看到结果。
任何人都知道发生了什么?我错过了什么吗?
ako*_*kis 10
我遇到了完全相同的问题 - 一个管道采用运行时参数,当通过 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/jsonAccept: application/jsonAUTHORIZATION: bearer $SYSTEM_ACCESSTOKEN.使用这种方法,在被调用的管道中,您将始终能够访问${{ parameters.testParam }}管道是通过 REST API 调用还是在 UI 中手动调用的值。
虽然您认为该值$(testParam)在通过 REST API 执行时可以访问是正确的,但在 UI 中运行管道时不会填充该变量。
因此,我建议使用这个未记录的 API,因为被调用的管道可以在${{ parameters.testParam }}不考虑它是如何被调用的情况下使用。当然,它(在写作时)没有记录,所以....¯_(?)_/¯
同样,应该注意的是,您的管道必须按照@Josh Gust 建议的格式进行格式化:
我的模板.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 }}
看起来parameters在这种情况下没有必要,我将 yaml 合并为如下所示。
# File: azure-pipelines.yml
trigger:
    - master
steps:
    - script: echo $(testParam)
$(testParam)注意和之间的区别${{ parameters.testParam }}。
然后我从 REST API 触发它,它工作得很好。
| 归档时间: | 
 | 
| 查看次数: | 5127 次 | 
| 最近记录: |