Search code examples
buildmsbuildtfsbuildtfs-2015release-management

how to access $(Build.BuildNumber) from different project in tfs2015


There are two projects eg. Project1 and Project2. Now In project2, we need to create a release definition,in which we need to copy build output of project1's build definition.

Problem is that we could not give artifact as that of Project1 build definition in Project2 release.

If we could access the $(Build.BuildNumber) variable of project1 from Project2, prolem will be solve.

Please suggest.

Thank you.


Solution

  • You can access the build number via REST API, simple steps:

    1. Create a new powershell script file in your project with this code:

    Code:

    Param(
        [string]$teamProject,
        [string]$collectionURL,
        [string]$buildId,
        [string]$userName,
        [string]$password
        )
    $basicAuth = ("{0}:{1}" -f $userName,$password)
    $basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
    $basicAuth = [System.Convert]::ToBase64String($basicAuth)
    $headers = @{Authorization=("Basic {0}" -f $basicAuth)}
    $url="$($collectionURL)$($teamProject)/_apis/build/builds/$($buildId)?api-version=2.0"
    Write-Output $url
    Function GetBuildNumber{
        $result = Invoke-RestMethod -Uri $url -headers $headers -Method Get
        Write-Output $result.buildNumber
        Write-Host "##vso[task.setvariable variable=anotherBuildNumber;]$($result.buildNumber)"
    }
    GetBuildNumber
    
    1. Check into the source control
    2. Queue build and publish artifacts
    3. Edit your release definition
    4. Add these variables

    enter image description here

    1. Add PowerShell task to your release definition.

      Script filename:[your powershell script file in artifact, e.g. $(System.DefaultWorkingDirectory)/PowerShellVnext/TFS2015U2Solution/PowerShellModuleProject1/RestTest.ps1];

      Arguments: -teamProject $(teamProject) -collectionURL $(System.TeamFoundationCollectionUri) -buildId $(buildId) -userName $(username) -passwor $(password)

    After that the build number value is stored in anotherBuildNumber variable.