Value for a variable defined at Pipeline level is showing blank at script level

43 views Asked by At

I am new to Playwright and Azure.

In my AZDO pipeline yaml file, I have a variable "environment_type" set to value "test" defined while we run the pipeline.

And it prints the value correctly as shown here:

          - script: |
              npm run test:$(environment_type)
            displayName: "Run Playwright tests"

I have a further requirement where I want to run my Playwright tests in various other environments e.g. acc, dev, preprod etc

For that I already have all the necessary scripts inside package.json file.

I am writing the pipeline in a following way (not sure whats wrong in this code, because it is always printing the whole value as $(ENV_NAME_TEST5) in the password field

- script: |
    npm run test:$(environment_type)
  displayName: "Run Playwright tests"
  env:
    USERNAME: ‘user-name’
    ${{ if eq(variables['environment_type'], 'test') }}:
      PASSWORD: '$(ENV_NAME_TEST)'
    ${{ elseif eq(variables['environment_type'], 'test2') }}:
      PASSWORD: '$(ENV_NAME_TEST2)'
    ${{ elseif eq(variables['environment_type'], 'test3') }}:
      PASSWORD: '$(ENV_NAME_TEST3)'
    ${{ elseif eq(variables['environment_type'], 'test4') }}:
      PASSWORD: '$(ENV_NAME_TEST4)'
    ${{ else }}:
      PASSWORD: '$(ENV_NAME_TEST5)'
     

After enabling the debug option in pipeline, I came to know that its not resolving these variables in the conditions.

I have tried to use various variables combinations syntaxes like compile time, run time. And also tried to place the variable block in various stages in the yaml file.

Could you please support me here? Thanks in advance!

1

There are 1 answers

0
Miao Tian-MSFT On

I came to know that it's not resolving these variables in the conditions.

You can use the Runtime parameters instead of the variables.

Here is my test sample yaml:

trigger:
- none

parameters:
- name: environment_type
  type: string
  default: 'test'

pool:
  vmImage: ubuntu-latest

steps:

- script: |
    echo "${{ parameters.environment_type }}"
    echo  "${PASSWORD}"
  displayName: "Run Playwright tests"
  env:
    ${{ if eq(parameters.environment_type, 'test') }}:
      PASSWORD: '$(ENV_NAME_TEST)'
    ${{ elseif eq(parameters.environment_type, 'test2') }}:
      PASSWORD: '$(ENV_NAME_TEST2)'
    ${{ elseif eq(parameters.environment_type, 'test3') }}:
      PASSWORD: '$(ENV_NAME_TEST3)'
    ${{ elseif eq(parameters.environment_type, 'test4') }}:
      PASSWORD: '$(ENV_NAME_TEST4)'
    ${{ else }}:
      PASSWORD: '$(ENV_NAME_TEST5)'

I set the ENV_NAME_TEST variables for the yaml pipeline in the UI.

enter image description here

When we run the pipeline, we can change the environment_type manually before you run the pipeline and the Password will be changed based on the parameter.

enter image description here

Test result:

enter image description here