Create a Gradle Java Project
We will set up a CI/CD pipeline for a Java Gradle project. The project resides and is maintained in a Github repository. Anytime a new push or a pull request is made on the repo, the latest version of the project's code is compiled and built, tested againsted all the existing (unit) tests, and published as a docker image into Docker Hub. Along the way, we explore some exciting topics such as the use of secrets in GitHub Actions, running tests against the project's code and reporting code coverages, and conditional publishing to Docker Hub only if a certain coverage treshold is met. By the end of this tutorial you should have a fully automated GitHub Action pipleline that publishes your tested Java application as a container image to Docker Hub.
The following picture shows the main steps in the pipleline:
Anytime a developer pushes code to the repo the latest code is built, before the tests are run against it. Once all tests are exectued (and passed), we publish the latest build of the app (i.e. JAR
file) as a docker image into Docker Hub.
IDE & Build Tools
The instructions that follow use Gradle & Intellij, but feel free to use an IDE/tools of your choice!
ci-helloworld
The complete code for ci-helloworld
example (that we create step-by-step in the following several sections) can be found at: https://github.com/jhu-st/ci-helloworld/
#
Create a Java Gradle ProjectCreate brand new Java Gradle project called ci-helloworld.
- Under
src/main/java
folder, create a class namedArrayUtils.java
with two methods:
Next, create a second class named Main
which is where the main
function resides:
The main
function does not do much but we simply include it to have an entry point of execution for the project (if the project is ever executed!); The goal of our project is though to have a set of unit tests (see next step) run against the methods of ArrayUtils
class.
- Under
test/java
create a test class namedArrayUtilsTest.java
with the following test methods:
- Next is
build.gradle
:
This makes sure that both JUnit
and Jacoco
are imported into the project. It also has instructions on how to make a JAR
file by specifying the "main" class of the project (i.e., the class that contains the main
function). Finally, it has instructions for printing each test case status on the standard output when the tests are executed.
- Last but not the least,
add
,commit
andpush
the project to a Github repo namedci-helloworld
on your Github account.