8
votes

Here is an example of declarative pipeline where the agent is set for the pipeline but not set in the individual stages:

pipeline {
    agent { node { label 'linux' } }
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        stage('Build') {
            steps {
                sh 'make'
            }
        }
    }
}

Documentation I've found about scripted pipeline makes it clear that a single workspace will be used within a single node block but multiple node blocks might be allocated multiple workspaces, therefore it is necessary to stash between those steps, use the External Workspace Plugin, etc. if you want to be sure of what's in the workspace between steps.

I had a hard time finding documentation about workspace guarantees for declarative pipeline. What guarantees about workspaces exist for this example?

I believe I encountered two stages executing in different workspaces during testing of a similar pipeline but I'm not sure that's what was happening. I'd really like to avoid needing to stash my checkout prior to my build step or use the External Workspace plugin so I was hoping there'd be a way to force all my stages to run all in one workspace/on one node.

2

2 Answers

5
votes

The Pipeline code presented should only create a single workspace and run all stages in it. Unless you create a new agent directive in any of your stages it will not utilize another node or workspace.

btw, checkout scm happens automatically at the beginning of the Pipeline with Declarative so you don't need to explicitly call that out.

3
votes

i'm 70% sure--based on anecdotal evidence--that you will always get the same workspace on the same node in different stages of a declarative pipeline if you specify a node at the top level and never override it, the way you're doing.

i reserve the right to adjust my confidence level as i receive feedback on this answer. :D