0

Could someone help me with how to call one pipeline from another pipeline in Azure DevOps?

I have to run a pipeline and this should trigger another pipeline in different project.

Bernard Vander Beken
  • 4,848
  • 5
  • 54
  • 76
priya
  • 391
  • 1
  • 5
  • 24
  • Not get your latest information, is the workaround helpful for you? Or if you have any concern, feel free to share it here. – Hugh Lin Aug 10 '20 at 01:45
  • Here's a [detailed answer](https://stackoverflow.com/questions/60643528/triggering-an-azure-devops-pipeline-from-another-pipeline/61398607#61398607) on how to achieve that. – ccoutinho Jun 28 '22 at 12:51

6 Answers6

3

I think resources for Azure Pipelines is what you looking for.

Add a resource in the pipeline that shall be called from another one and name the source pipeline:

# Explicitly set none for repository trigger
trigger:
- none

resources:
  pipelines:
  - pipeline: myappbuild  # Name of the pipeline resource
    source: myapp-build-pipeline # Name of the triggering pipeline
    trigger: 
      branches:
      - master
lordisp
  • 634
  • 9
  • 21
  • yes, I think this is the best/correct answer. There's some other native options to create a trigger based on the completion of a pipeline. This seems to be the is the way to do it in YAML. Thanks. I look forward to trying this out. – Joshua K Dec 16 '22 at 16:28
2

You can try to use Trigger Azure DevOps Pipeline task to trigger another pipeline in different projects. Depending on your choice in the task it will trigger a build or a release pipeline.

To be able to use the extension an Azure DevOps API endpoint needs to be created. For the service connection to work as it should you need to configure the following parameters:

How you can create a personal access token can be found here: Use personal access tokens to authenticate. Make sure the personal access token has the following rights:

  • Triggering a Release: Release – Read, write & execute – Build Read & Execute
  • Triggering a Build: Read & Execute

enter image description here

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
1

You can use API for triggering build. Here is the example that I use to trigger another build pipeline.

- powershell: |
   # Write your PowerShell commands here.   
   Write-Host " ***** Start Script ***** "
   $body = '
   { 
    "parameters":  "{\"parameter1\":  \"value1\"}",
    "definition": {"id": "1234"},
    "sourceBranch": "git/branch",
    "templateParameters": {"templateparameter": "paramvalue"}
   }
   '
   $bodyJson=$body | ConvertFrom-Json
   Write-Output $bodyJson
   $bodyString=$bodyJson | ConvertTo-Json -Depth 100
   Write-Output $bodyString
   $user="$(user)"
   $token="$(token)"
   $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

   $Uri = "https://tfs.com:8443/Organization/_apis/build/builds?api-version=6.1-preview.6"
   $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $bodyString -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
   write-host $buildresponse
   $buildID = $buildresponse.id
   write-host $buildID
   Write-Output "Build ID is $buildID... Sleep for 5 seconds.."
   Start-Sleep -Seconds 5
   $buildInfo = ( Invoke-RestMethod -Method Get -UseDefaultCredentials -Uri "https://tfs.com:8443/Organization/_apis/build/builds/${buildID}?api-version=6.1-preview.6" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} )
   while($buildInfo.status -eq "inProgress" -or $buildInfo.status -eq "notStarted") # keep checking till build completed
   {
      Write-Output "Build is $($buildInfo.status)... Sleep for 5 seconds.."
      Start-Sleep -Seconds 5 # Start sleep for 5 seconds
      $buildInfo = ( Invoke-RestMethod -Method Get -UseDefaultCredentials -Uri "https://tfs.com:8443/Organization/_apis/build/builds/${buildID}?api-version=6.1-preview.6" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} )
   }

   Write-Output "Build Status : $($buildInfo.status)" # print build status
   Write-Output "Build Result : $($buildInfo.result)" # print build result
  displayName: 'Trigger Another Build Pipeline'
  • hi Rahul; can I check, do you find it a problem that you have one agent tied up waiting for the second pipeline to complete? – Vince Bowdren Feb 26 '22 at 20:41
  • Hi Vince; you may choose to remove the waiting check. For my requirement i needed the triggered pipeline to finish successfully before proceeding to next task in the current pipeline, hence, the waiting block. It is totally optional, you may ignore the waiting block. – Rahul Kawadkar Feb 28 '22 at 05:26
1

Here is an implementation I use with the following:

I created a stage

######### stage_call_other_pipelines ###################
#########################################################
- stage: stage_call_other_pipelines
  displayName: "call other pipelines"
  jobs:
    #XYZ deployment
    - job: job_call_XYZ_deployment
      displayName: "execute XYZ deployment"
      steps:
        - checkout: none
        - task: PowerShell@2
          displayName: "via REST API"
          env:
            SYSTEM_ACCESSTOKEN: $(System.AccessToken)
          inputs:
            targetType: 'inline'
            script: |
              #url
              $url = 'https://dev.azure.com/XYZOrganization/XYZProject/_apis/build/builds?api-version=5.0'
              #header
              $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
              $headers.Add("Content-Type", "application/json")
              $headers.Add("Authorization","Bearer $env:SYSTEM_ACCESSTOKEN");
              #body
              $body = "    {
              `n        `"definition`": {
              `n            `"id`": 134
              `n        },
              `n        `"templateParameters`": {
              `n            `"ParameterA`": `"ParameterValueA`",
              `n            `"ParameterB`": `"ParameterValueB`"
              `n        }
              `n    }"
              #call rest api
              $response = Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $body
              #output
              $response | ConvertTo-Json
            failOnStderr: true
            pwsh: true

The token is passed to the agent as an environment variable. The pipeline and its parameters are defined in the body.

Additional

On the pipeline that is to be executed, permissions must be adjusted.

  1. Go to the desired pipeline, click in the right upper corner on the menu button and select "Manage security"

    Pipeline - manage security

  2. A form will apear. Choose the Build Service service principal and set "Queue builds" on "Allow"

    Pipeline - build service permissions

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
HeeLoco
  • 11
  • 1
0

You can install az devops extension in your pipeline agent and then you can call az pipeline CLI commands" to manage other build or release pipelines. Next, you can call az pipeline CLI commands from your main pipeline and for this you can use AzureCLI task or Bash task.

nick
  • 29
  • 8
  • Is there any other way? the organization is similar its just that..pipelines are in different projects. – priya Aug 05 '20 at 10:14
  • I have pipeline written in this form. But this pipeline is running after the mentioned project pipeline ran. But I want in the opposite way, I should run this parent pipeline and the child pipeline should trigger automatically – priya Aug 05 '20 at 10:16
  • parent pipeline: resources: pipelines: - pipeline: TestSonarQubeIssueReport project: # project name source: # Name of the triggering pipeline trigger: branches: - feature – priya Aug 05 '20 at 10:16
  • let me know how my child pipeline task should be written to trigger only when my parent pipeline is built. – priya Aug 05 '20 at 10:19
  • The other way is to use REST API calls from your main pipeline using programming languages like python or simple curl calls in bash. – nick Aug 05 '20 at 10:21
  • One solution is that you can disable all the triggers in the child pipeline and then let build of your parent pipeline to do API calls (or use az pipelines) to your child pipeline. – nick Aug 05 '20 at 10:39
0

Since the OP didn't specify how they wanted to accomplish this, I'll share how I'm doing this without YAML pipelines. I use the Edit pipeline option then the meatball menu to select triggers and I can specify which triggering build with branch filters.

enter image description here

Joshua K
  • 457
  • 3
  • 12