azure-devopsazure-pipelinesazure-pipelines-yaml

Passing branch names between pipelines


So far I was using the below pipeline which triggers the main pipeline. It tracks the pushed tags by developer to the master branch, and if so, starts running.

first_pipeline.yml

trigger:
  tags:
    include:
      - '*'
  paths:
    include:
      - master

variables:
  agent_pool: 'DEVAGENT'

pool:
  name: $(dev_pool)

The above pipeline triggers that one:

main_pipeline.yml

trigger: none
pr: none

resources:
  repositories:
    - repository: Grand
      type: git
      name: Automotive/Grand
      ref: master
      endpoint: Grand
    - repository: CICD
      type: git
      name: pipelines
      ref: master
  pipelines:
    - pipeline: Grand
      source: 'Grand'
      project: Automotive
      trigger: true

variables:
  - template: variables.yml

stages:
    - stage: DEV
      displayName: 'Development'
      variables:
        - name: environment
          value: ${{ variables.env_dev}}
      pool:
        name: ${{ variables.agent_dev }}
      jobs:
        - deployment: DEV
          displayName: 'Deploying to DEV'
      # Rest of the code   
  - stage: PRD
    displayName: 'Production Stage'
    variables:
      - name: environment
        value: ${{ variables.prd }}
    pool:
      name: ${{ variables.agent_prd }}
    jobs:
      - deployment: PRDDeploy
        displayName: 'Deploying to PRD'
    # Rest of the code

Above pipeline deploys changes to DEV stage, and than to PRD stage.

Now I'm trying to modify the pipeline to work in such a way that, for example:

So far it works only with the last condition, my first attempt leads to errors, because I am not sure how to pass those branch names from the first to main pipelines and modify that second one.


Solution

  • When you use the pipeline.resources.repository.trigger there are additional pipeline variables available:

    resources.pipeline.<Alias>.projectName
    resources.pipeline.<Alias>.projectID
    resources.pipeline.<Alias>.pipelineName
    resources.pipeline.<Alias>.pipelineID
    resources.pipeline.<Alias>.runName
    resources.pipeline.<Alias>.runID
    resources.pipeline.<Alias>.runURI
    resources.pipeline.<Alias>.sourceBranch
    resources.pipeline.<Alias>.sourceCommit
    resources.pipeline.<Alias>.sourceProvider
    resources.pipeline.<Alias>.requestedFor
    resources.pipeline.<Alias>.requestedForID
    

    Based on your triggers, it looks like you're using either a "tag" or commits to "master" to trigger the pipeline. You should be able to use $(resources.pipeline.Grand.sourceBranch) to get the originating branch. According to this answer, when a build is triggered by a tag, the sourceBranch will start with /refs/tags/<tag-name>.

    Based on this, you should be able to do something like the following:

    #first pipeline
    trigger:
      tags:
        include:
        - '*'
      branches:
        include:
        - master
    
    ...
    
    
    
    ```yaml
    # main_pipeline.yml
    trigger: none
    
    resources:
      pipelines:
      - pipeline: Grand # this is the alias
        source: 'Grand'
        project: Automotive
        trigger: true
    
    stages:
    - stage: dev
      condition: |
        or(
          startsWith(variables['resources.pipeline.Grand.sourceBranch', 'refs/tags/dev'),
          eq(variables['resource.pipeline.Grand.sourceBranch', 'refs/heads/master')
          )
      ...
    
    - stage: prod
      condition: |
         or(
          startsWith(variables['resources.pipeline.Grand.sourceBranch', 'refs/tags/feature'),
          eq(variables['resource.pipeline.Grand.sourceBranch', 'refs/heads/master')
         )
      ...