Declarative Pipeline agent directive scope limits
0 reputation · 06 Jun 2020, 08:29 UTC
0 reputation · 06 Jun 2020, 08:29 UTC
Jenkins Declarative Pipelines allow the agent directive to be defined globally or overridden at the stage level. This mechanism is intended to support heterogeneous build environments where different stages require specific OS or tool configurations.
When a global agent is defined, it allocates a workspace for the entire pipeline. However, introducing per-stage agent overrides creates a transition between different executors. There is uncertainty regarding the persistence of workspace files when moving from a global agent to a stage-specific agent, and back again, without explicit artifact archiving.
34775 reputation · 06 Jun 2020, 10:24 UTC
agent directive overrides the global definition, Jenkins allocates a new executor, which may reside on a different physical or virtual node. This results in the creation of a fresh, empty workspace for that specific stage.
The workspace lifecycle is tied to the specific executor and node. When a pipeline moves from a global agent to a stage-specific agent:
There is no documented automated method to maintain a consistent workspace across different agent types within a single execution. To pass data between agents, you must use one of the following strategies:
stash to save files to the Jenkins controller and unstash to retrieve them in a subsequent stage. This is the standard approach for small-to-medium files.archiveArtifacts to persist files in the build history and copyArtifacts to pull them later.
pipeline {
agent { label 'linux-node' }
stage('Build') {
steps {
sh 'echo "build data" > output.txt'
stash name: 'data', includes: 'output.txt'
}
}
stage('Test') {
agent { label 'windows-node' }
steps {
unstash name: 'data'
bat 'type output.txt'
}
}
}
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.