skip to Main Content

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


  1. 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:

    echo "##vso[task.setvariable variable=kaasServiceConnection]SVC-DEV-CONN"
    

    The behaviour you described is consistent of using a non-defined variable in a macro syntax.

    Login or Signup to reply.
  2. 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:
     - name: environment
       type: string
       values:
        - dev
        - prod
    variables:
    - ${{ if eq(parameters.environment, 'dev') }}:
        - name: kaasServiceConnection
          value: "SVC-DEV-CONN"
    - ${{ if eq(parameters.environment, 'qa') }}:
        - name: kaasServiceConnection
          value: "SVC-QA-CONN"
    
    stages:
      - stage: yourStage
        steps:
          - task: HelmDeploy@0
            displayName: 'Helm upgrade'
            inputs:
              connectionType: 'Kubernetes Service Connection'
              kubernetesServiceEndpoint: $(kaasServiceConnection)
    
    

    Please adjust the pipeline accordingly.

    Login or Signup to reply.
  3. 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:

    variables:
    - name: kaasServiceConnection
      value: "SVC-${{ parameters.environment }}-CONN"
    

    Going further, though, you should consider using a multistage pipeline, not passing an environment as a parameter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search