3

How can I pass a parameter in Jenkins from a build step to post-build step? I don't want to do it through a file, as it's just a simple string (version number) that I want to pass.

In post-build I use Groovy postbuild plugin to add this string to the summary of the build.


Update: I was in despair )) and decided to go via file route. Couldn't figure out how to get WORKSPACE env var in Groovy post-build step to get file from there.

Final update: So I ended up parsing build log, see my own answer below.

Ivan
  • 9,089
  • 4
  • 61
  • 74

2 Answers2

2

I ended up printing information to the build log in the build step, and then parsing it in the post-build Groovy plugin.

A bit ugly workaround, I know, sometimes I miss TeamCity with it's simplicity...

Regex pattern below is obviously specific to my own build...

def matcher = manager.getLogMatcher("^Build Found:  [\\w\\s-]+_(\\d+\\.\\d+\\.\\d+)_\\d{8}(\\.\\d+)?\$")
if (matcher?.matches()) {
    def Version = matcher.group(1)
    manager.addShortText(Version, "grey", "white", "0px", "white")
    manager.createSummary("fingerprint.gif").appendText("Version: " + Version, false)
} else {
    manager.listener.logger.println("ERROR: Version number is not found in the build log")
    manager.buildFailure()
}
Ivan
  • 9,089
  • 4
  • 61
  • 74
  • Thanks for sharing your solution. The EnvInject plugin didn't work for my use-case, wherein I need to export an environment variable when the build fails. When the build fails, the EnvInject build steps is simply not called at all - probably a bug. I am using your solution to export an environment variable in the PostBuild Groovy Step. This environment variable is then picked up by another post-build step (email-ext plugin). Life-saver :) – nonbeing Jan 06 '14 at 09:04
0

Maybe using the envinject plugin ?

coffeebreaks
  • 3,787
  • 1
  • 26
  • 25
  • that should do it through a file – Johnny Chen Aug 21 '13 at 08:22
  • Thanks, coffeebreaks! I've tried it. First, it looked promising - adding a build step of type "Inject environment varialbes" which states that it will "Inject environment variables to the following build steps and post-build actions." Then I couldn't figure out how to get that env var in the post-build Groovy step... Thanks, anyway! – Ivan Aug 21 '13 at 11:14
  • Did you get any solution for this? – encrypted name Jan 29 '20 at 16:21