Azure DevOps YAML Pipeline Parameters Not Working from REST API Trigger

后端 未结 2 2145
时光取名叫无心
时光取名叫无心 2021-02-16 00:16

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 th

相关标签:
2条回答
  • 2021-02-16 00:56

    Got the solution after spending 2 to 3 Hour:

    https://dev.azure.com/{organization}/{project}/_apis/pipelines/2/runs?api-version=6.0-preview.1
    
       Where 2= {pipelineId}
    

    Header :

    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
    

    Right Now I'm using: Postman for testing this API So sharing posting main screenshot:

    In the Body part :

    {"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.}
    

    It is working for me And having test around 5 to 7 time

    0 讨论(0)
  • 2021-02-16 01:02

    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.

    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

    With the following POST body:

    {
      "stagesToSkip": [],
      "resources": {
        "repositories": {
          "self": {
            "refName": "refs/heads/master"
          }
        }
      },
      "templateParameters": {
        "testParam": "hello world"
       },
      "variables": {}
    }
    

    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.

    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.

    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.

    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.... ¯_(ツ)_/¯

    As well, it should be noted that your pipeline must be formatted as @Josh Gust suggested:

    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 }}
    
    0 讨论(0)
提交回复
热议问题