How to re-use previously created workspace across stages

帅比萌擦擦* 提交于 2019-12-02 20:33:00

Not sure if it fits your use case, but this example script shows how to share the same node/workspace between different stages & containers:

Additionally, if you're running a Docker agent for a specific stage while specifying agent { label 'whatever' } at the top level, you can ensure that this stage will use the same node and workspace as the rest of the Pipeline:

pipeline {
  agent {
    label 'whatever'
  }
  stages {
    stage('build') {
      steps {
        sh "./build-artifact.sh"
      }
    }
    stage('test in docker') {
      agent {
        docker {
          image 'ubuntu:16.04'
          reuseNode true
        }
      }
      steps {
        sh "./run-tests-in-docker.sh"
      }
    }
  }
}

https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Controlling-your-build-environment#reusing-nodeworkspace-with-per-stage-docker-agents

Use dir instead of ws.

ws automatically amend the prefix "@NUMBER" when the workspace to be used was already occupied by other build job.

dir just move current working directory to exactly where you designate.

pipeline {
    agent none
    environment {
        WIN_WORKSPACE = ""
        MAC_WORKSPACE = ""
    }
    stages {
        stage("Build") {
            parallel {
                stage("Build on Windows") {
                    agent {
                        label "windows"
                    }
                    steps {
                        script {
                            WIN_WORKSPACE = WORKSPACE
                        }
                        // steps...
                    }
                }
                stage("Build on macOS") {
                    agent {
                        label "macos"
                    }
                    steps {
                        script {
                            MAC_WORKSPACE = WORKSPACE
                        }
                        // steps...
                    }
                }
            }
        }
        stage("Deploy") {
            parallel {
                stage("Deploy on Windows") {
                    agent {
                        label "windows"
                    }
                    steps {
                        dir(WIN_WORKSPACE) {
                            // steps...
                        }
                    }
                }
                stage("Deploy on macOS") {
                    agent {
                        label "macos"
                    }
                    steps {
                        dir(MAC_WORKSPACE) {
                            // steps...
                        }
                    }
                }
            }
        }
    }
}

Works well as you want.

Specify a custom workspace. From the Pipeline syntax: "ws: Allocate workspace"

ws("/usr/local/jenkins/jobs/custom_workspace") {
    stage . . . 
    stage . . . 
}

. . does this work ?

For me, I set the global agent to "agent none", then the stages will use the same workspace.

Example 1: Using "agent none", 2 stages use the some workspace

pipeline {
    agent none // <- the difference is here
    stages {
        stage('prep') {
            agent { docker { image 'yourdockerimage' }}
            steps {
                sh 'pwd' // same workspace, without @2
            }
        }
        stage('build') {
            agent { label 'master' }
            steps {
                sh 'pwd' // same workspace, without @2
            }
        }
    }
}

Example 2: Using a global agent, 2 stages use the difference workspaces

pipeline {
    agent { label 'master' } // <- the difference is here
    stages {
        stage('prep') {
            agent { docker { image 'yourdockerimage' }}
            steps {
                sh 'pwd' // different workspace, with @2 appended
            }
        }
        stage('build') {
            // no agent specified
            steps {
                sh 'pwd' // same workspace, without @2
            }
        }
    }
}

Would appreciate an explanation why this happens. My guess is that the global agent of the whole pipeline, whenever specified will use the "original" workspace (without @2), any other step which uses a different agent will need to use the @2 workspace. So if no global agent is used, all stages share the same workspace

I thing using the node syntax will solve it.

in order to be certain I would use a dir scope to set the workspace on your own

The External Workspace Manager Plugin might solve your problem.

[...] it defines the local path to the workspace, and switches to it.

// Basic usage:

def extWorkspace = exwsAllocate diskPoolId: 'diskpool1'
node ('linux') {
    exws (extWorkspace) {
        scm checkout
        sh 'mvn clean install -DskipTests'
    }
}
node ('test') {
    exws (extWorkspace) {
        sh 'mvn test'
    }
}

I am glad to say that, Jenkins can share workspace now.

You can specify stages nested within other stages to share workspace.

The new feature is called Sequential Stages.

I will use your original code to implement it.

#!groovy
pipeline {
  agent { label 'master' }
  stages {
    stage('Build') { // 1. Running on master in /var/lib/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ
      steps {
        sh '''
          npm install
          bower install
          gulp set-staging-node-env
          gulp prepare-staging-files
          gulp webpack
        '''
        stash includes: 'dist/**/*', name: 'builtSources'
        stash includes: 'config/**/*', name: 'appConfig'
        node('Protractor') { // 2. Running on vnccentos7 in /var/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ
          dir('/opt/foo/deploy/') {
            unstash 'builtSources'
            unstash 'appConfig'
          }
        }        
      }
    }
    stage('Unit Tests') { // 3. Running on master in /var/lib/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ@2
        parallel {
            stage("Unit Tests@") {
                agent {label 'master'}
                stages {
                    stage("Jasmine") {
                        steps {
                            sh 'gulp karma-tests-ci'
                        }
                    }
                    stage(Mocha") {
                        steps {
                            sh 'gulp mocha-tests'
                        }
                    }
                }
            }
        }
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!