Thomas Segato
Thomas Segato

Reputation: 5213

Trigger another pipeline and set variable

I have a pipeline. The pipeline has a variable for the different environments. Now instead of copy the pipeline in three I would like to have a single pipeline the three other pipelines starts with a specific variable. When the pipelines calling the master pipeline has finished the releases should then be called for each. Is this possible with yaml and pipelines?

Upvotes: 1

Views: 519

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40533

If you want to end up with just two pipeline where the second is parameterized you may have an issue to pass that variable. A perfect match seems to be runtime parameters. You may pass those parameters using REST API endpoint. This is sample example how you may run REST API from pipeline

- pwsh: |
    
    $url = "https://dev.azure.com/thecodemanual/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)?api-version=5.1"
    $build = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:MY_SECRET"}
    Write-Host "Pipeline = $($build | ConvertTo-Json -Depth 100)"

    $status = $build.status
    Write-Host $status
  name: initial
  env: 
    MY_SECRET: $(System.AccessToken)

Note: this call different endpoint than the one mentioned above.

However I would recommend you templates. You may use this to extract common part and pass paramaters so it seems that it suits your case.

template

parameters:
- name: name
  type: string
  default: 'lambdaNumbertOneFunctionName'
- name: arn
  type: string
  default: 'lambdaNumberOneFunctionArn'
- name: stepName
  type: string
  default: 'StepOne'
- name: value
  type: string
  default: 'testValue'

steps:
- bash: |
    echo "##vso[task.setvariable variable=${{ parameters.name }};isOutput=true]${{ parameters.value }}"
    echo "##vso[task.setvariable variable=${{ parameters.arn }};isOutput=true]${{ parameters.value }}"
    echo "##vso[task.setvariable variable=Test;isOutput=true]${{ parameters.value }}"
    echo "##vso[task.setvariable variable=Test2;isOutput=true]Yesy2"
  name: ${{ parameters.stepName }}

- script: |
    echo $(${{ parameters.stepName }}.${{ parameters.name }})
    echo $(${{ parameters.stepName }}.${{ parameters.arn }})
    echo $(${{ parameters.stepName }}.Test)
    echo $(${{ parameters.stepName }}.Test2)

main file

  steps:
  - template: template.yaml
    parameters:
      name: 'lambdaNumbertOneFunctionName'
      arn: 'lambdaNumberOneFunctionArn'
      value: 'value-1'
      stepName: 'StepOne'
  - template: template.yaml
    parameters:
      name: 'lambdaNumberTwoFunctionName'
      arn: 'lambdaNumberTwoFunctionArn'
      value: 'value-2'
      stepName: 'StepTwo'

And templates you can use it in thre ways:

  • define all three parts as separate stages/jobs in your main build
  • define all three parts as separate stages/jobs in your second build and then trigger it using pipeline trigger
  • define three separate pipelines and tigger them using above mentioned pipeline trigger

Upvotes: 1

Related Questions