Using Jenkins with Scala, ScalaTest, SBT, and Git

I’ve used Jenkins before, but hadn’t used it in a while, so when I got it running with Scala, SBT, ScalaTest, and Git, I made some notes about how to configure it. You can get Jenkins going with Docker, but I just got Jenkins running by starting its WAR file like this:

java -jar jenkins.war

Jenkins with Scala, SBT, ScalaTest, and Git

My notes on getting everything up and running are a little cryptic, but if you have a little experience with Jenkins I hope they’ll make sense. Here they are:

  • Download and start Jenkins
  • Install the default plugins (or optionally choose the plugins to install)
  • Create the first admin user
  • Configure the port to run Jenkins on (8080 or other)
  • Create a new job/item
    • Give it a name (“Scala/SBT Test 1”)
    • Choose Pipeline
    • Click Ok
  • Next screen
    • Fill in the Description (whatever you want)
    • Under Pipeline:
      • Choose “Pipeline script from SCM”
      • SCM = Git
      • Repo URL: For my purposes today I entered the path to the Git repo on the local filesystem
      • Important: Add a Jenkinsfile to the repo (see below)
      • Save
  • Click “Build now”

Assuming you have a Jenkinsfile like this in the root of your Git project, the build process should succeed:

/* uses sbt, which i installed with homebrew. */
/* this works without requiring the 'sbt plugin'. */

pipeline {
    agent any

    stages {

        stage('Compile') {
            steps {
                echo "Compiling..."
                sh "/usr/local/bin/sbt compile"
            }
        }

        stage('Test') {
            steps {
                echo "Testing..."
                sh "/usr/local/bin/sbt test"
            }
        }

        stage('Package') {
            steps {
                echo "Packaging..."
                sh "/usr/local/bin/sbt package"
            }
        }

    }
}

As noted in the comments, there’s no need for the Jenkins SBT plugin with this approach. I tried to use the SBT plugin initially, but I kept getting errors with it, so I finally went without it. Honestly, I don’t know what the benefit of the SBT plugin is. (If you want to use it, I show how to configure it below.)

Once I finished those steps and clicked “Build Now,” the build succeeded. You’ll have to go through a little more work to configure a Git repository that’s not on the local system, but that’s well documented elsewhere. You can also set up Triggers and other options, but I’m really just trying to show how to use Jenkins with Scala, ScalaTest, SBT, and Git, so again, other Jenkins tutorials can show how to do those things.

For the record, this is what the Scala/SBT build/test results look like in Jenkins:

Jenkins, Scala, SBT, ScalaTest, and Git

Using the Jenkins SBT plugin

If you want to use the Jenkins SBT plugin, you can choose the option to download SBT from scala-sbt.org, or use these steps to configure and use an SBT installation that’s already on your system:

  • Jenkins home > Manage Jenkins > Global Tool Configuration > sbt
    • Add Sbt
    • name = "1.2.6"
    • de-select “Install automatically”
    • sbt launch jar = /usr/local/Cellar/sbt/1.2.6/libexec/bin/sbt-launch.jar
    • Don’t need anything for “sbt launch arguments”
    • Click Save

If you choose this route, other Jenkins/SBT tutorials suggest using a Jenkins configuration like this:

pipeline {
    agent any

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

That worked for me when I didn’t use Git and a Jenkinsfile, but it did not work for me when I used Git and a Jenkinsfile. I didn’t work on this problem for too long because I realized that I could do everything without the Jenkins SBT plugin.

Summary

In summary, if you ever need to use Scala, SBT, ScalaTest, and Git with Jenkins, I hope these notes are helpful.

Reporting live from Boulder, Colorado,
Alvin Alexander