Triggering an Azure Devops pipeline from another p

2020-05-01 19:41发布

问题:

I'm having problems triggering a pipeline from another Pipeline in Azure DevOps. I have a CI pipeline and I want to trigger a Deploy Pipeline whenever CI passes on a master branch. This seems to be technically possible, but the documentation is unclear.

I see the following:

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

But it's unclear as to a) whether this goes in the triggering pipeline (in my case the CI pipeline) or the triggered pipeline (in my case, the deploy pipeline).

It's also unclear as to what the pipeline and source refer to, and how I find out these variables? Are they both the name of the pipeline? I've tried various different permutations and nothing seems to be working.

回答1:

Above yaml pipeline trigger should be defined in the triggered pipeline(deploy pipeline).

- pipeline: string the string here is identifier you give to this pipeline resource. It can any string.

source: string the string here is the definition name of the triggering pipeline(the name of your CI pipeline).

Below yaml is from the document pipeline resource.

resources:
  pipelines:
  - pipeline: string  # identifier for the pipeline resource
    project:  string # project for the build pipeline; optional input for current project
    source: string  # source pipeline definition name
    branch: string  # branch to pick the artifact, optional; defaults to all branches
    version: string # pipeline run number to pick artifact, optional; defaults to last successfully completed run
    trigger:     # optional; triggers are not enabled by default.
      branches:
        include: [string] # branches to consider the trigger events, optional; defaults to all branches.
        exclude: [string] # branches to discard the trigger events, optional; defaults to none.

Option: You can also set the pipeline triggers from Ui page. Go the edit page of the triggered yaml pipeline(Deploy pipeline), Click the 3dots and choose Triggers

Go to Triggers--> Build completion and click add--> Select your triggering pipeline(CI pipeline)

Update:

I saw the pipeline resource in azure-deploy.yml is defined as below.

resources:
  pipelines:
  - pipeline: 'Deploy to Development'
    source: 'DFE-Digital.dfe-teachers-payment-service'
  trigger:
    branches:
      include:
      - "master"
      - "release-stuff"

please try changing the indentation of trigger element the same as source element. Check below example:

    resources:
      pipelines:
      - pipeline: 'Deploy to Development'
        source: 'DFE-Digital.dfe-teachers-payment-service'
        trigger:
          branches:
            include:
            - "master"
            - "release-stuff"


回答2:

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

Also, there is a very big restriction on the use of these types of triggers. It is necessary to change the defaultBranch for manual and scheduled builds in the depends pipeline, to the working branch. Otherwise it won't kick in at the end of the source pipeline execution. So, let's say you're working on feature branch, and defaultBranch is set to feature. You commit your code, and everything will run as expected: the source pipeline kicks in, and at its end, the depends pipeline will be triggered. All good! But when you will merge into master, if you do not change the defaultBranch, the depends pipeline won't be triggered at the end of the source pipeline. I explain how to change the defaultBranch at the end of the answer.


How to setup a pipeline trigger

I managed to get this up and running on a minimalistic project. Here you can have the code and here the project on Azure DevOps. I will try go guide you through how I did it, and answer the questions you've asked in your post.

I will be calling the triggered pipeline as depends pipeline and the triggering pipeline as source pipeline.

On the source pipeline, there's no need to do anything except publishing an artifact. If you don't publish an artifact from the source pipeline, it won't work. Below you can find the code I am using for my dummy source pipeline. I want it to be triggered for master branch, and at the end I want to be sure to publish an artifact.

trigger:
  branches:
    include: # branch names which will trigger a build
    - master
pr: none

steps:
  # required to cause pipeline triggering downstream
  - task: CopyFiles@2
    inputs:
      contents: $(System.DefaultWorkingDirectory)/**/*.yml
      targetFolder: $(Build.ArtifactStagingDirectory)
  - task: PublishBuildArtifacts@1
    inputs:
      pathtoPublish: $(Build.ArtifactStagingDirectory)
      artifactName: dummy-$(Build.BuildId)

On the depends pipeline (code shown below), I have to disable CI and PR triggers, otherwise when I commit to this repo, this pipeline will be triggered by the CI trigger, and then by the end of the execution of the source pipeline. This is done by the two first lines of my code. Then I want that the pipeline named source (this is the source property in the YAML below), within the project named Pipelining (project property in the YAML) will trigger the current (depends) pipeline when this updates master branch.

trigger: none
pr: none
resources:
  pipelines:
    - pipeline: source
      project: Pipelining
      source: source
      trigger: 
        branches:
          include:
          - master
steps:
  - checkout: none
  - script: echo 'triggered depends'

Does it make sense? It is important for your project name on Azure DevOps to match the property in the YAML depends pipeline code.For me it is Pipelining

As well as the source property, again in the YAML depends pipeline code.


Change the default branch

In order to change the defaultBranch, because of the issue mentioned above, you should edit the pipeline (in this case, the depends pipeline), then on the three dots on the top right corner pick Triggers. Then choose the YAML tab, and you will get to the screen shown in the image below, where you can set the working branch.