Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External workspace manager plugin with declarative pipeline

I want to use the mentioned plugin with a declarative pipeline, to be precise I want to convert the following documentation example to a declarative pipeline:

The pipeline code in the upstream job is the following:

stage ('Stage 1. Allocate workspace in the upstream job')
def extWorkspace = exwsAllocate 'diskpool1'

node ('linux') {
    exws (extWorkspace) {
        stage('Stage 2. Build in the upstream job')

        git url: 'https://github.com/alexsomai/dummy-hello-world.git'

        def mvnHome = tool 'M3'
        sh '${mvnHome}/bin/mvn clean install -DskipTests'
    }
}

And the downstream's Pipeline code is:

stage ('Stage 3. Select the upstream run')
def run = selectRun 'upstream'

stage ('Stage 4. Allocate workspace in the downstream job')
def extWorkspace = exwsAllocate selectedRun: run

node ('test') {
    exws (extWorkspace) {
        stage('Stage 5. Run tests in the downstream job')

        def mvnHome = tool 'M3'
        sh '${mvnHome}/bin/mvn test'
    }
}

Thanks!

like image 629
Evgeni Roitburg Avatar asked Jul 16 '17 10:07

Evgeni Roitburg


People also ask

What plugin is needed for pipeline?

Build Pipeline | Jenkins plugin.

What is the plugin for pipeline in Jenkins?

Pipelines are Jenkins jobs enabled by the Pipeline (formerly called “workflow”) plugin and built with simple text scripts that use a Pipeline DSL (domain-specific language) based on the Groovy programming language.


1 Answers

I searched everywhere for a clear answer to this, yet never found a definitive answer. So, I pulled the External Workspace Plugin code and read it. The answer is simple as long as the plugins Model doesn't change.

Anytunc's answer is very close, but the issue is getting the path from the External Workspace Plugin and getting it into the customWorkspace configuration.

What I ended up doing was creating a method:

def getExternalWorkspace() {
    extWorkspace = exwsAllocate diskPoolId: "jenkins"
    return extWorkspace.getCompleteWorkspacePath()
}

and setting my agent to:

agent {
    node {
        label 'Linux'
        customWorkspace getExternalWorkspace()
    }
}

If you'd rather not set the entire pipeline to that path, you could create as many external workspaces as you want, then use

...
steps {
    dir(getExternalWorkspace()) {
        do fancy stuff
        ...
    }
}
...
like image 52
trash80 Avatar answered Oct 21 '22 01:10

trash80