daxu
daxu

Reputation: 4104

Is it possible to trigger a build with another release in VSTS?

We created a nuget package and use VSTS release pipeline to release it.

Then we have a UI project that demos the usage of the nuget package.

Ideally, I would like to trigger the UI project's build automatically every time our nuget package is updated.

So in the UI project's project file, I did this:

<ItemGroup>
    <PackageReference Include="TheNugetPackageIwant" Version="1.*" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
  </ItemGroup>

I think this will make the UI project to always use the latest package (assume we always stick version 1).

However, how can I trigger the UI project to build automatically when a new nuget package becomes available? From devop, it seems a build can only be triggered by another build.

Is there a way to walk around it?

Upvotes: 1

Views: 108

Answers (1)

Eric Smith
Eric Smith

Reputation: 2560

You could use the Rest API and trigger the build pipeline from the context of the release whenever you publish the nuget package.

First make sure that your Service Account running the pipeline has access to queue a build.

enter image description here

enter image description here

Next in your release definition Select the Agent Job and in the options make sure that it can access the OAuth token.

enter image description here

Then add a powerShell Script in your release definition to queue the build. Note that you need to go and find the BuildDefinitionId of the Build you want to queue.

#Need to find the ID of your build definition. Just open the Defintion in the web UI it will be in the url
$BuildDefintionId = 37

$Body = @"
{
    "definition": {
        "id": $BuildDefintionId
    }
} 
"@

Write-host $Body

try {
    $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds?api-version=5.1"
    Write-Host "URL: $url"
    $response = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Body $Body -ContentType application/json
  if ($response -ne $Null) {
    Write-Host "*******************Bingo*********************************"
  }
}
catch {
  Write-Error $_
  Write-Error $_.Exception.Message
}

If that seems like too much work and you have the option, you can also try out an extension to do the heavy lifting for you.

Upvotes: 1

Related Questions