paint-brush
How to Get the Last Successful Git Commit SHA in Jenkinsby@vladislavbilay
7,102 reads
7,102 reads

How to Get the Last Successful Git Commit SHA in Jenkins

by Vladislav BilayMay 9th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, we will explore how to get a [Git commit] SHA from the last successful Jenkins build for further use in the upstream builds on different agents. Prerequisites are Jenkins (Freestyle project) with at least one or more Jenkins agents.
featured image - How to Get the Last Successful Git Commit SHA in Jenkins
Vladislav Bilay HackerNoon profile picture


In this article, we will explore how to get a Git commit SHA from the last successful Jenkins build for further use in the upstream builds on different agents.

Prerequisites:

  • Jenkins (Freestyle project) with at least one or more Jenkins agents.
  • GIT.
  • Groovy.


In Jenkins, we need to install the Groovy plugin. For that step you need to go Manage Jenkins → Manage Plugins → Available Plugin



We need to restart Jenkins to finish the plugin installation process.


After plugin installation, In Jenkins job configuration, in the field “Build Steps“ choose “Execute system groovy script”.





The difference between the System groovy script and the Groovy script is that system groovy script runs inside master JVM’s (you need to have root permission for your Jenkins user). Groovy scripts run in a forked JVM, on the slave where the build is run.


Script section:

In this section, we can choose between the Groovy command and the Groovy script file(you can put Groovy file wherever you want on the slave node or git repository).





Add the following groovy script:

import hudson.EnvVars
import hudson.model.*

// Get current name of running job. Example: "Test-Job"

def jobName = build.project.name;

// Get instance of the current job for specified name

def job = hudson.model.Hudson.instance.getItem(jobName);

// Get list of the builds (objects) for current job. Example: [Test-Job #1, Test-Job #N] 

def builds = job.getBuilds();

// Find the first build with environment variables equals 'YES'. Descending numbering. It means [99, 98, , 1]. The first is 99

def successfulBuild = builds.find { build -> (build.envVars['IS_BUILD_SUCCESSFUL'] == 'YES') };

if (successfulBuild == null) {
  return;
}

// Get git commit for successful build

def gitCommitForSuccessfulBuild =  successfulBuild.envVars['GIT_COMMIT'];
def isLastSuccessFullBuild = successfulBuildToSF.envVars['IS_BUILD_SUCCESSFUL'];

println('Successful build number');
println(successfulBuild.envVars['BUILD_NUMBER']);
println(isLastSuccessFullBuild);

// Prepare a variable for export to environment in order to be accessible from next jobs

if (gitCommitForSuccessfulBuild) {
  def varForExport = [
    GIT_COMMIT_FOR_LAST_SUCCESSFUL_BUILD:  gitCommitForSuccessfulBuild,
    IS_LAST_SUCCESSFUL: isLastSuccessFullBuild
  ];
  build.environments.add(0, Environment.create(new EnvVars(varForExport)));
};



Now we need to add our new variable “IS_BUILD_SUCCESSFUL“ to the build process and Inject environment variables.


For this, we need to add “Execute Shell” and “Inject environment variables“ in the last steps of our build.


Inside “Execute Shell” we are writing our variable and put it inside the env.properties file:

echo IS_BUILD_SUCCESSFUL="YES" > env.properties




Inside “Inject environment variables“ we are writing the name of the file with the variable:


This example will work after one successful build (Jenkins must write at least one successful variable to the file).


Summary

In this scenario, we explored the step-by-step instructions for how can we get the GIT commit SHA from a successful downstream build to an upstream build. Also, in a similar example, we can get any variable used in the downstream build.