1

I am writing azure pipelines and using variables from variable groups. As part of adding flexibility, we are trying to make most of the parts of pipeline configurable ( more variables). So, i was looking for a way to add default value to variable if it is not present in variable group.

The way we use is like below:

variables:
  dockerId: $(docker_id)
  imageName: $(DOCKER_IMAGE_NAME)

Is there option to specify default for the variable, if it doesn't find it from variable group. Something like below:

variables:
  dockerId: $(docker_id:"defaultDockerId")
  imageName: $(DOCKER_IMAGE_NAME:"defaultDockerImageName")
4
  • 1
    I don't think you can do that, but what you can actually do is create variables inside a task. So you could have some powershell or bash task that will create the variables if they do not exist Commented Apr 7, 2020 at 5:32
  • Thanks Carlos. I was looking for python os.getenv("variable","default") kind of syntax, which makes the job very easy instead of adding check for each variable and creating it if it doesn't exist. Commented Apr 7, 2020 at 5:42
  • you can build that experience in a task.. you can even do it in python :) I would still recommend to have the default values somewhere else, maybe a template file, and a task that takes that template file and do your getenv("variable", "default) Commented Apr 7, 2020 at 8:03
  • Hi @SunilSaggar as it is commented above, you may have to do it inside the script task or use templates. You can check out the example in below answer. Commented Apr 10, 2020 at 9:24

2 Answers 2

3

You cannot achieve this directly in azure pipeline. Azure pipeline doesnot have this feature currently and this syntax $(docker_id:"defaultDockerId") is not supported.

As workaround, you may have to write scripts in script tasks to assign the default value if the variables are not exist.

Please check out below python script:

The first python script task checks if the variable exists and set a default value for it, and define the variable using print("##vso[task.setvariable variable=variableName]value").

(Please note the variable variableTest value can only be referenced in the following tasks.)

- task: PythonScript@0
  displayName: 'setDefaultValue'
  inputs:
    scriptSource: inline
    script: |
     import os   

     b = os.getenv("variableTest","default value for variableTest")

     print("##vso[task.setvariable variable=variableTest]{b}".format(b=b))

- task: PythonScript@0    
  displayName: 'Run a Python script'    
  inputs:
    scriptSource: inline    
    script: |
        print("$(variableTest)")

Hope above helps!

Sign up to request clarification or add additional context in comments.

2 Comments

This is better ..!! Thanks
Unfortunatelly this doesn't work when using variables in repositories!
1

The comment is correct, you cannot have defaults (unless you are using templates and parameters, which you might want to look at, but they are not exactly the thing you are after). You can either use an if condition if you know when the variable exists or not (in the yaml file) or use a script task like the comment suggests and calculate the value in the script task and emit it back to the pipeline.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.