Hi!
To avoid having my .env.* files in my repo, I created some environmental variables on CircleCI with the values of my .env.* files. For example, ENV_PROD contains all my environment variables for the .env.prod file and ENV_TEST contain all my environment variables for the .env.test file.
In my config.yml file, I added this step to create my .env.* files when the build is run.-
- run:
name: 'Setting .env.* files'
command: |
echo "$ENV_PROD" > .env.prod
echo "$ENV_TEST" > .env.test
ls -la
cat .env.test
I’m noticing that my files are created when the step is executed, but I can’t see the content of my files (I suppose for security reasons).
The problem comes when the step related to my tests runs. It seems that my script doesn’t read the .env.* files created in the previous step. My code complains telling me that there’s no environment.
If I left my .env. files* in my repo, I don’t have any problems (but I don’t want to do that).
I started to suspect that the issue is probably in the way I’m reading the .env.* files. Some time ago I followed this tutorial to run my tests in multiple environments, but it doesn’t make sense to me since in my local, and on CircleCI if I left the .env.* files in my repo, it works as expected.
In my package.json, I have the following script to execute my tests (in this scenario, the .env file read is .env.test):
"test": "cross-env test_env=test npx playwright test tests/ui",
I have this GlobalSetup.ts file to set the .env.* file I want to use.-
import { FullConfig } from "@playwright/test";
import dotenv from "dotenv";
import ENV from "./ENV";
async function globalSetup(config: FullConfig) {
if (process.env.test_env) {
console.log('Environment (process.env.test_env): ' + process.env.test_env)
dotenv.config({
path: `.env.${process.env.test_env}`,
override: true,
});
/* const result = dotenv.config({
path: `.env.${process.env.test_env}`,
override: true,
});
console.log('Something should be printed here: ', result); */
}
}
export default globalSetup;
And in my playwright.config.ts I just have this.-
import { defineConfig, devices } from "@playwright/test";
import fs from "fs";
require("dotenv").config();
export default defineConfig({
globalSetup: "utils/GlobalSetup.ts",
// rest of the code...
The content of my .env.* files may look like this.-
ENVIRONMENT=TEST
###### URL ######
URL=https://google.com
###### Users ######
USERNAME=Username_Test_1
PASSWORD=Today123!
ENVIRONMENT=PROD
###### URL ######
URL=https://google.com
###### Users ######
USERNAME=Username_PROD_1
PASSWORD=Today123!
If I do an echo of the ENV_TEST environmental variable I set on CircleCI, I can see the content of this variable (although unformatted).
Thanks in advance for any help you can provide me!