Search code examples
jenkinsgroovyjenkins-pipelinejenkins-pluginsjenkins-groovy

Picking executor in jenkins Pipline


I have a Jenkins pipeline which uses 2 nodes, each node configured to have 3 executors.

is there a way to pick a specific executor to run the pipline/stage?

i know picking the node can be done using:

pipeline { agent { label XXX } stages{ stage("Build") { agent { label YYY } ...} }

but is there a way to pick the executor? is there any plugin to support such functionality?

I found that checking if executor is idle or busy can be performed using:

def nodeInstance = Jenkins.instance.getNode(nodeName)
def computer = nodeInstance.toComputer()
computer.executors.each { executor ->
def idleState = executor.isIdle()
      ... } 

but no mention of how picking them.


Solution

  • Jenkins allows to configure the number of executors in attempt to utilize the build agent resources more efficiently.

    From Managing Nodes:

    An executor is a slot for the execution of tasks. Effectively, it is a thread in the agent. The number of executors on a node defines the number of concurrent tasks that can run. In other words, this determines the number of concurrent Pipeline stages that can execute at the same time.

    So in fact it's quite an abstract entity. You're not supposed to select a specific executor in your pipelines, thus you cannot do it.

    However, there are some cases when a reference to the executor ID could be used.

    Generally, having multiple executors on the same node can lead to resource conflicts. Say, you have a Maven $HOME/.m2 repository on the agent, and you're in a team that develops a Java project. If all your builds use the same repository, and some of them happen to be built on the same agent by different executors, they:

    • will use the same directory to download gigabytes of the dependencies concurrently;
    • can produce different -SNAPSHOT artifacts of the same version, while being built from different sources.

    To avoid that and make things safer, an administrator could:

    • set up several .m2 repositories according to the number of executors configured on an agent - for example, $HOME/0/.m2, $HOME/1/.m2, and $HOME/2./.m2 if there are 3 executors;
    • after that, instruct you to use the EXECUTOR_NUMBER environment variable in your build scripts:
      mvn clean deploy -Dmaven.repo.local=$HOME/$EXECUTOR_NUMBER/.m2
      

    This will make the builds more isolated from each other, with a consequence of using three times more disk space on each agent.

    However, the safest and simplest option is still to have more agents with one executor per each.