Deploying Scala Application With Jenkins Pipeline

Once upon a time, the software was built using the Waterfall model. This model was simple and easy to understand and use. It is easy to manage due to the rigidity of the model – each phase has specific deliverables and a review process. But, it was painstakingly slow; the builds took a long time to build and deploy. But in this world of heavy competition, we need faster development cycles and faster deployments without compromising on the quality of the product. Therefore, the Agile model came into the picture.

Now, we have DevOps to speed up our deliveries to a very high rate which use CI/CD tools like Jenkins to achieve this. The job of Jenkins is to execute a predefined list of steps eg. to compile the source code, execute test cases, build a Jar from the resulting classes. Jenkins support the creation of pipelines. They are built with simple text scripts that use a Pipeline DSL (domain-specific language) based on the Groovy programming language. The script, typically called, Jenkinsfile defines multiple steps to execute both simple and complex tasks according to the parameters that you establish. Once created, pipelines can build code and orchestrate the work required to drive applications from commit to delivery.

Pre-Requisites

Scala application

SBT

Docker

Jenkins

The lifecycle of CI/CD is as follows:

Considering that our project is ready and has test suite ready to run we can then do the CI/CD stages with the help of Jenkins. Here we are going to create a Jenkins file and add it to the root of our project.

Procedure

Step 1: Configure plugins. For this example we will need andsbt pluginDocker Plugin apart from the default plugins.

On Jenkins, go to Manage Jenkins -> Manage Plugins. Then go to the Available tab, search for both plugins and install them.

Step 2: Configure Docker as a cloud. Go to Manage
Jenkins -> Configure System and at the bottom add a new Docker cloud, make sure it is set up as follows:

I have used sbt docker:publishLocal for setting up a container and running the application. This translates automatically to docker build -t "${projectName}:${version}" To use this command, add the following dependency to your build.sbt

libraryDependencies+="com.spotify"%"docker-client"%"3.5.13"

Also, add the following line to your plugin.sbt

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.3")

Step 5: Now, click on Build Now to execute the pipeline. Jenkins will automatically create and deploy the application image in a container.

It is simple as it is depicted above apart from the lengthy syntaxes which are to be used for using sbt. Admittedly, my pipeline still requires a lot of work, but it can build my Scala code, pass a bunch of unit tests and create a Docker image. Hope you find this useful.

Like this:

Sudeep James Tirkey is a software consultant having more than 0.5 year of experience. He likes to explore new technologies and trends in the IT world. His hobbies include playing football and badminton, reading and he also loves travelling a lot. Sudeep is familiar with programming languages such as Java, Scala, C, C++ and he is currently working on DevOps and reactive technologies like Jenkins, DC/OS, Ansible, Scala, Java 8, Lagom and Kafka.