<p>You normally would access environment variables using the following syntax:</p>

<pre><code>env.MY_ISSUE_ID
</code></pre>

<p>Typically you'll see the following in practice regarding envvars:</p>

<pre><code>pipeline {
  agent any
  environment {
    MY_ISSUE_ID = 'PROJECT-1234'
  }
  stages {
    stage('Build') {
      steps {
        echo env.MY_ISSUE_ID
      }
    }
  }
}
</code></pre>

<p>or </p>

<pre><code>node{
  stage('Build') {
    withEnv(["MY_ISSUE_ID=PROJECT-1234"]) {
      echo env.MY_ISSUE_ID
    }
  }
}
</code></pre>

CC BY-SA 4.0
2018-08-31T14:06:09.550Z
Thanks Casey, but I tried your answer, that not works with jira plugin.
Nilesh
CC BY-SA 4.0
2018-08-31T21:52:46.263Z
Can you show us your pipeline?
casey vega
CC BY-SA 4.0
2018-09-01T21:47:14.030Z
means XML config ?
Nilesh
CC BY-SA 4.0
2018-10-05T14:11:24.137Z
I added config file in the question.
Nilesh
CC BY-SA 4.0
2018-08-27T17:27:42.090Z
2018-08-27T17:27:42.090Z
casey vega
<p>I installed <code>JIRA Plugin</code> for the jenkins. It add some action in <code>Post-build Action</code> menu. One of the action is <strong>JIRA: Update relevant issues</strong>. </p>

<p>When I try to access Environment Variable, it gives error.</p>

<pre><code>WARNING: jira rest client get issue from jql search error. cause: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key '$MY_ISSUE_ID' for field 'issue' is invalid.]}]}
java.util.concurrent.ExecutionException: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key '$MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:289)
    at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:262)
    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:91)
    at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:69)
    at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.get(DelegatingPromise.java:107)
    at hudson.plugins.jira.JiraRestService.getIssuesFromJqlSearch(JiraRestService.java:177)
    at hudson.plugins.jira.JiraSession.getIssuesFromJqlSearch(JiraSession.java:136)
    at hudson.plugins.jira.selector.JqlIssueSelector.findIssueIds(JqlIssueSelector.java:49)
    at hudson.plugins.jira.Updater.perform(Updater.java:85)
    at hudson.plugins.jira.JiraIssueUpdater.perform(JiraIssueUpdater.java:64)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
    at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.model.Build$BuildExecution.post2(Build.java:186)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1749)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:421)
Caused by: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key '$MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:176)
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:170)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:67)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:11)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:268)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:158)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:268)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:158)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$1$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:46)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:197)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient.runInContext(SettableFuturePromiseHttpPromiseAsyncClient.java:90)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable.run(SettableFuturePromiseHttpPromiseAsyncClient.java:192)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
</code></pre>

<p><code>$MY_ISSUE_ID</code> is valid environment variable, and I am able to access it on other section of the build.</p>

<p>I tried <code>issue=${XXXX}</code>, <code>issue=XXXX</code>, <code>issue="$XXXX"</code> etc... but all gives error.</p>

<p>After <a href="https://devops.stackexchange.com/a/4855/9351">answer</a> I tried <code>env. MY_ISSUE_ID</code> but it give same error.</p>

<pre><code>WARNING: jira rest client get issue from jql search error. cause: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key 'env. MY_ISSUE_ID' for field 'issue' is invalid.]}]}
java.util.concurrent.ExecutionException: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key 'env. MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:289)
    at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:262)
    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:91)
    at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:69)
    at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.get(DelegatingPromise.java:113)
    at hudson.plugins.jira.JiraRestService.getIssuesFromJqlSearch(JiraRestService.java:177)
    at hudson.plugins.jira.JiraSession.getIssuesFromJqlSearch(JiraSession.java:132)
    at hudson.plugins.jira.selector.JqlIssueSelector.findIssueIds(JqlIssueSelector.java:46)
    at hudson.plugins.jira.Updater.perform(Updater.java:89)
    at hudson.plugins.jira.JiraIssueUpdater.perform(JiraIssueUpdater.java:64)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
    at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.model.Build$BuildExecution.post2(Build.java:186)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1749)
    at com.tikal.jenkins.plugins.multijob.MultiJobBuild.run(MultiJobBuild.java:76)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:421)
