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:

Step 3: Configure tools. Go to Manage Jenkins -> Global Tool Configuration and Set Docker and Sbt installations as follows:

Step 4: Create the Jenkins job. Go to the Jenkins root and click on New Item, give it any name you like and select the Pipeline type of project. To do that set it up as follows:

Step 5: Create your pipeline.

For compiling the Scala project, the code for the pipeline is as following :

stage('Build') {
    steps {
        echo "Compiling..."
        sh "${tool name: 'sbt', type:'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt compile"
    }
}

For running unit tests and scalastyle, the pipeline is as follows :

stage('Unit Test') {
  steps {
     echo "Testing..."
     sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt test"
     sh "${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt scalastyle"
   }
 }

For staging and deployment, the pipeline is :

stage('Docker Publish') {
  steps {
    sh ${tool name: 'sbt', type: 'org.jvnet.hudson.plugins.SbtPluginBuilder$SbtInstallation'}/bin/sbt docker:publishLocal"
  }
}

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.

knoldus-advt-sticker

This entry was posted in Devops, Scala and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s