Reading environment config files and passing it on the CLI

Hi team,

How can I read a specific environment config file and pass that environment as a CLI flag?
I found this but it doesn’t extend to having multiple environments:

Let’s say I have the following environment json files:

In my sample script.js, it should contain something like:
var r = http.get(http://${__ENV.HOST.BASE_URL}/); // <- This BASE_URL is inside the JSON files above and will change depending on the environment I chose

So, when I tried this on the CLI:
k6 run -e HOST=".env/preprod.json" script.js

I get an error ‘cannot read property’…

You can’t load a file directly with the --env property, but you can pass the filename and use open() from inside of your script to get the file contents and and something like JSON.parse() to parse them into usable options.

Now you may also use GitHub - szkiba/xk6-dotenv: A k6 extension that loads env vars from a .env file. if dotenv files is what you’re after. :tada: