Darryl RN
Darryl RN

Reputation: 8228

Cypress Github Actions load env file like env-cmd

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

Upvotes: 0

Views: 417

Answers (1)

Maddie.Squerciati
Maddie.Squerciati

Reputation: 156

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,
})

Upvotes: 5

Related Questions