Warm tip: This article is reproduced from stackoverflow.com, please click
azure-devops azure-pipelines

Is it possible to get parameters from the pull request for Control Options on Azure Devops Pipelines

发布于 2020-04-13 10:44:39

I have a simple pipeline on Azure dev ops that has 4 jobs

  • Npm i
  • Npm run tests
  • Npm version Bump
  • Npm release

That works. At this moment version bump does only 'patch' every time as a PR is made to the master branch. So it releases only patch versions... I have a custom condition for that: contains(variables['Build.SourceBranch'], 'refs/heads/master'). Well what I want is to add one more condition here that decides to bump version dynamically as patch, major or minor for example... So I was thinking to get that parameter from the pull request title or description sth like that..:

contains(variables['Build.PR.Title'], 'patch') contains(variables['Build.PR.Title'], 'major') contains(variables['Build.PR.Title'], 'minor')

That is indeed 3 different jobs that will fire if the pr title has 'patch', 'major' or 'minor'.. Is it possible to do something like that or is there a easier way?:)

Thanx in advance!

Questioner
Mar
Viewed
116
Shamrai Aleksander 2020-02-04 02:50

Azure Devops pipeline does not pass the title of prs. You can find here the predefined variables: Use predefined variables. To get detailed information of active Pull Request you may use Rest Api Pull Requests - Get Pull Request By Id with System.PullRequest.PullRequestId. As example to get the title:

  1. Add a custom variable Define variables like Custom.PRTitle.
  2. Set the access to the PAT in your job. System.AccessToken
  3. Add first step as script to read and save the title of your pull requests, like with PowerShell:
$user = ""
$token = "$(System.AccessToken)"
$teamProject = "$(System.TeamProject)"
$PRId = "$(System.PullRequest.PullRequestId)"
$orgUrl = "$(System.CollectionUri)"

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

$uriGetActivePr = "$orgUrl/$teamProject/_apis/git/pullrequests/$PRId"

$resultPR = Invoke-RestMethod -Uri $uriGetActivePr -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

Write-Host "##vso[task.setvariable variable=Custom.PRTitle]"$resultPR.title

Then you can use your new variable in your conditions, like:

and(contains(variables['Build.SourceBranch'], 'refs/heads/master'), contains(variables['Custom.PRTitle'], 'patch'))