Issue
In Jenkinsfile of my project I constantly meet commands like stash "src"
and unstash "src"
What is the main reasons of doing stash
unstash
in Jenkins Pipeline job?
Solution
Pipelines allow you to specify an agent for each stage that you want to execute. For example when you want to run tests on a Jenkins Windows Agent and on a Jenkins Linux agent. The problem when using different agents for different stages in one pipeline, is the fact that the content of your workspace is not provided to the new agent.
So you can do a git clone
on a agent labeled 'Linux' and when a next stage needs to be executed on a Windows agent, then the Windows Agent need to have the data from the workspace (from the git clone
) which is currently on the Linux agent.
Here for you can use stash/unstash. You can stash your content from the Linux agent (This will save your stashed files on your master) and unstash it on the Windows agent (= extract the data in the workspace of that agent and delete it from the master). In you example you will save the files in src
on your master so you can load them again on a different agent in a later stage.
Stash and unstash should be used for small files, so the example of above is not a very good one (or your repo should be small). Stash is created for stashing some small files/resources, not for stashing whole repositories (will be very slow). Then you need to look to systems which share workspaces between multiple agents.
Using stash is not necessary if you define a 'global' agent for your whole pipeline and run your whole pipeline on the same agent because there isn't a need to send/share the content of the workspace. But in this case you will loose some flexibility of pipelines like running parallel stages on different agents.
Answered By - lvthillo
Answer Checked By - Candace Johnson (JavaFixing Volunteer)