Code Monkey home page Code Monkey logo

jenkins-pipeline-util-lib's Introduction

User Guide

The jenkins-pipeline-util-lib is a Jenkins pipeline shared library that provides reusable groovy utility classes and global variables for defining Jenkins scripted pipelines. Please refer to the Jenkins pipeline shared library guide for detailed information on how to install and use the library.

Load library

To load this library, add the following lines to the beginning of your project's Jenkins file:

@Library('jenkins-pipeline-util-lib')
import se.henrrich.ci.*

API examples

Here are examples of utility classes from this shared library:

TestManager

TestManager class is a utility to help manage Jenkins test jobs as well as result collectors that will aggregate test report files generated from those test jobs to specified place.

  • To define a TestManager:
def testManager = new TestManager()
  • To add a test job to be managed by TestManager instance:
testManager.addTestJob this, new TestJob("job alias", 'job name', [job parameters as a list])

e.g.

testManager.addTestJob this, new TestJob("LoginLogoutTest", 'Web - Login Logout - Pipeline - Test', [string(name: 'test.env', value: "${env.testenv}"), [$class: 'MatrixCombinationsParameterValue', combinations: ['BROWSER=chrome', 'BROWSER=chrome incognito'], description: '', name: 'configurations'], string(name: 'tags', value: "${env.tags}"), string(name: 'testartifactsdir', value: "..\\..\\..\\${JOB_NAME}")])
  • To add a test report collector for a job:
testManager.addTestResultCollector new TestResultCollector("job alias", 'filter', 'job name', 'target folder')

Multiple test report collectors can be added to the same job if more types of reports need to be collected. One example can be in multi-configuration job, test reports are generated within each configuration, so reports from different configurations can be collected by defining multiple test result collectors, e.g.:

testManager.addTestResultCollector new TestResultCollector("LoginLogoutTest", 'reports/*chrome.json', 'Web - Login Logout - Pipeline - Test/BROWSER=chrome', 'aggregate/login/')
testManager.addTestResultCollector new TestResultCollector("LoginLogoutTest", 'reports/*chrome_incognito.json', 'Web - Login Logout - Pipeline - Test/BROWSER=chrome incognito', 'aggregate/login/')
  • To select which tests will be executed:
testManager.selectTests [test_alias_1, test_alias_2, test_alias_3, ...]
  • To execute selected test jobs in parallel on slave nodes:
testManager.runSelectedTestJobs this

Note that corresponding test Jenkins jobs have to be predefined. The API will not create job, but instead only trigger them.

  • To aggregate test reports from test jobs:
testManager.collectTestResults this

MavenTool

MavenTool class is a utility to help loading predefined Maven from Jenkins configuration, parsing POM.xml file and build the Maven project.

  • To define a MavenTool:
def mvnTool = new MavenTool('Maven 3.3.9')

The Maven tool name used here must refer to a pre-configured Maven tool in Jenkins "Global Tool Configuration".

  • To get version of a Maven dependency from properties tag in the POM.xml file:
def pom = script.readMavenPom file: 'pom.xml'
def version = MavenTool.getMvnDependencyVersion pom, '<dependency version property name>'
  • To check if artifact version is a snapshot:
boolean isSnapshot = MavenTool.isSnapshot version
  • To run maven build command:
def mvnTool = new MavenTool('Maven 3.3.9')
mvnTool.mvn this, "clean install"
mvnTool.mvn this, "clean package"

MavenProjectBuilder

MavenProjectBuilder is a utility to help build Maven project. If a project has dependencies on other Maven projects, it will check out other projects from their git repo and run build and install before building and packaging the parent project.

  • To specify project to build as well as dependency projects, and build all of them:
def mvnTool = new MavenTool('Maven 3.3.9')
def mavenProjectBuilder = new MavenProjectBuilder('myTestProject', 'https://github.com/henrrich/myTestProject.git', "${env.branch}", mvnTool)
mavenProjectBuilder.addDependency new Dependency('myDependencyProject', 'mydependency.version', 'https://github.com/henrrich/myDependencyProject.git', "${env.branch}", mvnTool)
mavenProjectBuilder.buildAndPackage this

Here are examples of global variables defined in this shared library:

allSlaves

This global variable allows to define steps in a groovy closure that are going to be executed in parallel on all Jenkins slave nodes at the pipeline job's workspace.

allSlaves 'Deploy', { label ->
    stage("Deploy on Slave ${label}") {
        directory.delete()
        artifactsStashUnstashTool.unstashAll this, '.'
        echo "Print out deployed artifacts on slave ${label} workspace:"
        directory.list()
    }
}

directory

This is a utility class to help with directory operations.

directory.list() // list all files under current folder
directory.delete() // clean up the current folder
directory.create(name) // create a folder with specified name

nodes

This is a utility class to help with Jenkins node information.

nodes.slaves() // return all hudson.model.Slave instances as a list
nodes.names(nodes) // return names of specified hudson.model.Slave nodes as strings in a list

cucumber

This is utility class to help with Cucumber reports.

cucumber.publishReport include, exclude // publish cucumber report as selected by include and exclude filters
cucumber.publishReport include // pushlish cucumber report as selected by include filter

e.g.

cucumber.publishReport 'aggregate/**/*.json'

jenkins-pipeline-util-lib's People

Contributors

henrrich avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.