12

I have Jenkins job that has execute shell part in which I have some variable BUILD that is dynamically populated.

After build execution, I want to pass this variable to email-ext plugin Default Content to able to show it's value.

I've tried couple of ways without a success:

  1. Passing this ${BUILD} value in Default Content is not recognized (Only Jenkins environment variables are visible in this context)
  2. Defined new Jenkins global environment variable and tried to overwrite its initial value in shell context which apparently is not possible

Any idea on how to do this?

Bakir Jusufbegovic
  • 2,806
  • 4
  • 32
  • 48
  • For me the accepted answer for this question solved the problem: https://stackoverflow.com/questions/49884904/access-variable-in-email-ext-template-using-jenkins-pipeline – bro Mar 03 '22 at 15:42

4 Answers4

7

Simple and easy:

In your "Execute Shell"

echo "test log" > /some/file/path/logFile.txt

Then in your "Editable Email Notification-Default Content"

${FILE,path="/some/file/path/logFile.txt"}

Build and you will receive a email with content "test log"


To see more email tokens, you can click the question mark beside "Content Token Reference" in "Editable Email Notification" section.

Ashutosh
  • 518
  • 7
  • 20
GrumpyMelon
  • 303
  • 1
  • 4
  • 10
  • your answer is great and the easiest one, however, when using it I found the email with BOM characters I used "text/Html; charset=utf-8" or but still shows in the mail the BOM character any solution for that – mohamed saeed Sep 29 '22 at 09:50
7

In my case, I'm not the administrator, then I can't install plugins. But can be done with a workaround.

In Content Token Reference help you can found an useful tool.

${PROPFILE,file="FILENAME",property="PROPERTYNAME"}

Expands to the value of a property in a property file. The filename is relative to the build workspace root.

Then save values in a property file inside Build > Execute Shell:

rm -f ${WORKSPACE}/env.properties
touch ${WORKSPACE}/env.properties 
store="/opt/current/store"

echo "store.folder=${store}" >> ${WORKSPACE}/env.properties

echo "${store}"

And read it from Post-build Actions > Editable Email Notification with:

${PROPFILE,file="env.properties",property="store.folder"}
equiman
  • 7,810
  • 2
  • 45
  • 47
1

Use EnvInject Plugin to read the variable from a file, after you write that file in the "shell part".


In general, environment variables never go from child process back to parent process, this is basic feature of both Windows and Unix operating system families. Child always gets a copy of parent's environment, and if it modifies it, it modifies it's own copy (which is then copied to any child process if it launches any, etc). But to get changes back, some other method must be used, such as child writing desired changes to a file, which is then parsed by parent, which can then edit it's own environment based on it.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • 5
    No need for that. I've found easier way to do this. Email-ext plugin supports pre email script where Groovy code can be written. So in the shell part I'm writting data into the file and with Groovy script, I'm reading that data and passing to the MimeMessage variable: String fileContents = new File("file.txt").text msg.setText(fileContents,"UTF-8"); – Bakir Jusufbegovic Apr 26 '13 at 09:41
  • 3
    Can you share the complete groovy script, to load the variable from the file – user2400564 May 23 '16 at 02:42
1

You can pass build parameters to email ext plugin by using:

${ENV,var="CAPITALIZED:VAR_NAME"}

In that way i see the variable value in the received mail.