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

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.

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.

Related

Build Jenkins job with Build periodically option with different argument

I have a Jenkins job which accepts only BranchName(bitbucket) as an input parameter.
I have scheduled this Job to run every morning 7 AM with Build periodically option H 7 * * *.
On triggering automatically, it takes default input parameter as development.
Now my requirement is that I need to pass few other branch names as input parameter automatically.
One option I tried it Down stream job with other branch name, but that works only from one branch and not a sophisticated solution.
Is there an easy way I can achieve this?
Job Configuration
If you only want to run a known set of branches you can either:
Create an upstream job for every branch that triggers the build with different parameters.
Create one upstream job, that utilize an list, loop over that list and execute the jobs in parallel.
If you need to get the list of branches dynamically, I would assume, that running sh script: 'git branch', returnStdout: true and some Groovy string split operation is the easiest way to archive that.

Notifications on jenkins job failures - with pipeline from scm

We have several jenkins pipeline jobs setup as "pipeline from scm" that checkout a jenkins file from github and runs it. There is sufficient try/catch based error handling inside the jenkinsfile to trap error conditions and notify the right channels.This blog post goes into a quite a bit of depth about how to achieve this.
However, if there is issue fetching the jenkinsfile in the first place, the job fails silently. How does one generate notifications from general job launch failures before the pipeline is even started?
Jenkins SCM pipeline doesn't have any execution provision similar to catch/finally that will be called if Jenkinsfile load is failed, And I don't think there will be any in future.
However there is this global-post-script which runs groovy script after every build of every job on Jenkins. You have to place that script in $JENKINS_HOME/global-post-script/ directory.
Using this you can send notifications or email to admins based on project that failed and/or reason/exceptions of failure.
Sample code that you can put in script
if ("$BUILD_RESULT" != 'SUCCESS') {
def job = hudson.model.Hudson.instance.getItem("$JOB_NAME")
def build = job.getBuild("$BUILD_NUMBER")
def exceptionsToHandle = ["java.io.FileNotFoundException","hudson.plugins.git.GitException"]
def foundExection = build
.getLog()
.split('\n')
.toList()
.stream()
.filter{ line ->
!line.trim().isEmpty() && !exceptionsToHandle.stream().filter{ex -> line.contains(ex)}.collect().isEmpty()
}
.collect()
.size() > 0;
println "do something with '$foundExection'"
}
You can validate your Jenkinsfile before pushing it to repository.
Command-line Pipeline Linter
There are some IDE Integrations as well
Apparently this is an open issue with Jenkins: https://issues.jenkins.io/browse/JENKINS-57946
I have decided not to use Yogesh answer mentioned earlier. For me it is simpler to just copy the content of the Jenkinsfile directly into the Jenkins project instead of pointing Jenkins to the GIT location of the Jenkinsfile. However, in addition I keep the Jenkinsfile in GIT. But make sure to keep the GIT and the Jenkins version identical.

Jenkins build trigger

I used jenkins remote trigger build option at job A and triggered another job B. When I open job B build, I could see "Started by remote project <path to job A>". I am trying to get the value from job B after execution but I not working. I tried working with BuildUser plugin which gave null output. Could someone help me to find a way to find the information?
The Parameterized Remote Trigger Plugin job setup options have a section Build Info with a field Parameters. Define parameters there like:
TRIGGERED_BY_JOB=${JOB_NAME}
TRIGGERED_BY_BUILD_NO=${BUILD_NUMBER}
and use them accordingly in your job B.
See Jenkins Set Environment Variables for other available information.

How can I analyze console output data from a triggered jenkins job?

I have 2 jobs 'job1' and 'job2'. I will be triggering 'job2' from 'job1'. I need to get the console output of the 'job1' build which triggered 'job2' and want to process it in 'job2'.
The difficult part is to find out the build number of the upstream job (job1) that triggered the downstream job (job2). Once you have it, you can access the console log, e.g. via ${BUILD_URL}consoleOutput, as pointed out by ivoruJavaBoy.
How can a downstream build determine by what job and build number it has been triggered? This is surprisingly difficult in Jenkins:
Solution A ("explicit" approach): use the Parameterized Trigger Plugin to "manually" pass a build number parameter from job1 to job2. Apart from the administrational overhead, there is one bigger drawback with that approach: job1 and job2 will no longer run asynchronously; there will be exactly one run of job2 for each run of job1. If job2 is slower than job1, then you need to plan your resources to avoid builds of job2 piling up in the queue.
So, some solution "B" will be better, where you extract the implicit upstream information from Jenkins' internal data. You can use Groovy for that; a simpler approch may be to use the Job Exporter Plugin in job2. That plugin will create a "properties" (text) file in the workspace of a job2 build. That file contains two entries that contain exactly the information that you're looking for:
build.upstream.number Number of upstream job that triggered this job. Only filled if the build was triggered by an upstream project.
build.upstream.project Upstream project that triggered this job.
Read that file, then use the information to read the console log via the URL above.
You can use the Post Build Task plugin, then you can get the console output with a wget command:
wget -O console-output.log ${BUILD_URL}consoleOutput

Calling a Scriptler script within another Scriptler script

I'm using the Scriptler plugin for Jenkins, and am having a hard time finding any information on how to share the scriptler scripts I'm writing between scripts. I've tried using the ScriptHelper from the Scriptler API, but have run into issues when passing in arguments to the script.
Anyone else come across this and solve it? Is there a standard way to do this (without calling the Jenkins REST API) to execute a script?
More Details
We have a full build MultiJob that contains many phase jobs, each with their own artifacts, with a 3 day time to live on them. When a this full build job is promoted, a scriptler runs against it, pulling each of the phase jobs artifacts into the full build job. By doing so, we can keep the full build alive forever, without changing the lifetime on the artifacts for each phase job (essentially 'keep this build forever' on the full build, ignoring the lifetimes set in the phase jobs.
We also want to pull these artifacts into a deploy job. The idea is that we can point a deploy job to a full build, and it will pull out the artifacts we specify. If the full build is promoted, this script will pull the artifacts directly from the full build job, otherwise, it will pull them from the internal phase jobs. Since we have 2 scripts that work with MultiJobs, I would like to be able to share this code between them.
The script would take a MultiJob name and build number, and return the individual phase job's build numbers, build statuses, and artifact information.
This is possible using Groovy capabilities, though I don't know if Scripler supports it directly. If you are running on the master node, you can use Groovy evaluate. Scriptler scripts are stored as Groovy files on the file system of the master node in the $JENKINS_HOME/scriptler/scripts directory. The Scripter ID is the function name within that directory.
Here is a very simple example. It uses two files. The first is the parameterized function, findByScm.groovy, which finds jobs using a give source control type. The second script, findByGitScm.groovy will evaluate the first function for Git SCMs and print the results.
findByScm.groovy
import jenkins.model.*
jenkins = Jenkins.instance;
// Notice that myScmType is not defined in this function
scmJobs = jenkins.items
.findAll { job -> job.scm != null && job.scm.type == myScmType }
findByGitScm.groovy
// This is supplying the argument to findByScm.groovy
myScmType = 'hudson.plugins.git.GitSCM'
// Now we are evaluating the script
evaluate(new File("${System.getProperty('JENKINS_HOME')}/scriptler/scripts/findByScm.groovy"))
// scmJobs is a variable which was introduced in findByScm.groovy
scmJobs.each { println it }

Resources