Jenkinsfile - Build Agents questions - docker

I have following questions about Jenkins build agents:
Question 1: agent any means that "Execute the Pipeline, or stage, on any available agent" - how to check what are list of available agents and what are their capabilities (e.g. one agent can build maven, another not...)?
Question 2: agent { label 'docker' } means that I will use agent called "docker" - how to find out is that agent actually exists? Where to find it?
Thanks for help :)

Jenkins allows you to have multiple agents (nodes or slaves) but when you install jenkins the only agent configured is the master.
It is quite simple to configure new nodes, please refer to one of these guides:
https://wiki.jenkins.io/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines+on+Windows
https://www.packtpub.com/mapt/book/application_development/9781783553471/7/ch07lvl1sec47/managing-jenkins-master-and-slave-nodes
http://www.donaldsimpson.co.uk/2011/10/06/jenkins-slave-nodes/
When you are setting up a new node you can assign labels to it so that you can then use to perform specific tasks on that node from a pipeline, for example.
So answering your questions:
This setting can be done using labels.
Example:
All nodes with maven have a label, eg "maven".
Then running something like agent { label 'maven' } will only execute in one of this nodes.
You can list all available nodes and check the configurations for each one in Manage Jenkins > Manage Nodes.

Related

Running one particular job in parallel on Jenkins

I have multiple jobs set on Jenkins. There are only one executor on masters, there are many other slaves. I want to create a job which would run on a separate job queue or a separate executor concurrently. How can I achieve this in simplest way? Can it be achieved without modifying slaves?
The next thing is how can I achieve running on any new build for this job in parallel. Rest of the jobs set should not be disturbed and interfered.
As I can understand you need to configure a particular job to run on a slave node...
In order to achieve that you can 1st add a node to the Jenkins master. There you have a place to add a label to that slave node. The in the pipeline script of your job you can edit it as follows.
pipeline {
agent {
node {
label '<YOUR SLAVE NODE LABEL>'
}
}
}
In the latter part of the question, you have asked to run parallel jobs. Please refer to the parallel job documentation If you need to add sequential stages refer to the sequential stage documentation If you want to create a dynamic parallel job this link would help you.

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.

Jenkins build stalls on `'docker' is waiting`

Still waiting to schedule task
‘docker’ is offline
JenkinsFile:
pipeline {
agent {
node {
label 'docker'
customWorkspace "workspace/${JOB_NAME}/${BUILD_NUMBER}"
}
}
...
What is the cause of this error, and how can I diagnose it further?
I don't see any related containers running via docker ps.
Relating to the agent named docker. They can be viewed on the "Build executor status" / "Nodes" / https://jenkins-url.local/computer/
If you use node {} with a specific label, and don't have any nodes with that label set-up, the build will be stuck forever. You also need to make sure you have at least 2 executors set-up when using a single node (like 'master'), otherwise pipeline builds will usually be stuck, as they consist of a root build and several sub-builds for the steps.

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.

Resources