Boris Brodski
Boris Brodski

Reputation: 8715

How to check, if a node() is already allocated

I'm writing some groovy helper methods for my Jenkins-pipeline jobs. One of my methods is supposed to be called with and without an allocated node, like this:

myMethod() // Call outside of the node (no node allocated yet)
node("...") {
    myMethod() // Call within the node
}

Now to implement the method I have to check, if I'm currently inside a node, and if not, allocate one, like this

def myMethod() {
    if ( -->isNodeAllocated()<-- ) {
        // Do the stuff
    } else {
        node() {
            // Do the same stuff
        }
    }
}

So how to perform this kind of check?

Upvotes: 4

Views: 1241

Answers (1)

Maxfield Stewart
Maxfield Stewart

Reputation: 71

Jenkins sets a environment variable called NODE_NAME if you are in a node. Try this:

if (env.NODE_NAME) {
    // Do stuff 
} else {
    node ('yournode') {
        // Do stuff
    }
}

Upvotes: 4

Related Questions