Executing Gradle builds on Jenkins
Building Gradle projects doesn’t stop with the developer’s machine. Continuous Integration (CI) has been a long-established practice for running a build for every single change committed to version control to tighten the feedback loop.
In this guide, we’ll discuss how to configure Jenkins for a typical Gradle project.
A text editor
A command prompt
The Java Development Kit (JDK), version 1.7 or higher
A Gradle distribution, version 4.7 or better
A Jenkins installation (setup steps explained in this post)
As example, this guide is going to focus on a Java-based project. More specifically, a Gradle plugin written in Java and tested with Spock. First, we’ll get the project set up on your local machine before covering the same steps on CI.
Just follow these steps:
Clone Gradle Site Plugin repository
git clone https://github.com/gradle-guides/gradle-site-plugin.git cd gradle-site-plugin
As a developer of a Java project, you’ll typical want to compile the source code, run the tests and assemble the JAR artifact. That’s no different for Gradle plugins. The following command achieves exactly that:
./gradlew build BUILD SUCCESSFUL in 12s 14 actionable tasks: 14 executed
The project provides the Gradle Wrapper as part of the repository. It is a recommended practice for any Gradle project as it enables your project to built on CI without having to install the Gradle runtime.
The sample project is equipped with support for generating build scans.
Running the build with the command line option
--scan renders a link in the console.
Publishing build scan... https://gradle.com/s/7mtynxxmesdio
The following section will describe how to build the project with the help of Jenkins.
Jenkins is one of the most prominent players in the field. In the course of this section, you’ll learn how to set up Jenkins, configure a job to pull the source code from GitHub and run the Gradle build.
On the Jenkins website you can pick from a variety of distributions. This post uses the runnable WAR file. A simple Java command brings up the Jenkins server.
java -jar jenkins.war
In the browser, navigate to
localhost with port
8080 to render the Jenkins dashboard.
You will be asked to set up an new administration user and which plugins to install.
Setting up a new Gradle job can be achieved with just a couple of clicks. From the left navigation bar select "New Item > Freestyle project". Enter a new name for the project. We’ll pick "gradle-site-plugin" for the project.
Select the radio button "Git" in the section "Source Code Management".
Enter the URL of the GitHub repository:
Furthermore, create a "Build step" in the section "Build" by selecting "Invoke Gradle build script".
As mentioned before, we’ll want to use the Wrapper to execute the build. In the "Tasks" input box enter the
build and use the "Switches"
--scan -s to generate a build scan and render a stack trace in case of a build failure.
Save the configuration of job and execute an initial build by triggering the "Build Now" button. The build should finish successfully and render a "Gradle Build Scan" icon that brings you directly to the build scan for the given build.
There are various options to trigger Jenkins builds continuously: from polling the repository periodically, to building on a set schedule, or via callback URL.
You can learn more about advanced Jenkins usage through these resources:
Executing Gradle builds on CI can be set up and configured with just a handful of steps. The benefit of receiving fast feedback clearly speaks for itself. If you are not using Jenkins, no problem, many CI products tightly integrate with Gradle as a first-class citizen.