Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workspace path in job DSL within Jenkins pipeline

I am creating a jenkins pipeline job to seed jobs using the jenkins job DSL plugin. How do I get the workspace path inside the DSL file? The jenkins pipeline code is as such:

#!groovy
  node{
    stage("build jobs"){
      ws{
        git poll: true,  credentialsId: 'xxx', url: 'ssh://[email protected]:/xxx/xxx.git'
        checkout scm
        jobDsl(removedJobAction: 'DISABLE', removedViewAction: 'DELETE', targets: 'jobs/*.groovy', unstableOnDeprecation: true)
      }
    }
  }

The DSL code that is failing is:

hudson.FilePath workspace = hudson.model.Executor.currentExecutor().getCurrentWorkspace()

With the error:

Processing DSL script pipeline.groovy
java.lang.NullPointerException: Cannot invoke method getCurrentWorkspace() on null object
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:35)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at pipeline.run(pipeline.groovy:1)
    at pipeline$run.call(Unknown Source)

Variables created in the pipeline area are not accessible inside the job DSL step

like image 239
Graeme Avatar asked Jan 12 '17 10:01

Graeme


People also ask

How do I give a workspace path in Jenkins?

So if you wish to change the Jenkins workspace, all you've do is change the path of your JENKINS_HOME. For slave nodes, specify the default workspace on the slave machine in the slave configuration under Manage Jenkins > Manage Nodes > > Configure > Remote FS root.

Where is Jenkins job workspace?

They (usually) don't contain source code like a workspace. Builds are stored in the Jenkins\jobs\[projectName]\builds\[build_id]\ directory. This is a directory managed by Jenkins and you (usually) do not need to modify anything in this directory.

What is workspace in Jenkins job?

The workspace directory is where Jenkins builds your project: it contains the source code Jenkins checks out, plus any files generated by the build itself. This workspace is reused for each successive build.


1 Answers

I stumbled upon this because there seems to be no good way. Here is how I do it:

node {
    stage('test') {
        sh 'pwd > workspace.txt'
        jobDsl scriptText: '''
            String workspace = readFileFromWorkspace('workspace.txt').trim()
            def file = new File(workspace, 'test.txt')
            file.append('It worked!')'''
    }
}

So first grab the workspace in the pipeline script and then pass it to the job dsl. If you have more than just the workspace variable, that you need in your scripts I suggest transferring via a properties file:

node {
    stage('test') {
        sh 'echo "workspace="$(pwd) > build.properties'
        jobDsl scriptText: '''
            Properties props = new Properties();
            props.load(streamFileFromWorkspace('build.properties'))
            def file = new File(props.getProperty('workspace'), 'test.txt')
            file.append('It worked!')'''
    }
}
like image 172
Peter Lutz Avatar answered Nov 15 '22 10:11

Peter Lutz