2

I want to add timestamps() and colorizeOutput() features to our pipeline libraries. I find the wrappers {} in Jenkins documentation:

job('example') {
    wrappers {
        colorizeOutput()
        timestamps()
    }
}

I don`t get how to add wrappers to out library which looks like that:

// file ..src/helpers/Builder.groovy

package helpers.sw_main

def doSomething() {
    // some Groovy stuff here
}

def doSomethingElse() {
    // do something else
}

Our job pipeline looks like that:

#!/usr/bin/env groovy

// this is our library with custom methods
@Library('ext-lib')
def builder = new helpers.Builder()

node {
    try {

        stage('Some Stage') {
            builder.doSomething()
        }
    }

    catch (err) {
        throw err
    }
}

So, I want to add timestamps and ansi-colors to every function from library. Of course, I can do it with wrapping every function with

timestamps() {
    colorizeOutput() {
        // function body
    }
}

But its a little stupid.

So can I easily wrap pipeline or library?

daspilker
  • 8,154
  • 1
  • 35
  • 49
approximatenumber
  • 467
  • 1
  • 7
  • 22
  • If you already are using shared libraries, then what you probably really want to do here is make those methods global vars and not wrappers. That would do what you said you are looking for in the second block of code and avoid what you call "stupid" in the fourth block of code. – Matthew Schuchard Nov 21 '18 at 13:17
  • 1
    @MattSchuchard Can you post a more detailed anwer? What do you mean talking about "make those methods global vars"? Functions in the library may have some input parameters, so... – approximatenumber Nov 21 '18 at 13:45
  • Sure, but the input parameters are optional for global vars (it is unclear whether you want them or not in your comment, but they are optional so either way you are fine). – Matthew Schuchard Nov 21 '18 at 13:51
  • I guess the question was, how can a custim wrapper be defined, which combines the `timestamp()` and `colorizeOutput()` wrappers to one `timezampAndColizeWrapper()` which can be used like `timezampAndColizeWrapper() { dosomething() } @approximatenumber Right? Did you find a solution? – Datz May 06 '19 at 18:56
  • @Datz yes, the question was how to create common wrapper in shared library in order to use it in different scripts. No solution is found, so we dont use timestamps and colors in our jobs. – approximatenumber May 13 '19 at 12:29

1 Answers1

4

One solution to your problem is to use Global Variables (/vars/xxxxx.groovy).

To create an own build step, add a Global Variable like /vars/myOwnStep.groovy:

def call(STAGE_NAME, Closure closure) {
    // do something

    // return something if you like to
}

whcih you can call like this

myOwnStep("Step-name") {
    // what ever you want to do
}

in your pipeline script.

Another possibility is to "overwrite" the sh step. Therefore create a file called /vars/sh.groovy with this code:

def call(String script, String encoding=null, String label=null, boolean returnStatus=null, boolean returnStdout=null) {
    timestamps {
        return steps.sh(script: script, endoding: encoding, label: label, returnStatus: returnStatus, returnStdout: returnStdout)
    }
}

def call(Map params = [:]) {
    return call(params.script, params.get('encoding', null), params.get('label', null), params.get('returnStatus', false), params.get('returnStdout', false)) 
}

(This can be done for other steps too, but he parameters have to match.)

I just added a GitHub repository with some examples: https://github.com/datze/jenkins_shared_library (untested!)

Datz
  • 3,156
  • 3
  • 22
  • 50
  • 1
    I like your hack overwriting standard steps :) ok, we will decorate step by step starting from `sh`. That\`s not elegant way but it\`s better than copypaste `timestams()` to every step. – approximatenumber May 27 '19 at 12:03