Jenkins environment variables

Hello. I do not want to use the k6 run -e option to utilize system or Jenkins environment variables . what I have in mind would produce a terribly long command line input.

I am trying to utilize jenkins environment variables to populate a file to export them as variable from a common or constant.

for instance. a Jenkinsfile has a number of env variables set. DEVLINE is assigned a Jenkins pipeline choice parameter value

environment {
        // K6_PROMETHEUS_REMOTE_URL = "http://localhost:9090/api/v1/write"
        // K6_PROMETHEUS_REMOTE_OUTPUT = "output-prometheus-remote"
        DEVLINE = "${params.DEVLINE}"
parameters {
    choice(name: 'DEVLINE', choices: ['dev', 'qa', 'stage', 'prod'], description: 'environment endpoint to load')


in my common.js file I set a value to export a local variable
export let deploymentEnvironment = ${__ENV.DEVLINE} || 'dev';

when I attempt to import to a script file
import {authurlbase, thinktime1, thinktime2, deploymentEnvironment } from “./commons.js”;

then utilize deploymentEnvironment the script throws an error that deploymentEnvironment is undefined.

the use of DEVLINE only work when I pass it to the k6 runtime via -e DEVLINE=${DEVLINE}.

Is it possible to define export values from a Jenkins environment variables to then utilized as an import to k6 script files without having to use the -e option?


Hi @PlayStay !

From what I see, you did everything right except referring to an environment variable in the k6’s script. It’s __ENV.DEVLINE without any brackets.

So having two files:

// common.js
export let deploymentEnvironment = __ENV.DEVLINE || 'dev';


// script.js
import {deploymentEnvironment } from "./common.js";

export default function (data) {

and running by default produces:

 k6 -i 1 -u 1 run script.js
INFO[0000] dev                                           source=console

And let’s say the environment variable is defined:

 DEVLINE=yetanotherenviroment k6 -i 1 -u 1 run script.js
INFO[0000] yetanotherenviroment                          source=console

Let me know if that helps!

Hi @olegbespalov thanks for the response. While what you’ve presented is true (arg I can’t believe I missed the {} trap), I still am missing something in translation in practice. The freaking jenkins environment variable are still not propagating from pipeline to commons to script properly. I"ll keep tinkering but thanks for tip! :+1:t5:

the output to my post URL is still “undefined” {“remote_ip”:“”,“remote_port”:0,“url”:“undefined/api/method”,

Ooops. I fat fingered another param, masking the environment variable output… :face_with_head_bandage: I still don’t see jenkins environment variables being applied to k6 exports __ENV.DEVLINE in commons. but when I explicitly add the environment jenkins env.DEVLINE to the k6 -e ${env.DEVLINE} the string interpolation works as I expect…

how do I pass env.DEVLINE (jenkins env variable) to commons.js (__ENV.DEVLINE)?

Hi @PlayStay !

It seems to me that the main issue is that for some reason the environment variables aren’t set properly in Jenkins :thinking:

What output will be for the printenv ?

steps {
   sh 'printenv'

Is the environment variable DEVLINE filled properly?

Hey @olegbespalov I can’t be 100% sure the env variable is assigned properly but it is assigned and used in logic later in the pipeline. in the Jenkins pipeline its used in conditional if blocks to pull/prepare other environment variables in making build stage decisions. In short I’m using DEVLINE as a global env variable in downstream jenkins stages.

Hi @PlayStay !

That’s why I suggested executing printenv that way you will see what is in your environment. If that works there it should work for any other UNIX util (including k6) which just uses standard environment variables.

Hey @olegbespalov sorry for the misunderstanding but I do have echo statements (and printenv) debug statements throughout my jenkinsfile and the env variable is exposed to jenkins and I use those variable in other stages of my jenkinsfile. the missing link is that commons.js only utilizes the environment variable from jenkins when I use the -e option to a script. without the -e option where I rely on UNIX style access for __ENV.DEVLINE in commons.js the interpolation fails.

  1. Works
    def k6RunCommand = "k6 run /perf/$SCRIPT -e DEVLINE=${params.DEVLINE} -e CLIENT_ID=${CLIENT_ID} -e CLIENT_SECRET=${CLIENT_SECRET} -e SEED_DATA_KEY=${SEED_DATA_KEYS} --quiet"

because I have a million variable I want to parameterize through various methods I want to run

  1. No Worry
    def k6RunCommand = “k6 run /perf/$SCRIPT --quiet”

the __ENV.DEVLINE I expected to be assigned in commons.js via UNIX shell interpolation is not happening.

Hi @PlayStay !

the missing link is that commons.js only utilizes the environment variable from jenkins when I use the -e option to a script.

The thing is that ${params.DEVLINE} is not an environment variable, it’s a Jenkin’s parameter. Using k6’s -e option you manually tell k6, please run with the environment variable DEVLINE and the value that will be calculated in ${params.DEVLINE}.

But you don’t need to do that if you figure out how properly fill the environment variables from the Jenkins params. That’s why I suggested debugging the instructions:

environment {
   DEVLINE = "${params.DEVLINE}"
parameters {
    choice(name: 'DEVLINE', choices: ['dev', 'qa', 'stage', 'prod'], description: 'environment endpoint to load')

because if they work, the environment variable DEVLINE should appear with the proper value and k6 will pick up it without any -e options.

I agree with you @olegbespalov but I’m not seeing the expected behavior in practice. I’ll keep tinkering. At least I’m not (totally) crazy :face_with_head_bandage: