Tatevik
Tatevik

Reputation: 385

Pass Azure DevOps pipeline Variable to package.json file

I want to connect azure pipeline to my app and run my tests in headless mode by using cypress, and I need to pass Azure DevOps variable (which is my cypress record key) to my package.json. How can I pass variable right way?

I try to create Azure variable, pass it in .yml and receive in package.json

here is my .yml script part:

    npm run test:e2e:headless:record $(RECORD_KEY)
  displayName: 'Run Cypress tests headless'

and here is my package.json script part:

"test:e2e:headless:record": "vue-cli-service test:e2e --headless --record --key RECORD_KEY"

my azure job failed in this line, which means my package.json don't receive RECORD_KEY variable: test:e2e:headless:record: vue-cli-service test:e2e --headless --record --key RECORD_KEY

Upvotes: 0

Views: 3475

Answers (1)

Mengdi Liang
Mengdi Liang

Reputation: 18978

Why not use Replace Token task to pass the variable value to Package.json file?

In your package.json, write the variable with the format @@RECORD_KEY@@:

"test:e2e:headless:record": "vue-cli-service test:e2e --headless --record --key @@RECORD_KEY@@

This is the screenshot of my package.json file:

enter image description here

Then, in replace token task, configure the file path and file which will be replaced token:

enter image description here

enter image description here

Note, not forget to configure the value in Variable tab if the variable is not the pre-defined variable.

enter image description here

Then, execute the pipeline, in your local log, you will see that the value has been passed successfully:

enter image description here

Upvotes: 3

Related Questions