Search code examples
jenkins-pipelinejenkins-pluginsjira-xray

Jenkinsfile environment variables not available with parameter variables


I am using a Jenkins plugin to upload test run results to Jira. Using this plugin I can send two JSON blobs of data for the import, but the variables in those JSON blobs can only be environment variables (not variables generally available in the Jenkinsfile).

When I run it is recognizing environment variables that come from the parameters block (this is a parameterized build), but it does not recognize any environment variables I set, either in an environment {} block in the pipeline or by nesting the build step in a withEnv() {} block.

As a sanity check, right before the step in question, I echo two environment variables, one from the parameters block and one from the environment block, and both spit out to the console as expected, but then, as consumed by the plugin, only the variables coming from the parameters block are read as variables, with the rest being left as string.

So is there some difference in how these environment variables are stored/managed behind the scenes that might play into this?

So, for example, here are the parameters and environment blocks:

parameters {
    choice(name: 'ENVIRONMENT', choices: ['dev', 'test', 'staging', 'prod'], description: 'Select the environment to run against.')
    choice(name: 'TESTS', choices: ['All', 'API', 'Web'], description: 'Select the tests to run.')
}

environment {
    PROJECT_KEY = "$jiraProjectKey"
    TEST_PLAN_KEY = "$testPlanKeys[$env.ENVIRONMENT]"
    PRODUCT_NAME = "$productName"
    TEAM_NAME = "$teamName"
}

When I used these environment variables in the JSON blobs to set the Summary field of a Test Execution in Jira with a line that looks like this:

...
"summary": "${ENVIRONMENT} - ${PRODUCT_NAME} - ${TESTS} Tests",
...

The resulting issue summary is:

dev - ${PRODUCT_NAME} - API Tests

So it will properly interpret the environment variables set by the parameters block, but not ones I set explicitly in the environment block.


Solution

  • In the JSON blobs that you are sending inline make sure that for multiline strings you are using """ to delimit those strings and not '''.

    Replace:

     ... importInfo: '''{...'''
    

    by:

    ...importInfo: """{..."""