Home > OS >  How to reuse files from workspaces from different parallel stages?
How to reuse files from workspaces from different parallel stages?

Time:02-03

I have a Jenkins pipeline that runs several stages in parallel. Some of those stages produce intermediate build files that I'd like to reuse in a later step:

pipeline {
  stages {
    stage("Parallel build") {
      parallel {
        stage("A") { /* produces file A */ }
        stage("B") { /* produces file B */ }
        stage("C") { /* produces nothing relevant */ }
      }
    }
    stage("Combine") {
      /* runs a task that needs files A and B */
    }
  }
}

As far as I've been able to tell, Jenkins will randomly give me the workspace from one of the parallel stages. So my Combine step will have file A, B or neither, but not both.

How do I resolve this issue?

CodePudding user response:

Few ways to do this.

  1. You can copy the files to a directory you desire(Which you know the path, you can create a sub directory with the build ID for it to be unique) and access them from there.

  2. You can stash the files in the initial stages then unstash them and use them in the latter stages. Here is the documentation.

stash includes: 'something/A.txt', name: 'filea'

unstash 'filea'
  1. Save the workspace location to a global variable and use it in the stages.
pipeline {
  agent any 
  
  stages {
    stage('Run Tests') {
      parallel {
        stage('Stage A') {
          steps {
            script {
              sh ''' 
                  echo "STAGE AAAA" 
                  pwd echo 
                  "ATAGEA" > a.txt
                ''' 
                stageAWS = "$WORKSPACE"
            }
          }
        }
        stage('Stage B') {
          steps {
            script { 
                sh ''' 
                    echo "STAGE B" 
                    pwd
                ''' 
                stageBWS = "$WORKSPACE" }
          }
        }
      }
    }
    stage('Stage C') {
      steps {
        script { echo "$stageAWS" echo "$stageBWS" }
      }
    }
  }
}
  • Related