mascot
mascot

Reputation: 193

Azure Pipeline build specific pull requests

I have a list of pull requests, each linked to the specific work items. I want to run a pipeline only for some of the work items. Therefore I would like to somehow automatically cherry-pick only commits/pull requests related to work items I want to build. Either using tags on work items or something similar. In the build, I need only the files which were changed, so I was thinking of using git diff. Is that possible using the Azure Pipelines?

Upvotes: 0

Views: 449

Answers (1)

Yan Sklyarenko
Yan Sklyarenko

Reputation: 32270

If I understand your request correctly, you need to run the build pipeline only for those pull requests which meet certain requirements, for example, are linked to the work items tagged with specific words. Although I'm sure there's no out-of-the-box solution to this, I think it is possible to achieve with the Conditions concept and the REST API.

I would follow these steps:

  • The very first build step in the build job should be a script to find out if this particular pull request being built is related to the target work items. It can be a PowerShell build step, which should:

    • Get the pull request ID - it is available as a predefined system variable called System.PullRequest.PullRequestId
    • Make a REST API call to get work item IDs related to this pull request. It will return a collection of {id, url} pairs, where id is an ID of the work item in question
    • For each found work item ID, make a REST API call to get the details of the work item, in particular, you're interested in the System.Tags property of the response
    • Again, for each work item, parse the System.Tags property value to find out if it contains necessary tags
    • If at least one related work item contains the tags you're interested in, set the flag variable called i.e. ShouldRunTheRestOfThePipeline to true
  • Conditionalize the further steps of the job with the custom condition. You can read more about the syntax of the expression here. In particular, the Q&A section describes how to use the variable set in the previous build step in a subsequent step's condition

Now, regarding your notes about taking only changed files. In fact, the pull request nature is to keep the changed files compared to the target branch it is created against. So, you don't need to do any extra actions to git diff - the pull request already contains all you need.

Finally, I would suggest you reconsider the requirements of this setup, 'cause it sounds a bit unnatural to me. There are other options for how not to trigger a pipeline. For example, it can be decided based on the branch/tag name, both include and exclude options are available. Or, you can specify file paths to include or exclude, for instance, never trigger a pipeline if only docs/* were changed. The wildcard syntax is supported, which makes this tool even more flexible.

Hope this can help you build a suitable solution to your specific scenario.

Upvotes: 1

Related Questions