Serban Cezar
Serban Cezar

Reputation: 535

Jenkins shared pipeline lib - get build status

I am trying to build a reusable library for a Jenkins pipeline project, but I can't seem to figure out how to get the build status as well as set it. If I use a Jenkinsfile or a vars script, it works, but what I'm interested in is using the src/ library structure.

I've managed to get other stuff working such as regular git, sh, node, stage steps by using an interface but I cannot get the 'currentBuild' wrapper to work. I tried lots of things and I think I need to use RunWrapper but I can't figure it out.

I used to be able to use the following block before in my Pipeline script directly, and now I want to move it to my lib: currentBuild.result = "UNSTABLE"

Upvotes: 1

Views: 1550

Answers (1)

jos
jos

Reputation: 1092

while using src i believe you call the file using new key word. have you tried passing the script element from JenkinsFile

Example JenkinsFile

@Library('somalib@version')
import com.mypackage.MyClass //this is inside src folder
new MyClass(this).execute() //pass this to constructor

your MyClass looks like

package com.mypackage

class MyClass {
    this.script
    MyClass(script){
        this.script = script
    }

    execute(){
        script.node('nodeName'){
            script.echo(script.currentBuild.result)
        }
    }
}

Upvotes: 3

Related Questions