Get k6 input parameters in scripts


Is there a way to the access CLI parameters and configuration within the scripts?

For example, I’d like to change some headers depending on the --out option passed.

Another use case would be to adjust a script based on the JSON scenario content.

Unfortunately not in your case, sorry :disappointed: The consolidated (CLI flags + environment variables + script options + JSON config) configuration is propagated back to the script options, but --out is somewhat… special (1, 2, 3, 4)… and you can’t access it from there yet :disappointed:

Another use case would be to adjust a script based on the JSON scenario content.

Not sure if I understand what you’re asking here, do you mean the new scenarios in k6 v0.27.0? If so, you can specify different environment variables on a per-scenario basis and use them to adjust the script.

@ned - Thanks for the reply.
I will look at what options has to offered, and it would be nice to have a richer k6 API in the future. That would help to build solutions around k6 (that is already a pretty cool solution and well documented product).
Regarding the scenario, yes I am referring the to new ones :slight_smile:

As a workaround, I can suggest setting the output data from an environment variable, and using the same environment variable in the k6 script.

export MY_ENV_VAR="whatever"
k6 run --out influxdb="http://localhost:8086/$MY_ENV_VAR" script.js

and then using __ENV.MY_ENV_VAR in script.js. Just don’t forget to specify --include-system-env-vars if you’re using k6 cloud, since by default it’s only enabled for k6 run, to prevent unintentional data leaks.

I still don’t understand your exact use case, but considering you can use these environment variables both in your script and to affect the other exported options, and you can combine that with extra per-scenario environment variables, I think there’s not a lot that can’t be done with this much flexibility.

I am already using environment variables, and I was looking for a “smarter” solution. But it seems that I will stick a bit longer with __ENV :wink: