Search code examples
azureazure-devopscontinuous-integrationyaml

Conditionally Set Environment Azure DevOps


I am working with an Azure Pipeline in which I need to conditionally set the environment property. I am invoking the pipeline from a rest API call by passing Parameters in the body which is documented here.. When I try to access that parameter at compile time to set the environment conditionally though the variable is coming through as empty (assuming it is not accessible at compile time?)

enter image description here

enter image description here

Does anybody know a good way to solve for this via the pipeline or the API call?


Solution

  • After some digging I have found the answer to my question and I hope this helps someone else in the future.

    As it turns out the Build REST API does support template parameters that can be used at compile time, the documentation just doesn't explicitly tell you. This is also supported in the Runs endpoint as well.

    My payload for my request ended up looking like:

    {
        "Parameters": "{\"Env\":\"QA\"}",
        "templateParameters": {"SkipApproval" : "Y"},
        "Definition": {
            "Id": 123
        },
        "SourceBranch": "main"
    }
    

    and my pipeline consumed those changes at compile time via the following (abbreviated version) of my pipeline

    parameters:
      - name: SkipApproval
        default: ''
        type: string
    ...
            ${{if eq(parameters.SkipApproval, 'Y')}}: 
                environment: NoApproval-All
            ${{if ne(parameters.SkipApproval, 'Y')}}:
                environment: digitalCloud-qa