4

I would like to send an email at the end of my build to inform success or failure. I'm using the email ext-plugin for Jenkins. I want the version number of the build to be the subject of the email.

The problem is that the version is something that changes in each build, so I need to somehow edit an environmental variable to hold the version number each time (I have a script that saves it into a file). This is the default configuration: enter image description here

I would like to have, for example, set the version number into a variable using windows batch script, like this:

rem # Loading version File number
set MY_BUILD_VERSION=<myVersionFile.txt
rem # Loading content
set MY_BUILD_CONTENT=<myContentFile.txt

And using it in the email plugin, like this: enter image description here

The problem is that the environmental variables aren't recognized outside the .bat file they are being run from, although I'm using the envInject plugin, the documentation says that what's run from a script won't be recognized outside of it.

Is there any way to load data into the email plugin from a file, or any other dynamic way to load my version number dynamically?

1

I found a way to set environmental variables globally from a file! I'm using envInject for loading variables from a file.

For example, lets say I have a file that is called "versionNumber.txt" and that it's created during the build process. Maybe I update it in every build, or any other way. then I create a prosFile that can be identified by the envInject, by running a batch script:

set /p VERSION_NUM=<versionNumber.txt

Let's assume my version number was 1.5.5.10. so now my VERSION_NUM equals that string. now VERSION_NUM has the value that I want. But it's local, it won't help me. so I rewrite it to a file in the following format (also, using a batch script):

echo VERSION_NUM=%VERSION_NUM% > C:\Temp\prosFile

now I have a file that contains the following content:

VERSION_NUM=1.5.5.10

And now I just need to load the file using the envInject plugin:

Loading environmental variables from "prosFile" file

And now I have an environmental variable named VERSION_NUM that can be used anywhere in Jenkins. But I wanted to make it the subject of my email, so I'll simply do (in windows, in linux it might be different):

Version Number as the email subject

That's it!

  • I don't think that's true, and I couldn't find that in the documentation. This is what's written there: <br/> The /P switch allows you to set a variable equal to a line of input entered by the user. The Prompt string is displayed before the user input is read. (from SET documentation) – Noam Ohana Dec 11 '17 at 14:25
  • Additionally, if I don't use the envInject it won't at all be available for any part of the build. In Jenkins I mean. – Noam Ohana Dec 11 '17 at 14:27
  • My bad, messed it up with setx. – Tensibai Dec 11 '17 at 14:30
0

You should be able to pass the current params as an argument to the batch file and then assign them to a variable used in the batch file. You can have this step as the post-build step before you call the email plugin.

  • 1
    Can you give an example? I'm not sure how it can be done – Noam Ohana Dec 11 '17 at 7:05
  • Use this to pass the variables to the batch file: – arv_b Dec 12 '17 at 6:03
0

Sorry.. the previous post was incomplete as I hit enter. Though you have solved the issue, I'm pointing out how you can capture env variables into the batch file.

Use the same method to read the contents of the file into a variable:

set /p VERSION_NUM=<versionNumber.txt

and when calling the batch file:

call catchmyvar.bat %VERSION_NUM%

and in the batch file 'catchmyvar.bat`, set the parameter to another variable that's local to your batch file:

SET VER_NUM=%1

This should ensure you can capture the env variables set outside the batch file into the current context

  • You can edit your previous post, no need to add a new one. Please combine the content of your posts into one of them and delete the other. Thx. – Dan Cornilescu Dec 12 '17 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.