Search code examples
azureazure-pipelinescicdazure-pipelines-yaml

Pass parameters through Azure Pipelines


I have multiple Azure Pipelines. These work like a tree. So there is a main one if this is triggered all depending are also triggered at the end (if these are finished so also the depending has to be triggered and so on). But it has also be possible to start one of the "lower" ones so that part would be executed including all depending pipelines under this. So the pipelines will have the same parameters. If the pipeline is triggered from an "upper" one so this ones has to be used. If one is triggered manually so I can choose what I want.

What is the best way to achieve that?


Solution

  • I'm afraid there is no other way other than calling the REST API Runs - Run Pipeline to trigger the pipeline while passing parameters. You can call multiple REST API to trigger your several “lower" pipelines.

    I'm not sure if variables apply to your scenario, if so you might try using variable group inside the triggered pipelines. Run REST API Variablegroups - Update in your "upper" pipeline to update the variables in it. Refer to the sample below.

    1. Create a variable group. enter image description here

    2. In "upper" pipeline, call REST API Variablegroups - Update to update the variables.

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $token = "{PAT}"
          $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
          $url="https://dev.azure.com/{Org name}/_apis/distributedtask/variablegroups/{variablegroup Id}?api-version=7.1-preview.2"
          $body = @'
          {
              "name": "Shared Variables",
              "variables": {
                  "Var1": {
                      "isSecret": false,
                      "value": "New var1"
                  },
                  "Var2": {
                      "isSecret": false,
                      "value": "New var2"
                  }
              },
              "variableGroupProjectReferences": [
                  {
                      "description": "",
                      "name": "Shared Variables",
                      "projectReference": {
                          "id": "{Project Id}"
                      }
                  }
              ]
          }
          '@
          $head = @{ Authorization =" Basic $token" }
          Invoke-RestMethod -Uri $url -Method Put -Headers $head -Body $body -ContentType application/json
    
    1. Consume the variable group in the triggered pipelines.
    resources:
      pipelines:
      - pipeline: myresourcevars  # identifier for the pipeline resource
        source: MainPipeline # source pipeline definition name
        trigger: true 
    
    variables:
    - group: Shared Variables
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - task: CmdLine@2
      inputs:
        script: |
          echo 'The value of Var1:' $(Var1)
          echo 'The value of Var2:' $(Var2)
    

    enter image description here