azureazure-devopsbuild-pipeline

How to trigger a build from another build pipeline in azure devops


I have two build pipeline for two different projects.One is for building the actual project and another build pipeline for test automation. I want to automatically trigger the build pipeline of test automation once the actual project build succeed.

does there any possible way can i add one more task down to the actual build to trigger the test automation build, or suggest a possible way for the same.

enter image description here

Answers are much appreciable!!


Solution

  • You can use the "Build Completion" trigger in your second pipeline:

    enter image description here

    Additionally, you can add PowerShell script to queue another build from the parent build. Example:

    $user = ""
    $token = $env:SYSTEM_ACCESSTOKEN
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    $orgUrl = "$env:SYSTEM_COLLECTIONURI"
    $teamProject = "$env:SYSTEM_TEAMPROJECT"
    $buildBodyTemplate = "{`"definition`": {`"id`": <build_id>}}"
    
    $restApiQueueBuild = "$orgUrl/$teamProject/_apis/build/builds?api-version=6.0"
    
    function InvokePostRequest ($PostUrl, $body)
    {   
        return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
    }
    
    function RunBuild($buildId)
    {
        $buildBody = $buildBodyTemplate.Replace("<build_id>", $buildId)            
        Write-Host $buildBody
        
        $buildresponse = InvokePostRequest $restApiQueueBuild $buildBody
        Write-Host $buildresponse
    }
    
    RunBuild SECOND_BUILD_ID
    

    Update SECOND_BUILD_ID to ID of your build definition with tests. Additionally, add access to the security token in the parent build:

    enter image description here