Skip to content
Example for a Jenkins shared library with unit tests
Groovy Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/org/somecompany added some comments Jan 4, 2019
test/org/somecompany/build initial commit Jan 3, 2019
vars initial commit Jan 3, 2019
.gitignore initial commit Jan 3, 2019
LICENSE initial commit Jan 3, 2019
README.md Added link to blog post to README Mar 13, 2019
build.gradle initial commit Jan 3, 2019
gradlew initial commit Jan 3, 2019
gradlew.bat initial commit Jan 3, 2019
jenkins-shared-library.iml initial commit Jan 3, 2019
settings.gradle initial commit Jan 3, 2019

README.md

Jenkins Shared Pipeline Example with Unit Tests

This repository contains a example shared library for Jenkins that can be unit tested with JUnit and Mockito. The general idea is:

  1. Keep the custom steps inside vars as small as possible and without any logic. Instead do everything inside classes (inside src).
  2. Create an interface IStepExecutor which declares methods for all required Jenkins steps (sh, bat, error, etc.). The classes call steps only through this interface.
  3. Write unit tests for your classes like you normally would. Use your favorite mocking and dependency framework to mock IStepExecutor.

This way you should be able to:

  • Compile and execute your library/unit tests without Jenkins
  • Test that your classes work as intended
  • Test that Jenkins steps are called with the right parameters
  • Test the behaviour of your code when a Jenkins step fails
  • Build, test, run metrics and deploy your Jenkins Pipeline Library through Jenkins itself

I suggest the IntelliJ IDEA for the least painful Jenkins Shared Library development experience ;)

Head over to my blog post on dev.to if you want a tutorial/more detailed explanation.

Example Jenkinsfile

// add the following line and replace necessary values if you are not loading the library implicitly
// @Library('my-library@master') _

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                ex_msbuild 'test'
            }
        }
    }
}

License

Good ol' WTFPL

You can’t perform that action at this time.