Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps YAML Pipeline Parameters Not Working from REST API Trigger

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 my-template.yaml.

parameters:
    - name: testParam
      type: string
      default: 'N/A'


steps:
    - script: echo ${{ parameters.testParam }}

My pipeline yaml that extends the template.

trigger:
    - master

extends:
    template: my-template.yaml

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

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?

like image 776
little-eyes Avatar asked Mar 25 '20 16:03

little-eyes


People also ask

How do you pass variables in Azure pipelines YAML tasks?

Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.

How do you use variables in Azure DevOps release pipeline?

You define and manage these variables in the Variables tab in a release pipeline. In the Pipeline Variables page, open the Scope drop-down list and select "Release". By default, when you add a variable, it is set to Release scope. Share values across all of the tasks within one specific stage by using stage variables.

How do you trigger a pipeline on a pull request Azure DevOps?

You can set up pull request triggers for both Azure Repos or GitHub repositories. From within your project, Select Pipelines > Releases, and then select your release pipeline. Under the Pull request trigger section, select the toggle button to enable it.

What is trigger in Azure pipeline YAML?

Scheduled release triggers allow you to run a release pipeline according to a schedule. Pull request release triggers are used to deploy a pull request directly using classic releases. Stage triggers in classic release are used to configure how each stage in a classic release is triggered.

How to trigger pipelines using REST API of Azure DevOps?

In this article I will explain how you could trigger pipelines execution using the REST API of Azure Devops. First things first you should create a personal access Token (PAT) in order to get the access required to run the pipelines on your organization. You can accomplish that by pressing your profile icon and selecting the sub-menu shown below.

How to set runtime parameters in a YAML pipeline?

Set runtime parameters at the beginning of a YAML. This example pipeline accepts the value of image and then outputs the value in the job. The trigger is set to none so that you can select the value of image when you manually trigger your pipeline to run. When the pipeline runs, you select the Pool Image.

How to override a YAML trigger in a pipeline?

You might consider using the pipeline trigger YAML override feature. Go to the pipeline designer/editor view. Next to the "Run" button is the ellipsis. From that menu, select "Triggers". From the "Continuous Integration" section, you can choose "Override the YAML continuous integration trigger from here".

What is a scheduled trigger in YAML?

Scheduled triggers are independent of the repository and allow you to run a pipeline according to a schedule. Pipeline triggers in YAML pipelines and build completion triggers in classic build pipelines allow you to trigger one pipeline upon the completion of another. Branch consideration for triggers in YAML pipelines


Video Answer


2 Answers

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 }}
like image 170
akokskis Avatar answered Oct 07 '22 21:10

akokskis


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}

enter image description here

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: enter image description here enter image description here

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

like image 2
Rawan-25 Avatar answered Oct 07 '22 22:10

Rawan-25