How do I use readFileFromWorkspace(<filePath>) function with filepath relative to workspace root?

3,315 views
Skip to first unread message

Per-Arne Landström

unread,
Feb 8, 2017, 6:15:10 AM2/8/17
to job-dsl-plugin
How can I execute Jenkins buildstep "Process Job DSLs " and use "Look on Filesystem" dsl files in some workspace subdirectory and use readFileFromWorkspace() with filepath in other workspace subdirectory 

Example:

- workspace( Jenkins)
  - scripts
    - start.groovy
  - project1
    - dsls
      - dsl1.dsl
      - dsl2.dsl

If I add the following filepaths in "Process Job DSLs " - "Look on Filesystem" dialog box:

    project1/dsls/dsl1.dsl
    project1/dsls/dsl2.dsl

and in, for example dsl1.dsl, I use the following file fragment and place start.groovy in workspace/scripts directory:

                ...
                steps {
                    systemGroovyCommand(readFileFromWorkspace('scripts/start.groovy'))
                }
                ...

then the DSL process step will fail and say that the file is not found!!

- workspace( Jenkins)
  - project1
    - dsls
      - dsl1.dsl
      - dsl2.dsl
      - scripts
        - start.groovy

If I instead move the start.groovy script to workspace/project1/dsls/scripts it works!!

But the question is how can I access files that does not have DSL Root as root, the name of the function "readFileFromWorkspace()" indicates that it fetches from workspace and not from DSL Root?


Victor Martinez

unread,
Feb 8, 2017, 5:38:26 PM2/8/17
to job-dsl-plugin
Weird, I don't see that issue when using JobDSL version: 1.43  and 1.57

mkdir -p scripts
echo 'println "done"' > scripts/start.groovy
mkdir -p project1/dsls
echo "job('1'){ steps { shell(readFileFromWorkspace('scripts/start.groovy'))}}" > project1/dsls/job1.dsl
echo 'job("2"){ logRotator(-1, 10)} ' > project1/dsls/job2.dsl

If you run the above shell snippet in a particular jenkins job and add the jobdsl build step

files: project1/dsls/*.dsl

Do you see the same behaviour? What version?

See my console output from the test I ran

Started by user anonymous
Building in workspace /var/jenkins_home/jobs/test/workspace
[workspace] $ /bin/sh -xe /tmp/hudson5892725562506029948.sh
+ mkdir -p scripts
+ echo println "done"
+ mkdir -p project1/dsls
+ echo job('1'){ steps { shell(readFileFromWorkspace('scripts/start.groovy'))}}
+ echo job("2"){ logRotator(-1, 10)} 
Processing DSL script job1.dsl
Processing DSL script job2.dsl
Existing items:
    GeneratedJob{name='1'}
    GeneratedJob{name='2'}
Finished: SUCCESS

Cheers

Per-Arne Landström

unread,
Feb 17, 2017, 2:43:54 AM2/17/17
to job-dsl-plugin
Sorry for the late answer.
My problem is that I am using the the job-dsl-plugin both in jenkins and standalone outside jenkins and I want both to behave the same way
If I have the setup as specified above I get the following behaviour:

Inside jenkins:

job-dsl-plugin finds the files alright if I specifies
    project1/dsls/dsl1.dsl
    project1/dsls/dsl2.dsl

outside jenkins using precompiled job-dsl-core-1.51-standalone.jar

Executing job-dsl-core-1.51-standalone.jar from the workspace directory and specifies:
    project1/dsls/dsl1.dsl
    project1/dsls/dsl2.dsl

Then the plugin does not find the files that resides in 

workspace( Jenkins)
  - scripts
    - start.groovy

but it finds it if I put them in:

- workspace( Jenkins)
  - project1
    - dsls
      - dsl1.dsl
      - dsl2.dsl
      - scripts
        - start.groovy

It seams like the "root" directory for readFileFromWorkspace() is different depending on if the plugin is executed from inside jenkins or not

I think that it should have the same behavior both ways

Cheers

Daniel Spilker

unread,
Feb 20, 2017, 11:25:05 AM2/20/17
to job-dsl...@googlegroups.com
When running in Jenkins, the path is relative to the workspace. When running standalone, path is relative to the current working directory. When running standalone it's not possible to guess that the workspace would be. You need to change into your "workspace" directory and start the runner from there.

Daniel

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugin+unsubscribe@googlegroups.com.
To post to this group, send email to job-dsl-plugin@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/d87d224b-1acf-4e6a-97be-49a77afc2dd0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Per-Arne Landström

unread,
Feb 21, 2017, 9:40:07 AM2/21/17
to job-dsl-plugin
Yes I will certainly run the standalone plugin from the same directory( workspace root) as inside Jenkins but then I get problem with my imports in my *.dsl files.
They are placed in the same directory as my *.dsl files and the imports works fine when I run the standalone plugin in the same directory, but when I move to the workspace root it does not work.
I have tried to change the imports from "import constants" into "import project1/dsls/constants" and add package declarations in the imported files like "package project1.dsls" and that works but then it does not work from inside Jenkins.
Do you have any ide how I can solve this problem?
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.

Daniel Spilker

unread,
Feb 23, 2017, 4:25:43 PM2/23/17
to job-dsl...@googlegroups.com
Ah, that's odd. The behavior in Jenkins and in the standalone runner differs. The standalone runner uses the current working directory as classpath root (https://github.com/jenkinsci/job-dsl-plugin/blob/job-dsl-1.58/job-dsl-core/src/main/groovy/javaposse/jobdsl/Run.groovy#L37), but in Jenkins the scripts directory is used as classpath root (https://github.com/jenkinsci/job-dsl-plugin/blob/job-dsl-1.58/job-dsl-plugin/src/main/groovy/javaposse/jobdsl/plugin/ScriptRequestGenerator.groovy#L47). Maybe you can use a symlink to workaround this problem.

Please file an issue in JIRA. We may need to add a command line switch to the standalone runner to switch the behavior. The existing behavior should be kept as default to avoid breaking compatibility.

Daniel

To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugin+unsubscribe@googlegroups.com.
To post to this group, send email to job-dsl-plugin@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/dc0cb529-00b1-4ce4-95b9-3c5a412eb9b7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages