0

I am trying to edit the given groovy template file given for the emailext plugin to display Jenkins environment variables. The code I am experimenting is:

pipeline {
agent {
    label 'main'
}
environment {
    test = "This is a test line."
}

stages {
    stage('Debug') {
        steps {
            sh 'printenv'
            sh "echo ${env.test}"
            bat 'echo %test%'
        }
    }
}
post {
    always {
        echo "Emailing.."
        emailext body: '''${SCRIPT, template="groovy-html-edited.template"}''',
        subject: currentBuild.currentResult + " : " + env.JOB_NAME,
        to: 'example@email.com'
    }
}

}

What I want is to be able to display the value of the pipeline environment variable within the email sent to the email address. Within the groovy template, I have tried using the following in an edited groovy template file:

  • ${env.test}
  • ${ENV.test}
  • ${ENV,var="test"}
  • ${env,var="test"}

Within the groovy-html-edited.template, the portion I've added for testing is:

  <!-- TEST SECTION-->
  <table class="section">
    <tr class="tr-title">
      <td class="td-title-main" colspan=2>
        TEST SECTION
      </td>
    </tr>
    <tr>
      <td>${ENV,var='test'}</td>
    </tr>
  </table>
  <br/>

What is the correct variable name to get a pipeline environment variable within the groovy template?

1 Answers1

0

Here is an example of getting env vars from a pipeline within script template:

<%
import  hudson.Util
import  hudson.Functions
import  hudson.model.Result;
import  hudson.model.Build

//get data from pipeline
def envOverrides = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getOverriddenEnvironment()

%>
<!-- TEST SECTION-->
<table class="section">
<tr class="tr-title">
    <td class="td-title-main" colspan=2>
    TEST SECTION
    </td>
</tr>
<tr>
    <td>${envOverrides["test"]}</td>
</tr>
</table>
<br/>
Barel elbaz
  • 426
  • 3
  • 8