Jenkins declarative pipeline: What workspace is associated with a stage when the agent is set only for the pipeline?

筅森魡賤 提交于 2019-12-06 01:44:57

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/43948248/jenkins-declarative-pipeline-what-workspace-is-associated-with-a-stage-when-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!