Caused by: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key 'env.MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:176)
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:170)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:67)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:11)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:295)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:185)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:295)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:185)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$1$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:46)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:197)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient.runInContext(SettableFuturePromiseHttpPromiseAsyncClient.java:90)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable.run(SettableFuturePromiseHttpPromiseAsyncClient.java:192)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
</code></pre>

<p>What is the way to pass <code>environment variable</code> in <strong>jql</strong> for <code>post build action</code>.</p>

<p>When I gave <code>issue=MYPROJECT-1234</code>, it works fine. But, I want to set <code>MYPROJECT-1234</code> as environment variable.</p>

<p>Job <strong>config.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;com.tikal.jenkins.plugins.multijob.MultiJobProject&gt;
  &lt;actions/&gt;
  &lt;description&gt;&amp;lt;!-- Managed by Jenkins Job Builder --&amp;gt;&lt;/description&gt;
  &lt;keepDependencies&gt;false&lt;/keepDependencies&gt;
  &lt;disabled&gt;false&lt;/disabled&gt;
  &lt;blockBuildWhenDownstreamBuilding&gt;false&lt;/blockBuildWhenDownstreamBuilding&gt;
  &lt;blockBuildWhenUpstreamBuilding&gt;false&lt;/blockBuildWhenUpstreamBuilding&gt;
  &lt;concurrentBuild&gt;false&lt;/concurrentBuild&gt;
  &lt;assignedNode&gt;linux&lt;/assignedNode&gt;
  &lt;canRoam&gt;false&lt;/canRoam&gt;
  &lt;properties&gt;
    &lt;com.coravy.hudson.plugins.github.GithubProjectProperty plugin="github"&gt;
      &lt;projectUrl&gt;https://myuser@mygithub.mydomain.com/myteam/myproject&lt;/projectUrl&gt;
      &lt;displayName/&gt;
    &lt;/com.coravy.hudson.plugins.github.GithubProjectProperty&gt;
  &lt;/properties&gt;
  &lt;scm class="hudson.plugins.git.GitSCM"&gt;
    &lt;configVersion&gt;2&lt;/configVersion&gt;
    &lt;userRemoteConfigs&gt;
      &lt;hudson.plugins.git.UserRemoteConfig&gt;
        &lt;name&gt;origin&lt;/name&gt;
        &lt;refspec&gt;+refs/pull/*:refs/remotes/origin/pr/*&lt;/refspec&gt;
        &lt;url&gt;https://myuser@mygithub.mydomain.com/myteam/myproject.git&lt;/url&gt;
        &lt;credentialsId&gt;github_username_with_token&lt;/credentialsId&gt;
      &lt;/hudson.plugins.git.UserRemoteConfig&gt;
    &lt;/userRemoteConfigs&gt;
    &lt;branches&gt;
      &lt;hudson.plugins.git.BranchSpec&gt;
        &lt;name&gt;${sha1}&lt;/name&gt;
      &lt;/hudson.plugins.git.BranchSpec&gt;
    &lt;/branches&gt;
    &lt;disableSubmodules&gt;false&lt;/disableSubmodules&gt;
    &lt;recursiveSubmodules&gt;false&lt;/recursiveSubmodules&gt;
    &lt;doGenerateSubmoduleConfigurations&gt;false&lt;/doGenerateSubmoduleConfigurations&gt;
    &lt;remotePoll&gt;false&lt;/remotePoll&gt;
    &lt;gitTool&gt;Default&lt;/gitTool&gt;
    &lt;submoduleCfg class="list"/&gt;
    &lt;reference/&gt;
    &lt;gitConfigName/&gt;
    &lt;gitConfigEmail/&gt;
    &lt;extensions/&gt;
  &lt;/scm&gt;
  &lt;triggers class="vector"&gt;
    &lt;org.jenkinsci.plugins.ghprb.GhprbTrigger&gt;
      &lt;spec&gt;H/5 * * * *&lt;/spec&gt;
      &lt;adminlist/&gt;
      &lt;allowMembersOfWhitelistedOrgsAsAdmin&gt;false&lt;/allowMembersOfWhitelistedOrgsAsAdmin&gt;
      &lt;whitelist/&gt;
      &lt;orgslist/&gt;
      &lt;whiteListLabels/&gt;
      &lt;blackListLabels/&gt;
      &lt;cron&gt;H/5 * * * *&lt;/cron&gt;
      &lt;triggerPhrase&gt;^\s*check\s+everything\s*$&lt;/triggerPhrase&gt;
      &lt;skipBuildPhrase/&gt;
      &lt;onlyTriggerPhrase&gt;false&lt;/onlyTriggerPhrase&gt;
      &lt;useGitHubHooks&gt;true&lt;/useGitHubHooks&gt;
      &lt;permitAll&gt;true&lt;/permitAll&gt;
      &lt;autoCloseFailedPullRequests&gt;false&lt;/autoCloseFailedPullRequests&gt;
      &lt;whiteListTargetBranches&gt;
        &lt;org.jenkinsci.plugins.ghprb.GhprbBranch&gt;
          &lt;branch&gt;staging&lt;/branch&gt;
        &lt;/org.jenkinsci.plugins.ghprb.GhprbBranch&gt;
      &lt;/whiteListTargetBranches&gt;
      &lt;extensions&gt;
        &lt;org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus&gt;
          &lt;commitStatusContext&gt;pull request validation&lt;/commitStatusContext&gt;
          &lt;triggeredStatus&gt;starting pull request validation...&lt;/triggeredStatus&gt;
          &lt;startedStatus&gt;running pull request validation...&lt;/startedStatus&gt;
          &lt;completedStatus&gt;
            &lt;org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildResultMessage&gt;
              &lt;message&gt;All is well.&lt;/message&gt;
              &lt;result&gt;SUCCESS&lt;/result&gt;
            &lt;/org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildResultMessage&gt;
            &lt;org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildResultMessage&gt;
              &lt;message&gt;Something went wrong. Investigate!&lt;/message&gt;
              &lt;result&gt;FAILURE&lt;/result&gt;
            &lt;/org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildResultMessage&gt;
          &lt;/completedStatus&gt;
        &lt;/org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus&gt;
      &lt;/extensions&gt;
    &lt;/org.jenkinsci.plugins.ghprb.GhprbTrigger&gt;
  &lt;/triggers&gt;
  &lt;builders&gt;
    &lt;com.tikal.jenkins.plugins.multijob.MultiJobBuilder&gt;
      &lt;phaseName&gt;JIRA ID Validation&lt;/phaseName&gt;
      &lt;continuationCondition&gt;SUCCESSFUL&lt;/continuationCondition&gt;
      &lt;executionType&gt;PARALLEL&lt;/executionType&gt;
      &lt;phaseJobs&gt;
        &lt;com.tikal.jenkins.plugins.multijob.PhaseJobsConfig&gt;
          &lt;jobName&gt;cicd-myproject-prtest-jiraid&lt;/jobName&gt;
          &lt;currParams&gt;true&lt;/currParams&gt;
          &lt;configs/&gt;
          &lt;abortAllJob&gt;false&lt;/abortAllJob&gt;
          &lt;enableRetryStrategy&gt;false&lt;/enableRetryStrategy&gt;
        &lt;/com.tikal.jenkins.plugins.multijob.PhaseJobsConfig&gt;
      &lt;/phaseJobs&gt;
    &lt;/com.tikal.jenkins.plugins.multijob.MultiJobBuilder&gt;
    &lt;com.tikal.jenkins.plugins.multijob.MultiJobBuilder&gt;
      &lt;phaseName&gt;Tox Validation&lt;/phaseName&gt;
      &lt;continuationCondition&gt;SUCCESSFUL&lt;/continuationCondition&gt;
      &lt;executionType&gt;PARALLEL&lt;/executionType&gt;
      &lt;phaseJobs&gt;
        &lt;com.tikal.jenkins.plugins.multijob.PhaseJobsConfig&gt;
          &lt;jobName&gt;cicd-myproject-prtest-unittest&lt;/jobName&gt;
          &lt;currParams&gt;true&lt;/currParams&gt;
          &lt;configs/&gt;
          &lt;abortAllJob&gt;false&lt;/abortAllJob&gt;
          &lt;enableRetryStrategy&gt;false&lt;/enableRetryStrategy&gt;
        &lt;/com.tikal.jenkins.plugins.multijob.PhaseJobsConfig&gt;
      &lt;/phaseJobs&gt;
    &lt;/com.tikal.jenkins.plugins.multijob.MultiJobBuilder&gt;
    &lt;hudson.tasks.Shell&gt;
      &lt;command&gt;#!/bin/bash
echo "MY_ISSUE_ID=`echo ${ghprbPullLongDescription} | grep -e 'MY-[0-9]\+' -o`" &amp;gt; MY_issue.property
&lt;/command&gt;
    &lt;/hudson.tasks.Shell&gt;
    &lt;EnvInjectBuilder&gt;
      &lt;info&gt;
        &lt;propertiesFilePath&gt;$WORKSPACE/MY_issue.property&lt;/propertiesFilePath&gt;
      &lt;/info&gt;
    &lt;/EnvInjectBuilder&gt;
    &lt;hudson.plugins.jira.JiraIssueUpdateBuilder plugin="jira@3.0.0"&gt;
      &lt;jqlSearch&gt;issue=${MY_ISSUE_ID}&lt;/jqlSearch&gt;
      &lt;workflowActionName&gt;Ready for Review&lt;/workflowActionName&gt;
      &lt;comment&gt;[Unit Testing|${BUILD_URL}] Done&lt;/comment&gt;
    &lt;/hudson.plugins.jira.JiraIssueUpdateBuilder&gt;
    &lt;hudson.tasks.Shell&gt;
      &lt;command&gt;#!/bin/bash
curl -X POST --data-urlencode 'payload={"text": "'"${ghprbPullLink}"' is ready for review"}' &lt;slackurl&gt;
&lt;/command&gt;
    &lt;/hudson.tasks.Shell&gt;
  &lt;/builders&gt;
  &lt;publishers&gt;
    &lt;hudson.plugins.jira.JiraIssueUpdater plugin="jira@3.0.0"&gt;
      &lt;issueSelector class="hudson.plugins.jira.selector.JqlIssueSelector"&gt;
        &lt;jql&gt;issue=${MY_ISSUE_ID}&lt;/jql&gt;
      &lt;/issueSelector&gt;
      &lt;labels/&gt;
    &lt;/hudson.plugins.jira.JiraIssueUpdater&gt;
  &lt;/publishers&gt;
  &lt;buildWrappers&gt;
    &lt;hudson.plugins.ansicolor.AnsiColorBuildWrapper&gt;
      &lt;colorMapName&gt;xterm&lt;/colorMapName&gt;
    &lt;/hudson.plugins.ansicolor.AnsiColorBuildWrapper&gt;
  &lt;/buildWrappers&gt;
&lt;/com.tikal.jenkins.plugins.multijob.MultiJobProject&gt;
</code></pre>

CC BY-SA 4.0
2018-09-04T20:29:59.897Z
hi! please attach your Jenkinsfile so we know exactly what you were running to access and use the env variable.
AnneTheAgile
CC BY-SA 4.0
2018-10-05T14:11:01.717Z
@AnneTheAgile added config file.
Nilesh
<jenkins><jenkins-plugins><jira>
How to access environment variable in post-build access for jira plugin
Nilesh
# How to access environment variable in post-build access for jira plugin

(Asked by: username_0 on 2018-08-24)

I installed JIRA Plugin for the jenkins. It add some action in Post-build Action menu. One of the action is JIRA: Update relevant issues. 
When I try to access Environment Variable, it gives error.
WARNING: jira rest client get issue from jql search error. cause: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key '$MY_ISSUE_ID' for field 'issue' is invalid.]}]}
java.util.concurrent.ExecutionException: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key '$MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:289)
    at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:262)
    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:91)
    at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:69)
    at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.get(DelegatingPromise.java:107)
    at hudson.plugins.jira.JiraRestService.getIssuesFromJqlSearch(JiraRestService.java:177)
    at hudson.plugins.jira.JiraSession.getIssuesFromJqlSearch(JiraSession.java:136)
    at hudson.plugins.jira.selector.JqlIssueSelector.findIssueIds(JqlIssueSelector.java:49)
    at hudson.plugins.jira.Updater.perform(Updater.java:85)
    at hudson.plugins.jira.JiraIssueUpdater.perform(JiraIssueUpdater.java:64)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
    at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.model.Build$BuildExecution.post2(Build.java:186)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1749)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:421)
Caused by: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key '$MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:176)
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:170)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:67)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:11)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:268)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:158)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:268)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:158)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$1$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:46)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:197)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient.runInContext(SettableFuturePromiseHttpPromiseAsyncClient.java:90)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable.run(SettableFuturePromiseHttpPromiseAsyncClient.java:192)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

$MY_ISSUE_ID is valid environment variable, and I am able to access it on other section of the build.
I tried issue=${XXXX}, issue=XXXX, issue="$XXXX" etc... but all gives error.
After answer I tried env. MY_ISSUE_ID but it give same error.
WARNING: jira rest client get issue from jql search error. cause: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key 'env. MY_ISSUE_ID' for field 'issue' is invalid.]}]}
java.util.concurrent.ExecutionException: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key 'env. MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:289)
    at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:262)
    at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:91)
    at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:69)
    at com.atlassian.jira.rest.client.internal.async.DelegatingPromise.get(DelegatingPromise.java:113)
    at hudson.plugins.jira.JiraRestService.getIssuesFromJqlSearch(JiraRestService.java:177)
    at hudson.plugins.jira.JiraSession.getIssuesFromJqlSearch(JiraSession.java:132)
    at hudson.plugins.jira.selector.JqlIssueSelector.findIssueIds(JqlIssueSelector.java:46)
    at hudson.plugins.jira.Updater.perform(Updater.java:89)
    at hudson.plugins.jira.JiraIssueUpdater.perform(JiraIssueUpdater.java:64)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
    at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
    at hudson.model.Build$BuildExecution.post2(Build.java:186)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
    at hudson.model.Run.execute(Run.java:1749)
    at com.tikal.jenkins.plugins.multijob.MultiJobBuild.run(MultiJobBuild.java:76)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:421)
Caused by: RestClientException{statusCode=Optional.of(400), errorCollections=[ErrorCollection{status=400, errors={}, errorMessages=[The issue key 'env.MY_ISSUE_ID' for field 'issue' is invalid.]}]}
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:176)
    at com.atlassian.jira.rest.client.internal.async.AbstractAsynchronousRestClient$2.apply(AbstractAsynchronousRestClient.java:170)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:67)
    at com.atlassian.httpclient.api.ResponsePromiseMapFunction.apply(ResponsePromiseMapFunction.java:11)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:295)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:185)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.util.concurrent.Promises$Of$3.apply(Promises.java:295)
    at com.atlassian.util.concurrent.Promises$2.onSuccess(Promises.java:185)
    at com.google.common.util.concurrent.Futures$6.run(Futures.java:975)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:253)
    at com.google.common.util.concurrent.ExecutionList$RunnableExecutorPair.execute(ExecutionList.java:149)
    at com.google.common.util.concurrent.ExecutionList.execute(ExecutionList.java:134)
    at com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:170)
    at com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:53)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$1$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:46)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable$1.run(SettableFuturePromiseHttpPromiseAsyncClient.java:197)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient.runInContext(SettableFuturePromiseHttpPromiseAsyncClient.java:90)
    at com.atlassian.httpclient.apache.httpcomponents.SettableFuturePromiseHttpPromiseAsyncClient$ThreadLocalDelegateRunnable.run(SettableFuturePromiseHttpPromiseAsyncClient.java:192)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

What is the way to pass environment variable in jql for post build action.
When I gave issue=MYPROJECT-1234, it works fine. But, I want to set MYPROJECT-1234 as environment variable.
Job config.xml

  
  <!-- Managed by Jenkins Job Builder -->
  false
  false
  false
  false
  false
  linux
  false
  
    
      https://myuser@mygithub.mydomain.com/myteam/myproject
      
    
  
  
    2
    
      
        origin
        +refs/pull/*:refs/remotes/origin/pr/*
        https://myuser@mygithub.mydomain.com/myteam/myproject.git
        github_username_with_token
      
    
    
      
        ${sha1}
      
    
    false
    false
    false
    false
    Default
    
    
    
    
    
  
  
    
      H/5 * * * *
      
      false
      
      
      
      
      H/5 * * * *
      ^\s*check\s+everything\s*$
      
      false
      true
      true
      false
      
        
          staging
        
      
      
        
          pull request validation
          starting pull request validation...
          running pull request validation...
          
            
              All is well.
              SUCCESS
            
            
              Something went wrong. Investigate!
              FAILURE
            
          
        
      
    
  
  
    
      JIRA ID Validation
      SUCCESSFUL
      PARALLEL
      
        
          cicd-myproject-prtest-jiraid
          true
          
          false
          false
        
      
    
    
      Tox Validation
      SUCCESSFUL
      PARALLEL
      
        
          cicd-myproject-prtest-unittest
          true
          
          false
          false
        
      
    
    
      #!/bin/bash
echo "MY_ISSUE_ID=`echo ${ghprbPullLongDescription} | grep -e 'MY-[0-9]\+' -o`" > MY_issue.property

    
    
      
        $WORKSPACE/MY_issue.property
      
    
    
      issue=${MY_ISSUE_ID}
      Ready for Review
      [Unit Testing|${BUILD_URL}] Done
    
    
      #!/bin/bash
curl -X POST --data-urlencode 'payload={"text": "'"${ghprbPullLink}"' is ready for review"}' 

    
  
  
    
      
        issue=${MY_ISSUE_ID}
      
      
    
  
  
    
      xterm

**Tags:** jenkins, jenkins-plugins, jira

> **Comments:**
> **username_1**: hi! please attach your Jenkinsfile so we know exactly what you were running to access and use the env variable.  
> **username_0**: @username_1 added config file.  

---

## 1 answers

### Answer by username_2 on 2018-08-27. Score: 1

You normally would access environment variables using the following syntax:
env.MY_ISSUE_ID

Typically you'll see the following in practice regarding envvars:
pipeline {
  agent any
  environment {
    MY_ISSUE_ID = 'PROJECT-1234'
  }
  stages {
    stage('Build') {
      steps {
        echo env.MY_ISSUE_ID
      }
    }
  }
}

or 
node{
  stage('Build') {
    withEnv(["MY_ISSUE_ID=PROJECT-1234"]) {
      echo env.MY_ISSUE_ID
    }
  }
}

> **Comments:**
> **username_0**: Thanks Casey, but I tried your answer, that not works with jira plugin.  
> **username_2**: Can you show us your pipeline?  
> **username_0**: means XML config ?  
> **username_0**: I added config file in the question.  

---

