I am working with Azure Pipelines and facing an issue with setting a pipeline variable using the ##vso[task.setvariable] syntax. My intention is to set the variable kaasServiceConnection with different values based on the environment parameter in the pipeline. However, when I reference this variable later in the pipeline, it doesn’t seem to be taking the assigned value; instead, it seems to be resolving as $(kaasServiceConnection).
Here is the relevant snippet from my pipeline:
- script: |
if [ "$(parameters.environment)" == "dev" ]; then
echo "##vso[task.setvariable variable=kaasServiceConnection;]SVC-DEV-CONN"
elif [ "$(parameters.environment)" == "qa" ]; then
echo "##vso[task.setvariable variable=kaasServiceConnection;]SVC-QA-Conn"
fi
displayName: 'getting Service Connection'
- task: HelmDeploy@0
displayName: 'Helm upgrade'
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceEndpoint: '$(kaasServiceConnection)'
...
3
Answers
There is an extra semicolon
;
in the variable definition string, I bet it’s causing a misleading processing of the instruction.Try to change the command to this:
The behaviour you described is consistent of using a non-defined variable in a macro syntax.
I think this would suit your case and be even a bit more native to azure DevOps pipelines.
Expressions are accepted in the variables section too, refer conditionally-assign-a-variable
Parameters aren’t available during runtime expressions (i.e.
$[]
) or within macro expressions ($()
). Neither condition is evaluating to true, so the variable isn’t set. The correct syntax to use for referencing parameter is compilation-time syntax,${{ parameters.environment }}
.The example provided above is closer to correct, although you could also do this based on the naming pattern you provided:
Going further, though, you should consider using a multistage pipeline, not passing an environment as a parameter.