2

I have a Jenkins pipeline looking like this

stage 'build app'
build 'app-build'
stash 'app-stash'

stage 'build container'
unstash 'app-stash'
build 'container-build'

The builds app-build and container-build obtain new nodes from our Kubernetes system.

With stash I want to transfer the artifacts from app-build to container-build.

However when running this pipeline the following error occurs:

[Pipeline] stash
Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
[Pipeline] End of Pipeline
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
    at org.jenkinsci.plugins.workflow.steps.StepDescriptor.checkContextAvailability(StepDescriptor.java:254)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:179)

I don't want to use node in my pipeline since I only have one executor on my nodes. Is it possible to use stash without the node directive?

Harold L. Brown
  • 8,423
  • 11
  • 57
  • 109
  • Stash will use the context to stash. So if you are not on a node or on a different node then the sources were built, you are probably not stashing what you want. Jenkins can handle executors efficiently. When some job is idle it can run multiple on one executor – Rik Dec 27 '16 at 11:21

2 Answers2

2

You can use stash/unstash to share the files/data between multiple jobs in a single pipeline.

node {
    stage ('HostJob')
     {
        build 'HostJob'
        dir('/var/lib/jenkins/jobs/Hostjob/workspace/') {
        sh 'pwd'
        stash includes: '**/build/fiblib-test', name: 'app' 
        }
     }

        stage ('TargetJob') {
            dir("/var/lib/jenkins/jobs/TargetJob/workspace/") {
            unstash 'app'
            build 'Targetjob'
        }
}

In this manner, you can always copy the file/exe/data from one job to the other. This feature in pipeline plugin is better than Artifact as it saves only the data locally. The artifact is deleted after a build (helps in data management).

It is not possible to use a stash without a node. :(

lakshmi kumar
  • 558
  • 6
  • 7
  • What do you mean by 'It is not possible to use a stash without a node. :(' ? Can you elaborate? I run it trobles. Maybe it has to do with this: https://stackoverflow.com/questions/43942073/unstash-is-not-putting-anything-in-the-next-step-of-jenkins-pipeline – aholbreich May 12 '17 at 16:17
1

Using build you're building an external job. However you cannot use 'stash' to copy things from one job to another.

You either need to archive artifacts inside 'app-build' and copy them using the aritfact copy plugin, or you have to move the content from 'app-build' into the pipeline itself. When doing that you'll have the node context required for the stash.

Btw.: Unstash needs a node context as well as it want to copy the files somewhere.

Joerg S
  • 4,730
  • 3
  • 24
  • 43