2

ENVIRONMENT
I have a series of jobs in Jenkins where Job #1 is an initial build and job #n is deployment to production.

Only the first few jobs are connected, and all jobs are parameterized.

I only build the one time, and if that build is successful post build steps trigger a job to deploy that build to dev.

After deployment to dev I have to manually go to Jenkins and click to run the job to deploy to the next region/environment.

THE PROBLEM
I am able to successfully pass the $GIT_COMMIT to the downstream jobs because as a workspace based environment variable during job-run I can write it to a file for use later.

However, $CHANGES is an email-ext specific variable and I am having issues writing its contents to a file I can pass to downstream jobs for the purpose of tracking what the current build is deploying in a given environment.

My unfamiliarity with Groovy and my weak Google-fu have made trying pre-send script and post-send script difficult to work with to get the data I want passed to downstream jobs.

WHAT I HAVE TRIED
What works

  • I am able to send HTML emails using email-ext
  • I am able to pass $GIT_COMMIT to a file and use it in downstream jobs
  • I am able to load the contents of a file into an email using email-ext

What doesn't work

  • I cannot seem to use Groovy in a pre-send script to output the $CHANGES to a file for use.

  • Trying to output $CHANGES to a file in a post-send script also does not work, but probably wouldn't be best anyway since it would likely come after any opportunity to pass the file to a downstream job.

WHAT I HAVE NOT TRIED
I have seen suggestions to use the changelog registered by the SCM process, which apparently is recorded in XML which must then be parsed by either the initial build job or downstream jobs if it is to be formatted for inclusion in an HTML email.

HELP
If anyone has any suggestion on what to try next I would appreciate it. I'm pulling my hair out.

Community
  • 1
  • 1
Chris Giddings
  • 697
  • 2
  • 7
  • 20

1 Answers1

2

You can use this groovy script to access the build environment parameters of an arbitrary job on the same jenkins instance.

To execute the script you have to install the groovy plugin and execute the script as system groovy script.

import jenkins.model.Jenkins

job = Jenkins.instance.getJob("freestyle-test")
numbuilds = job.builds.size()
if (numbuilds == 0) { return }
lastbuild = job.builds[numbuilds - 1]
println 'JOB: ' + job.fullName
println ' -> lastbuild: ' + lastbuild.displayName + ' = ' + lastbuild.result
println ' -> lastbuild someEnv: ' + build.environment.get("SOME_ENV")

The coupling to the job is over the job name. The selected build is the latest.

haschibaschi
  • 2,734
  • 23
  • 31
  • I attempted to use that plugin to pass that data and it works fine. However the real problem is that I have non-chained jobs which need the changes and got commit details from other jobs. For example, I have a job to deploy some artifacts to production, and I want to present the $CHANGES & $GIT_COMMIT values from jobs which may have been run days or week's prior. – Chris Giddings Apr 21 '17 at 00:26
  • Thanks for the help! I am accepting the answer although for my purposes at this time I am writing the git commit and the change details from the upstream build job to files and simply putting those details into an `HTML` formatted email as required. At some point down the line I am likely to convert to groovy scripts. But, at least for now, I think my organization needs some more maturity in these processes before we script them out. – Chris Giddings Apr 26 '17 at 21:25