skip to Main Content

I have variables in azure library that I want to pass to cypress so that I can have multiple stages (staging & production)

I have tried the following:

Using fixtures by having a file called environment.json that looks like so:

{
  WEB_APP_BASE_URL": "https://blight-town.com" // the "default" url for lost souls like I
}

In Azure I have a library that contains the variable "
WEB_APP_BASE_URL" with the value "https://not-my-website.com".

my yaml file looks so:

- task: Npm@1
  displayName: NPM run cy:run-smoke-tests
  inputs:
    workingDir: 'webapp'
    command: 'custom'
    customCommand: 'run cy:run-smoke-tests' //this just does runs cypress for a specific spec file
  env:
    WEB_APP_BASE_URL: $(WEB_APP_BASE_URL)

From this was hoping to overwrite the default base url and use the value from the library but alas. Any thoughts folks?

2

Answers


  1. Let’s assume two variable groups named smoke-tests-staging and smoke-tests-production, each containing a variable named WEB_APP_BASE_URL.

    You can reference and use the variable groups like this:

    stages:
      - stage: Staging
        jobs:
        - job: Deploy
          variables:
            # Reference staging variable group
            - group: smoke-tests-staging
          steps:
            # other steps here
            - task: Npm@1
              displayName: NPM run cy:run-smoke-tests
              inputs:
                workingDir: 'webapp'
                command: 'custom'
                customCommand: 'run cy:run-smoke-tests'
              env:
                # reference variable from smoke-tests-staging variable group
                WEB_APP_BASE_URL: $(WEB_APP_BASE_URL)
    
      - stage: Production
        dependsOn: Staging
        jobs:
        - job: Deploy
          variables:
            # Reference production variable group
            - group: smoke-tests-production
          steps:
            # other steps here
            - task: Npm@1
              displayName: NPM run cy:run-smoke-tests
              inputs:
                workingDir: 'webapp'
                command: 'custom'
                customCommand: 'run cy:run-smoke-tests'
              env:
                # reference variable from smoke-tests-production variable group
                WEB_APP_BASE_URL: $(WEB_APP_BASE_URL)
    
    Login or Signup to reply.
  2. According to the guidance outlined in Cypress documentation Cypress environment variables can be specified in the following:

    1. Cypress configuration file: cypress.config.js
    2. Cypress environment file: cypress.env.json
    3. CYPRESS_* environment variables
    4. Command-line arguments: --env <name>=<value>
    5. Defined as an env within a test

    You’re interested in the option #3, where CYPRESS_WEB_APP_BASE_URL overrides the value for WEB_APP_BASE_URL

    In Azure DevOps, variables defined in the pipeline or referenced from a library are represented at runtime as operating system Environment Variables on the build agent performing the work. (The only exception to this rule is if the variable is listed as a secret, then it must be explicitly specified by referencing the variable name in the env parameter to the task)

    To have Cypress pick up your pipeline variables as Cypress Environment Variables, you have the following options:

    Option 1: Explicitly Declare Cypress Variables in the Task

    I recommend this approach if you only have a few variables that need to be mapped, or if you want to adapt existing variables to match the names used in your tests. For each variable that you want to pass in, explicitly declare it with a CYPRESS_ prefix for the task process:

    - task: Npm@1
      displayName: NPM run cy:run-smoke-tests
      inputs:
        workingDir: 'webapp'
        command: 'custom'
        customCommand: 'run cy:run-smoke-tests'
      env:
        # explicitly declare variables as CYPRESS_* convention
        CYPRESS_WEB_APP_BASE_URL: $(WEB_APP_BASE_URL)
    

    Option 2: Define your variables in the Library using a CYPRESS_ naming convention

    To have Cypress automatically infer values from environment variables, define them in your pipeline or your library starting with the CYPRESS_ prefix.

    Your variable group has the following:

    Name Value
    CYPRESS_WEB_APP_BASE_URL https://not-my-website.com

    The task will automatically pick up the CYPRESS_ values.

    - task: Npm@1
      displayName: NPM run cy:run-smoke-tests
      inputs:
        workingDir: 'webapp'
        command: 'custom'
        customCommand: 'run cy:run-smoke-tests'
    

    Note:

    The values for variables are evaluated at runtime, so it is possible to have variables contain other variables. If you have existing variables that are used for other purposes, you can also map these variables to CYPRESS_ convention.

    Assume you have a variable Server_Url that you would like to be represented as CYPRESS_WEB_APP_BASE_URL:

    Name Value
    Server_URL https://not-my-website.com
    CYPRESS_WEB_APP_BASE_URL $(Server_URL)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search