Use stash in Jenkins pipeline without node

12,084

Solution 1

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. :(

Solution 2

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.

Share:
12,084
Harold L. Brown
Author by

Harold L. Brown

Updated on July 24, 2022

Comments

  • Harold L. Brown
    Harold L. Brown almost 2 years

    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?