Search code examples
azure-devopspull-requestazure-pipelines-release-pipeline

How to include Source Branch name or pull request ID in ADO release title?


I have a CD release pipeline which gets deployed whenever new Pull Request (PR) build is succeeded. I want to include the source branch name of that PR as well as PR id in my release title.

I tried below variables in title, but they didn't work:

$(system.pullRequest.sourceBranch)

$(BUILD.PullRequest.SourceBranch)

$(BUILD.PullRequest.ID)

I just don't want to use $(Build.SourceBranch) or $(Build.SourceBranchName) as it doesn't give me expected value.

My expected release title: "PR Run- personal/ak/randombuild with PRID 1234 - 01"


Solution

  • PR variables are not available in the release scope: How do I manage the names for new releases?

    However,. you can use BuildId to get the detailed build information via Rest API and update your release name.

    The PowerShell example:

    $user = ""
    $token = $env:SYSTEM_ACCESSTOKEN
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    $orgUrl = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
    $teamProject = "$env:SYSTEM_TEAMPROJECT"
    $currentBuildId = "$env:BUILD_BUILDID"
    
    $restApiGetBuild = "$orgUrl/$teamProject/_apis/build/builds/$currentBuildId`?api-version=6.0"
    
    function InvokeGetRequest ($GetUrl)
    {   
        return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    }
    
    $resBuild = InvokeGetRequest $restApiGetBuild
    
    if (-not [string]::IsNullOrEmpty($resBuild.triggerInfo.'pr.number'))
    {
        Write-Host "##vso[release.updatereleasename]PR Run - PR ID" $resBuild.triggerInfo.'pr.number'
    }
    

    Add that as a step in the release. The result:

    enter image description here

    Do not forget to provide access to the token for your release pipeline:

    enter image description here