Search code examples
jenkinsjenkins-pipelinecontinuous-deliveryjenkins-2jenkins-declarative-pipeline

Jenkins 2 Declarative pipelines - Is it possible to run all stages within a node (agent any) but having some of them running without it?


I have a CD pipeline that requires user confirmation at some stages, so I would like to free up server resources while the pipeline is waiting for the user input.

pipeline {
  agent any
  stages {
    stage ('Build Stage') {
      steps {
        ...
      }
    }
    stage ('User validation stage') {
      agent none
      steps {
         input message: 'Are you sure you want to deploy?'
      }
    }
    stage ('Deploy Stage') {
      steps {
        ...
      }
    }
  }
}

You can see above that I have a global agent any but in the User Validation Stage I added agent none.

Can someone confirm that this doing what I want (no agent/node is waiting for the user input)? I don't see how to verify it, nothing different in the execution log...

If not, how could I do it?


Solution

  • This will not work as you expect. You cannot specify agent any on the entire pipeline and then expect agent none to not occupy the executor.

    To prove this, you can run this code as you have it, and while it is waiting at the input stage, go to your main jenkins page and look at the Build Executor Status. You will see there is an executor still running your job.

    Next, switch your pipeline to agent none and add agent any to all the other steps (besides your input step) and do the same test. You can see that while waiting at the input stage, none of the executors are occupied.

    As to your question about different workspaces on different nodes... Assuming you are using code from an SCM, it will be checked out on every new node, so that isn't a concern. The only thing you need to worry about is artifacts you created in each stage.

    It is not safe to "hope" that you will stay on the same node, though Jenkins will "try" to keep you there. But even then, there is not a guarantee that you will get the same workspace directory.

    The correct way to handle this is to stash all the files that you may have created or modified that you will need in later stages. Then in the following stages, unstash the required files. Never assume files will make it between stages that have their own node declaration.