Search code examples
azure-devopsyamlazure-pipelines

Can a yaml pipeline trigged by another pipeline's completion access the completed pipeline's parameters?


I have a deploy and a test pipeline

The deploy pipeline is large with a huge number of stages, and can be set to run against different regions or environments

The test pipeline is small and triggers after the deploy pipeline finishes, and can also be set to run against different regions or environments

Is is possible to access the deploy pipeline's parameters from the automatically triggered test pipeline?

Here's an example of what I'm trying to do:-

deploy.yml

trigger: none
parameters: 
 - name: region
   type: string
   values: 
    - region 1
    - region 2

variables:
 - group: $(parameters.region) group variables

etc...

test.yml

trigger: none
parameters: 
 - name: region
   type: string
   values: 
    - region 1
    - region 2

variables:
 - name: derivedRegion
   value: coalesce($(DeployPipeline.region), $(parameters.region))
 
 - group: $(derivedRegion) group variables

resources:
  pipelines:
   - pipeline: DeployPipeline
     source: Deploy Pipeline
     trigger: 
etc...

Is it possible to get the DeployPipeline's parameter variables in time for a group variable?

I've not been able to find any resources on learn.microsoft.com regarding this.


Solution

  • For pipeline resource trigger, it's NOT supported to access the source pipeline parameter from triggered pipeline. All the variables are listed in official doc Pipeline resource metadata as predefined variables.

    As an alternative, you can use rest api Runs - Run Pipeline instead of pipeline resource trigger to trigger the target build pipeline, and set the parameter to target pipeline.

    For example:

    My source pipeline(change the build uri to yours):

    parameters: 
     - name: region
       type: string
       values: 
        - region 1
        - region 2
    
    variables:
     - group: ${{ parameters.region}} group variables
    
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $body = '
          { 
                  "definition": {
                      "id": 293
                  },
                  "templateParameters": {
                    "region": "${{ parameters.region}}"
                 }
          }
          '
          $personalToken = "$(personalToken)"
          $token =   [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
          $header = @{authorization = "Basic $token"}
          $Uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=7.1-preview.7"
          Write-Host $Uri
          $buildresponse = Invoke-RestMethod -Method Post -ContentType "application/json" -Uri $Uri -Body $body -Headers $header
          write-host $buildresponse
    

    My target build:

    parameters: 
      - name: region
        type: string
        default: region 1 in target2
    
    steps:
    - script: |
        echo ${{ parameters.region }}
      displayName: 'Check parameter value'
    

    It gets region 1 from source pipeline.

    enter image description here