Alex Kaszynski
Alex Kaszynski

Reputation: 1986

Azure Pipeline to trigger Pipeline using YAML

Attempting to trigger an Azure pipeline when another pipeline has been completed using a YAML. There's documentation indicating that you can add a pipeline resource with:

resources:   # types: pipelines | builds | repositories | containers | packages
  pipelines:
  - pipeline: string  # identifier for the pipeline resource
    connection: string  # service connection for pipelines from other Azure DevOps organizations
    project: string # project for the source; optional for current project
    source: string  # source defintion of the pipeline
    version: string  # the pipeline run number to pick the artifact, defaults to Latest pipeline successful across all stages
    branch: string  # branch to pick the artiafct, optional; defaults to master branch
    tags: string # picks the artifacts on from the pipeline with given tag, optional; defaults to no tags

However, I've been unable to figure out what the "source" means. For example, I have a pipeline called myproject.myprogram:

resources:
  pipelines:
  - pipeline: myproject.myprogram
    source: XXXXXXXX

Moreover, it's unclear how you'd build based a trigger based on this.

I know that this can be done from the web-GUI, but it should be possible to do this from a YAML.

Upvotes: 48

Views: 75767

Answers (5)

Sunny Goel
Sunny Goel

Reputation: 2132

For trigger of one pipeline from another azure official docs suggest this below solution. i.e. use pipeline triggers

resources:
  pipelines:
  - pipeline: RELEASE_PIPELINE # any arbitrary name
    source: PIPELINE_NAME.    # name of the previous pipeline shown on azure UI portal
    trigger:
      branches:
        include:
        - dummy_branch        # name of branch on which pipeline need to trigger

But actually what happens, is that it triggers two pipelines. Take an example, let suppose we have two pipelines A and B and we want to trigger B when A finishes. So in this scenario B runs 2 times, once when you do a commit (parallel with A) and second after A finishes.

To avoid this two times pipeline run problem follow the below solution

trigger: none # add this trigger value to none 
resources:
  pipelines:
  - pipeline: RELEASE_PIPELINE # any arbitrary name
    source: PIPELINE_NAME.    # name of the pipeline shown on azure UI portal
    trigger:
    branches:
      include:
        - dummy_branch        # name of branch on which pipeline need to trigger

By adding trigger:none second pipeline will not trigger at start commit and only trigger when first finish its job.

Hope it will help.

Upvotes: 58

Oliver Nilsen
Oliver Nilsen

Reputation: 1273

For my case, I was testing out my 2 YAML pipelines, CI and CD, from a features branch. Everything YAML code wise was the same as in this post, yet my CD pipeline wouldn't trigger when my CI completed in my feature branch. MS docs mention this:

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/pipeline-triggers?view=azure-devops

If your pipeline completion triggers don't seem to be firing, check the value of the Default branch for manual and scheduled builds setting for the triggered pipeline. The branch filters in that branch's version of the pipeline are used to determine whether the pipeline completion trigger initiates a run of the pipeline. By default, Default branch for manual and scheduled builds is set to the default branch of the repository, but you can change it after the pipeline is created.

So I used the Classical UI to set my CD pipeline's default branch to my feature branch that I am currently using to create CI-CD YAML pipelines:

enter image description here

and set the default branch to my feature branch:

enter image description here

And it worked immediately. When my CI pipeline completes, the CD pipelines is automatically triggered for my features branch:

enter image description here

When I'm done and satisfied with my CI-CD pipelines, I will switch the default branch in Triggers back to main branch.

Upvotes: 4

ccoutinho
ccoutinho

Reputation: 4534

If you're not publishing an artifact from the triggering pipeline, it won't trigger the triggered pipeline.

Also, if the defaultBranch for manual and scheduled builds in the triggered pipeline is not the same as your working branch, the triggered pipeline won't kick in at the end of the triggering pipeline execution.

I have created a minimum viable product for a pipeline trigger, and I explain better the two issues I just mentioned in this answer.

Upvotes: 6

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

The resources are not for the Build Completion trigger. according to the docs the build completion trigger not yet supported in YAML syntax.

After you create the YAML pipeline you can go to the classic editor (click on settings or variables) and there create the trigger.

Edit:

Now you need to click on the "Triggers":

enter image description here

And then:

enter image description here

Second Edit:

Microsoft added this feature also the YAML :) see here:

# this is being defined in app-ci pipeline
resources:
  pipelines:
  - pipeline: security-lib
    source: security-lib-ci
    trigger: 
      branches:
      - releases/*
      - master

In the above example, we have two pipelines - app-ci and security-lib-ci. We want the app-ci pipeline to run automatically every time a new version of the security library is built in master or a release branch.

Upvotes: 15

SijuMathew
SijuMathew

Reputation: 362

Microsoft documentation says that YAML is the preferred approach. So, instead of going for the build-trigger option let's understand the, little bit confusing, YAML trigger. The following tags will work from the original question and now with a bit easier documentation:

resources:
  pipelines:
  - pipeline: aUniqueNameHereForLocalReferenceCanBeAnything
    project: projectNameNOTtheGUID
    source: nameOfTheOtherPipelineNotTheDefinitionId
    trigger:
      branches:
        include:
        - master
        - AnyOtherBranch

The documentation from Microsoft is confusing and the IDs are numerous. At times they want the Project GUID at times the project name. At times they want the pipeline name and at times the pipeline definition Id. But they use the same name for the variable (project and pipeline). And on top of that they write documentation that does not make it easy to guess which one to use the best way is to trial and error.

I think to avoid the confusion in other places I'm giving example of another place in the pipeline you refer to the same variables with different values. In the DownloadArtifact task, you need to use the project GUID and the pipeline definition Id as shown below:

- task: DownloadPipelineArtifact@2
      inputs:
        source: specific (a literal constant value not the pipeline name)
        project: projectGUIDNOTtheProjectName
        pipeline: numericDefinitionIdOfPipelineNotPipelineNameOrUniqueRef
        runVersion: 'latest'

Just look at how they used the same variables in a different way, but both referring to a pipeline and in my case the same exact pipeline. That could create confusion and to avoid stumbling into the next issue I give it here for clarification.

Upvotes: 19

Related Questions