0

What is the equivalent configuration for Github Action similar function like the command below:

env-cmd -f .env cypress run --component

I tried with one env var imported into Github Action, but not working:

env:
  CYPRESS_PUBLIC_PATH: /public/

So, instead of defining the env variables one by one, I prefer to load the env file and directly consumed by the application/cypress.

I can't find any documentation that will load one env file in Github Action. Or perhaps there is a way to run the exact same code above in Github Action?

Update: I followed @Maddie.Squerciati's instruction to define env file on Cypress config, it works on my local, however github actions still doesn't recognize/use the env file.

Here is my github action config:

name: Cypress
uses: cypress-io/github-action@v5
with:
      config-file: cypress.config.js
      command: npm run cy:run-unit
      component: true
      record: false
      parallel: false
      browser: chrome
Darryl RN
  • 7,432
  • 4
  • 26
  • 46
  • You can install https://github.com/toddbluhm/env-cmd and use that. For the env vars, you can store them in a `.env` file yourself. – Azeem Jul 06 '23 at 05:16

1 Answers1

5

It's possible to read the .env inside cypress.config.js.

You would install dotenv as a dev dependency, then the .env file will be read by Cypress upon startup.

const { defineConfig } = require("cypress")

// read in .env file
const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed

module.exports = defineConfig({
  'e2e': {
    ...
  },
  env: {
    email: 'abc@123',   // example hard-coded var
    ...env,                        
  },
})

or if there are no hard-coded vars

const { defineConfig } = require("cypress")

const dotenv = require('dotenv')
const env = dotenv.config('./.env').parsed  // read in .env file

module.exports = defineConfig({
  'e2e': {
    ...
  },
  env,
})
  • how do you debug if the env is loaded to Cypress? I followed the above code, but my Cypress.env() doesn't have variables from my env file – Darryl RN Jul 06 '23 at 09:01
  • your answer works fine in my local, however Github Actions still doesn't pick up the config file even after I define `config-file` – Darryl RN Jul 07 '23 at 01:37
  • 1
    That's a shame. I would need to check the repo - code doesn't just stop working for no reason. – Maddie.Squerciati Aug 16 '23 at 11:32