'Jenkins agent none VS labels - block execution on delegated step

I have 2 workers, but only one of them has proper 'build stack' (should be used for build only), so I'd like to 'delegate' one step of my pipeline to that node. Build happens only on 'develop' branch, so other Jobs in that repo should execute on the 'remaining' available node.

If I use the label 'builder' in the stage, I get a very strange behavior...

Scenario A

Job is running on a node2 - and waits indefinitely for a free worker - as it does not understand that builder is the same node as a node2

Waiting for next available executor on 'builder'

Scenario B

Job is running on a node1, and is delegated to builder (node2) - but it blocks both nodes, other jobs that could meanwhile execute on node1, are waiting in the queue...

also, step xxxx is waiting indefinitely

Waiting for next available executor on 'node1' (like it could not return to itself).

Any idea what could be a workaround?

I know this is not a common practice to delegate one step to some external node, but to me a behavior where node2 and builder labels are not mapped as the same worker, seems to be a bug...

I know that for a docker-based agent there is a flag reuseNode that in a way would be what I want :D

And for a delegation... well I wish it would not block Job execution on the node1 until the builder is finished... this seems wired to me as well..

2 workers with labels

  • node1, deployer
  • node2, builder
pipeline {
    //agent none
    //agent { label 'node1' } - preferred node for most of the steps
    agent { label 'node1 || node2' }
    stages {
        stage("Prepare build") {
            agent { label 'node2 || builder' }
            when { branch 'develop'}
            steps {
                echo "prepare build"
            }
        }
        stage("Build") {
            agent { label 'builder' }
            when { branch 'develop'}
            steps {
                sh "echo do build"
            }
        }
        stage("Test") {
            agent { label 'node1 || node2' }
            steps {
                sh "echo do test, other things ... "
            }
        }
        stage("xxxx") {        
            agent { label 'node1' }
            steps {
                sh "xxxx"
            }
        }
    }
  post {
    failure {
      sh "echo do something, cleanup.sh"
    }
}

If I use agent none, NONE of my stages are executed, and the post triggers, with an error
ERROR: Attempted to execute a step that requires a node context while agent none was specified. Be sure to specify your own node { ... } blocks when using agent none.

I could add a node {} section to the post step, but since the previous steps are skipped (I believe the git repo is not properly pulled) it is failing anyway even with the node{} due to file missing (cleanup script).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source