Not able to use Jenkins labels in pipelines - jenkins

I can't get Jenkins labels to work in Jenkins while using pipelines.
My node is defined with a label say "poolA", and the following pipeline should attempt to run on that node but it doesn't.
Any ideas why?
Console says:
MyAgentis reserved for jobs with matching label expression; Jenkins
is reserved for jobs with matching label expression
This is my pipeline:
node{
label 'poolA'
echo " jenkins pipeline for rapidx node"
}
My node is defined as follows:
Only build job with label expression ....
Launch method : Java webstart
Avail : Keep this agent online as much as possible
Executors # : 1
Other settings unchanged
Note that I am behind a firewall (no internet access during execution) using
Jenkins 2.73.2
This is a clean install of jenkins on windows, the only change I made was to enable Java web start.

Try the following syntax for the pipeline:
node("poolA"){
echo "Jenkins pipeline for rapidx node"
}

Related

Run multiple stages of Jenkins declarative pipe line in same docker slave

Objective
Objective here to migrate a scripted jenkins pipe line to declarative .Scripted pipe line is running on docker slave managed by kubernetes and the working syntax is as below
slave = 'dtr#tes.com/namespace/image:1.0'
dockerNode(image:slave)
{
stage('1'){echo "1"}
stage('2'){echo "2"}
}
The scripted pipe line is working perfect .
Concerns
Trying to use dockerNode to declrative pipeline but in declarative the dockerNode syntax is allowed only after stepes inside a stage
eg:
pipeline{
agent any
stages{
stage('1and2'}{
dockerNode(image:slave){
echo "1"
echo "2"
}
}
}
}
This is making concern to club bulky steps in to one stage than in to multiple one .So we would like your help to understand how can we better align and have multiple stages that is running in same container always .The container images is managed by kubernetes (kube pod with docker images)
To use one container for all steps you need to specify it in agent section
pipeline{
agent {
label 'docker-agent-label'
}
}
To use like so you need to configure pod template in 'Manage jenkins' -> 'Manage nodes and
Clouds' -> 'Configure clouds' -> 'Add new cloud' or use existing one.
It must be kubernetes if your jenkins host integrated with k8s.
UPDATE:

Jenkins building a job in the slave who have the best requirements that matches my job requirements

I want to set Jenkins to launch the build in the slave who have the best environment requirements. For example I have slave 1 with JDK 7 and the slave 2 with JDK 8 and my job runs on JDK 8. I want Jenkins to building my job on the slave 2 automatically because it has JDK 8.
I didn't find anything related to this situation. I have tried some plugins like node and label parameter plugin and Parameterized Trigger Plugin.
These are 2 slaves in 2 different machines. I want Jenkins to build my job in the one with the best requirements for my job.
Solution 1
From the doc, in you pipeline, you can select an agent based on labels: agent { label 'my-defined-label' }
So you should edit your node and add appropriate labels, then target the right one in your pipeline, based on the conditions that suits you. For example if you define a label 'java8' you can then target the agent with:
pipeline{
agent { label 'java8'}
stages {
...
Solution 2
The other solution is to "Restrict where this job can be run" in your job's configuration. When you check that box, you can then specify the name of the slave on which you want the job to execute.

What does the agent mean in jenkins?

I am trying to use jenkins. But when I reading the Declarative Pipeline Syntax, I confused by the agent term
https://jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline
What does the agent stand for?
Is that mean I can set the pipeline runtime folder path?
How to create an agent?
How set a label for agent?
I can feel you :-D.
Here are the answers:
The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional. - Content copied from the agent section
NO, this has nothing to do with the pipeline runtime folder path.
You can for example Create an agent/node by the following tutorial:
How to Setup Jenkins Agent/Slave Using Password and ssh Keys. -
But there are many other ways to create an agent e.g. using a Docker-Container (...).
You can Set a label under the Configuration of the Node.
You can use a label in your pipeline like:
pipeline {
agent { label 'labelName' }
(...)
}
While #adbo covered questions asked, Jenkins glossary describes agent really well:
typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller.
You can choose to run entire pipeline on any available agent (agent any at the top of the pipeline) or run a specific stage on the agent of choice e.g. run build stage in a specific environ by overriding agent in that stage:
agent { docker { image 'my image' } }

jenkins & labels getting : pending—master is offline while trying to execute on non-master nodes

I have a Jenkins instance where I am not able to use labels, builds are triggered but get stuck at "pending—master is offline". I have disabled the master (executor # : 0) as I do not wish to use it.
Instead I would expect the build to go to the next available node with the label mentioned in the pipeline.
node("mylabel"){
echo " jenkins pipeline for mylabel nodes"
}
This works in a clean install of jenkins so I can only assume this is a configuration/compatibility issue on my master instance.
Could it be a permission issue?
more info about my master instance :
I have used in the past the nodeLabel (with freestyle jobs) and have removed it (and removed all extra instructions in my jobs once removed via the management view).
I am using the Role-based Authorization Strategy and have defined roles for each projects in jenkins.
Note that I am behind a firewall (no internet access during execution) using Jenkins 2.73.2
EDIT 1:
another syntax - same issue observed.
pipeline {
agent{
label "mylabel"
echo " jenkins pipeline for mylabel node"
}
}
I found the issue occurred because I am not able to bypass the master node using the above pipeline. I understand that before the label selection there is a need for a default node to be available to run instructions.

Using Multi-configuration projects with Jenkinsfile (build pipeline plugin)

I am trying to create a multi-configuration project that tests a bunch of builds that defines a compatibility matrix (for example by browser and os). I would like to define the steps for a single combination via Jenkinsfile.
How can I achieve this? I can create a pipeline parameterized build which depends on JenkinsFile but I can't figure out how to connect it to the multi-configuration build.
It does not appear to be possible to have a Jenkinsfile as a buildstep in a multi-configuration project. A Jenkinsfile is used in a Pipeline job which is a jobtype aswell and Jenkins does not support (easy) conversion between jobtypes. In addition to that, a buildstep implies that it is run on a particular node in an executor slot. A Jenkinsfile however is evaluated on the master and itself defines buildsteps (with their node labels) which would inherently clash if it was run in a buildstep itself.
It is possible to trigger a pipeline through a multi-configuration job and supply it with the parameters from the multi-configuration job. (I also used the parameterized trigger plugin to do this)
My pipeline job has two text parameters, label and version with a example Jenkinsfile that looks like this:
node(this.label){
println this.version
}
My multi-configuration job has the following configuration:
A custom axis version with values alpha beta gamma and a slave axis label with a selected node
The buildstep "Trigger/call builds on other projects" to trigger my pipeline job with predefined parameters version=${version} and label=${label}
This setup leads to the pipeline job being called 3 times (as I have only one node selected), each time with a different version and running on my specified label. Here is one of the logs:
[Pipeline] node
Running on master in /var/lib/jenkins/jobs/pipelinejob/workspace
[Pipeline] {
[Pipeline] echo
gamma
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
This solution works if you only want to pass text based parameters or label names. It will be considerably trickier to use if you want to do something like "build with different jdks". I hope it is still helpful though.

Resources