3
pipeline{
    agent none
     stages{
        stage('Git checkout'){
            agent { label 'master' }
            steps{  
               stash includes: '/root/hello-world/*', name: 'mysrc'  
               }
}
        
        stage('maven build'){
            agent { label 'slave-1' }
            steps{
            
               unstash 'mysrc'
               sh label: '', script: 'mvn clean package'
            }
        }
    }
    
}

I have cloned my code in master inside hello-world directory. Now I need to copy that folder hello-world to slave. After copying I need to run mvn clean package in slave node.

When I ran it, I got this error:

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Git checkout)
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-12
[Pipeline] {
[Pipeline] stash
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (maven build)
Stage "maven build" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: No files included in stash ‘mysrc’
Finished: FAILURE 
Amedee Van Gasse
  • 328
  • 3
  • 18
iamarunk
  • 103
  • 2
  • 2
  • 4

1 Answers1

3

The include paths provided to the stash command must be relative to the working directory (which is normally the workspace). Jenkins treats them as relative paths even if they start with /. You can, however, stash from an arbitrary location by wrapping the stash directive in a dir:

dir( '/root' ) {
  stash includes: 'hello-world', name: 'mysrc'
}

If you really are stashing from /root though, your problem might also be that unless you are running Jenkins as root the permissions on /root probably won't allow you to access that directory, which would explain why you got the error about no files being included in the stash (because nothing got stashed because it wasn't readable).

You can avoid the "nothing got stashed" problem by adding allowEmpty: false to your stash options, to make it raise an error while stashing if no files were found, rather than raising it while unstashing.

Jason Kohles
  • 190
  • 5