Chris Giddings
Chris Giddings

Reputation: 727

Passing git changes to a file for use in later Jenkins jobs

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

What doesn't work

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.

Upvotes: 2

Views: 666

Answers (1)

haschibaschi
haschibaschi

Reputation: 2792

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.

Upvotes: 2

Related Questions