How to use env variables set in .yml file in my script

625 Views Asked by At

I am new to use the drone secrets, need help in below. I have defined few secrets in the drone.io secrets settings. And now I want to access them in my test file.

Now in my repo there is a file called production.yml file in which I have defined the secrets values like below.

- name: build
    shm_size: 4096000000
    image: cypress/included:10.11.0
    environment:
     CYPRESS_CLIENT_KEY_PROD:
       from_secret: CYPRESS_SECRET_KEY_PROD
     CYPRESS_CLIENT_KEY_STAG:
       from_secret: CYPRESS_SECRET_KEY_STAG
    commands:
      - echo $CYPRESS_CLIENT_KEY_PROD
      - echo $CYPRESS_CLIENT_KEY_STAG

When I run the build I can see the echo prints with ****. Now I want these values to use in my test to authenticate a user. My question is what is the syntax to use these variables in my code? I tried multiple ways but nothing is working.

export function xClientKey() {
    process.env.CYPRESS_CLIENT_KEY_PROD
  };

I have created a function like above to access the secrets. But the syntax process.env.CYPRESS_CLIENT_KEY_PROD is not working here. I also tried Cypress.env.CYPRESS_CLIENT_KEY_PROD and this is also not working. It I am unable to access the secrets values. Can anyone let me know the correct syntax to access the secrets. Thanks

1

There are 1 best solutions below

1
On

In environment variables section of the docs

Any exported environment variables set on the command line or in your CI provider that start with either CYPRESS_ or cypress_ will automatically be parsed by Cypress.

So you will be able to access the variable in your test code using Cypress.env()

export function xClientKey() {
    Cypress.env('CLIENT_KEY_PROD')
  };

You can access it with config.env in node,

export function xClientKey() {
    config.env.CLIENT_KEY_PROD
  };