Skip to content

Add support for --env-file option #5662

@jkbcz

Description

@jkbcz

Feature Description

--env-file command line option would allow specifying a file containing environment files to be used when running a load test. It could be used wherever --env can be used.

.env files have become a de facto standard for specifying environment variables in the Node ecosystem.
If a load test script requires more than a few env vars, then setting them --env becomes quite cumbersome.

The env-file option would be especially useful when running a test against different environments. For example when I'm working on my load test I may want to run it against my local instance, so I could use --env-file=.env.local. Then after I'm happy with my test I could run it against my production environment with --env-file=.env.prod.
Of course this workflow can already be achieved today by loading the .env file into my OS env vars, and then running the k6 command. This however usually doesn't work cross-platform or requires installing additional tools. Also it doesn't really work when uploading my test with k6 cloud. Supporting it in the k6 command could streamline this process.

Suggested Solution (optional)

Implementing this feature seems to be pretty straight forward.
It would require just adding the flag in the https://github.com/grafana/k6/blob/master/internal/cmd/runtime_options.go

And then parsing the file and adding it to lib.RuntimeOptions.Env
The parsing could either be done with an existing library (e.g. https://github.com/joho/godotenv), or with a custom implementation

I myself would be happy to implement this feature

Already existing or connected issues / PRs (optional)

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions