Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set node label in Jenkins pipeline

Is it possible to set a label instead of a name for nodes Groovy? We want to define labels outside the script to easily access them from the Jenkins Dashboard.

Idea: Instead of:


Groovy Script

node('Slave_1 || Slave_2'){ echo 'Hello world' }

We want something like this:


Pipeline configuration

Node Label Name:     slaveGroup
Node Label Value:    Slave_1 || Slave_2

Groovy Script

node(slaveGroup){echo 'Hello world'}

Or is it possible to use the labels you can set in slave configuration directly in the Groovy script?

like image 891
Rod Kimble Avatar asked Feb 17 '17 07:02

Rod Kimble


People also ask

How do I change a master node name in Jenkins?

Click on Manage Jenkins in the left corner on the Jenkins dashboard. Click on Manage Nodes. Select New Node and enter the name of the node in the Node Name field.

What is label expression in Jenkins?

Defines a logical expression which determines which agents may execute builds for a project. This expression, when tested against the agent name and labels of each available agent, will be either true or false. If the expression evaluates to true, then that agent will be allowed to execute builds of this project.


1 Answers

Just found out that the Pipline Syntax (Generator) gives this option:

Valid Operators

The following operators are supported, in the order of precedence.

(expr)

parenthesis

!expr

negation

expr&&expr

and

expr||expr

or

a -> b

"implies" operator. Equivalent to !a|b. For example, windows->x64 could be thought of as "if run on a Windows slave, that slave must be 64bit." It still allows Jenkins to run this build on linux.

a <-> b

"if and only if" operator. Equivalent to a&&b || !a&&!b. For example, windows<->sfbay could be thought of as "if run on a Windows slave, that slave must be in the SF bay area, but if not on Windows, it must not be in the bay area."

All operators are left-associative (i.e., a->b->c <-> (a->b)->c ) An expression can contain whitespace for better readability, and it'll be ignored.

Label names or slave names can be quoted if they contain unsafe characters. For example, "jenkins-solaris (Solaris)" || "Windows 2008"

More in the documentation.

like image 72
Rod Kimble Avatar answered Oct 24 '22 00:10

Rod Kimble