In our previous article, we learned how to dockerize your Java Springbot project using Jib and Gradle plugin. But in that we have to manually build the docker image and push it to the docker hub. In this article we are going to learn how to automate this manual process using GitHub action.
Prerequisites
A GitHub repository containing your Java project with Gradle as the build tool
A Docker Hub account
Initalize the GitHub Action Workflow
Naviagte to your local project directory and create a new directory called .github/workflows
. Inside that directory create a new file called action.yml. This file will contain the workflow configuration. First we need to provide name for our workflow and need to define the event that will trigger the workflow. In this case we are going to use push event. So our workflow configuration will look like this.
1 2 3 4 5 6
name:JavaCIwithGradle//nameoftheworkflow
on: push://eventthatwilltriggertheworkflow branches: -master//branchthatwilltriggertheworkflow
Now we need to define the jobs that will run when the workflow is triggered. In this case we are going to use job called deploy but you can use any other name you wish to use and it’s running on ubuntu-latest virtual machine. So our workflow configuration will look like this.
Before we build the Docker image, we need to run tests and verify that our application is working properly. To do that we need to create Java env within our workflow. To do that we need to setup Java. To do that we need to add the following code to our workflow configuration.
In certian cases there might be an error saying gradlew is not executable. To avoid that we need to add the following code to our workflow configuration.
As I previsouly mentioned we have used Gradle as our build tool. So we need to setup Gradle environment. To do that we need to add the following code to our workflow configuration.
Now we have setup both Java and Gradle environments within our workflow. Next we need to build our project to check whether it’s working or not.
Build the project
Now we are going to build our project. This step helps us to verify that whether there's an any error in our application or not. To do that we need to add the following code to our workflow configuration.
In here we have used –no-daemon flag to avoid Gradle daemon to avoid long running process and to isolate the each build. clean is used to clean the previous build and build task is a built-in task that compiles the source code, runs tests, and creates build artifacts. It is typically the primary task used to generate the final output of the project, such as JAR files, test reports, and other build artifacts. By using this step we can check whether our project is building successfully or not.
Build the Docker image
Now we are going to build our Docker image. To do that we need to add the following code to our workflow configuration.
To use this step we need to add some custom configuration to our project. First we need to add Jib to our project. To do that we need to add the following code to our build.gradle file’s plugin section.
1 2 3 4
plugins { ... id 'com.google.cloud.tools.jib' version "${jibVersion}"// jibVersion is a variable that we have defined in gradle.properties file https://github.com/dinushchathurya/springboot-gradle-jib-cicd/blob/master/gradle.properties }
Then we need to add the following code to our build.gradle file’s jib section.
1 2 3 4 5 6 7 8
jib { from { image = "gcr.io/distroless/java@sha256:629d4fdc17eec821242d45497abcb88cc0442c47fd5748baa79d88dde7da3e2d" } to { image = "<docker-repository>/${project.name}:${project.version}" } }
To generate unique and standard docker image tag, here I have used custom task called generateMetadata. To do that we need to add the following code to our build.gradle file’s task section.
version = "${project.majorVersion}-build-${project.ext.buildNumber}".toString() }
We need to import apache ant filters ReplaceTokens library in our build.gradle file. To do that we need to add the following code to our build.gradle file’s import section.
1
import org.apache.tools.ant.filters.ReplaceTokens
The ReplaceTokens class is a built-in filter class provided by Apache Ant, which is used to perform token replacement in text files. In Ant build scripts, tokens are placeholders surrounded by ${} or @{}. These tokens can be replaced with specific values during the build process.
Push the Docker image
Now we are going to push our Docker image to the Docker Hub. To do that we need to add the following code to our workflow configuration.
To use this step we need to create a custom task in our build.gradle file. To do that we need to add the following code to our build.gradle file’s task section.
Now we have finihsed with our entire workflow part. You can find the complete workflow configuration from here.
Conclusion
In this article, we learned how to automate your Java Springbot project building using Jib, Gradle plugin and GitHub Actions. You can find the all the related commands for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail.