1
votes

my query is How to pass parameters to azure Dev ops build pipeline externally to control the tasks execution? To explain in detail heres the explanation:

I have a project in the azure Dev ops which has a build pipeline configured with some series of tasks involving building the solution, generating deployable package and etc. Usually this is getting executed well and good without any issues.

What I want to achieve is declare a pipeline variable in this project build definition that I can access externally when I say it is some thing like an altogether different or External application like ms flow so that I can pass a value to the newly created pipeline variable as stated above and using this value stored in this new variable I should be able to skip few steps in the build pipeline andi should be able to execute only few steps.

Let me explain with an example:

  1. Consider a project called A in azure Dev ops which has a build pipeline configured with 5 different tasks
  2. In the same build pipeline let's say there is a new pipeline variable created called as flag
  3. Consider a external application like ms flow which triggers the project A build pipeline in step 1
  4. I should be able to pass a value to the flag variable crated in step 2 using the flow in 3rd step like true or false.
  5. Using the flag value passed to the build pipeline from ms flow the tasks in build pipeline should be executed i.e. If the value coming through the external application to the build pipeline is true then it should execute only 3, 4 tasks in the build pipeline and skip others of false should execute all the steps in the build pipeline in azure Dev ops.

The query is how to achieve or make this kind of behaviour to happen?

Please help me out in solving this issue?

If passing the value to variable is not possible, can you please let me know how to achieve the skipping behaviour in azure Dev ops build pipeline triggered from external application like ms flow?

2

2 Answers

2
votes

Since there is a REST API can pass parameters when queue a build and you can specify custom conditions in your build pipeline, there is a workaround. According to the REST API documentation, you can convert it to Powershell script like below.

Param(
       [string]$collectionurl = "https://dev.azure.com/{orgname}",
       [string]$project = "{projectname}",
       [string]$user = "{useraccount}",
       [string]$token = "{yourPAT}"
)

$base64AuthInfo= [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $token)))

$defurl = "$collectionurl/$project/_apis/build/builds?api-version=5.0"
$json = '{"parameters":  "{\"AnotherParameter\":  \"true\"}","definition":  {"id":  "{definitionId}"}}'
$updatedef = Invoke-RestMethod -Uri $defurl -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

You can create three build pipelines. The first pipeline is your main pipeline which has 5 different tasks. You can set the custom condition like below. It means, only when the value of AnotherParameter is true, the task will run.

enter image description here

The second pipeline and the third pipeline is the trigger pipeline. In the second pipeline, you can use powershell script to set the AnotherParameter as false and in the third pipeline, set the value as true.

Then set the second pipeline is triggered by external application and the third pipeline is triggered by your repo or others.

When the external application triggered the second pipeline, the value of AnotherParameter will be false and some of tasks in the first pipeline will not run.

When the third pipeline is triggered by your commit, the value of AnotherParameter will be true and all of task in the first pipeline will run as expected.

0
votes

You can call the Azure DevOps REST API and pass parameters in the request. Take a look at this post for more information: Start a build and passing variables through VSTS Rest API

If you want to add a condition to a task or a build pipeline, check out the documentation for conditions: Conditions - Azure Pipelines