Giter VIP home page Giter VIP logo

jenkins-util-scripts's Introduction

jenkins-util-scripts jj

A collection of scripts, custom steps that can be used in a Jenkins pipeline (declarative and scripted) as a shared library.

Some of the API's provided:

  • Get a list of the failed stages
  • Extract details about test results (junit)
  • Add/Remove environment variabels to a node
  • Delete jobs based on a condition
  • Fetch plotting data from the current job/pipeline
  • Run a script on multiple slaves
  • ...

Installation

Add this repository as a shared library in your jenkins instance

Usage

See the example for more information.

get_failed_stages

It is possible to get a list of the failed stages. However this is only possible when the stages use the super_stage wrapper instead of directly using the stage API by jenkins.

try {
    super_stage("stage1") {
        // Do stuff
    }
    super_stage("stage2") {
        error "oei"
    }
} catch(e) {
    println "Failed stages: ${get_failed_stages().join(',')}"
}

This will print out => Failed stages: stage2

super_lock

Jenkins default locking API only allows the locking of one resource at the time. This becomes especially an issue if the number of locks is dynamicly programmed. Super_lock is a wrapper arround that API that allows the locking of multiple resources at the same time.

 super_lock(['lock1', 'lock2']) {
     // Do stuff
 }

run_script

Running scripts is a fundamental stone of jenkins, however sometimes a script is not critical for a jenkins pipeline but it still can pollute the logging of the pipeline because of the amount of output it prints to stdout. With the run_script API you can prevent this from happening. The run_script is also os independent (dynamic scripted pipelines, anyone?).

 def directories = run_script(script: "ls -w1", quiet: true, returnStdout: true)
 println directories.split("\n").join(",")

get_junit_results

Jenkins supports the parsing of junit fail to determine the build result. However the results are not directly available to be used in a various number of reporting plugins. For example if you want to share the test results through e-mail, slack, ...

 junit "test.xml"

 def results = get_junit_results()

 println "FAILED TC:"
 println "=========="

 for (def failed: results.failed) {
     println "Name ${failed.name}"
     println "Log: ${failed.log}"
     println "-----"
 } 

get_plot_data

The plot plugin of jenkins is very minimalistic, it does exactly one thing and one thing only: plotting. However sometimes you would want to plot based on the previous data from the previous builds or you want to calculate the average:

node("master")
{
  sh "echo 'job_duration\n${get_job_duration()}'>test.csv"
  plot csvFileName: 'plot-jobduration.csv', group: 'group', style: 'line', title: 'Job duration', 
       csvSeries: [[displayTableFlag: false, exclusionValues: '', file: 'test.csv', inclusionFlag: 'OFF', url: '']]
}
def plot_data = get_plot_data(plot: "jobduration", serie: "job_duration").collect() { it.toBigDecimal() }
println "Average job duration: ${plot_data.sum()/plot_data.size()}"

Other

See the example for more examples ...

Contributing

Feel free to fork and add your own pipeline 'hacks'

jenkins-util-scripts's People

Contributors

roel0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.