0

Can I change the working directory during a Jenkins job for all successive steps?

My job's first step checks out a git project. Unfortunately this project has a mix of technologies; it's not a java/maven project (so the trick of 'mvn -f subdir/pom.xml' doesn't apply) nor is it a gradle project. So I'd like to change to a subdirectory of the checked-out project and start running Jenkins plugins, like invoking shell scripts, like running tox to test python code, like running docker to build images, etc.

Maybe Jenkins wants every step to begin in $WORKSPACE, and allowing a directory change during the job would break some vital assumptions?

I know this has been asked before. Similar questions but answers specific to maven: Jenkins Maven Build -> Change Directory and Jenkins: How To Build multiple top-level projects from a git repository? Similar question but answer specific to gradle: Change directory during a build job on jenkins

chrisinmtown
  • 3,571
  • 3
  • 34
  • 43
  • [This anwser](https://stackoverflow.com/a/52372748/7736617) suggest eto use `dir("relativeDir") { .. }` in the JenkinsFile. Here is the [link](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#dir-change-current-directory) of the documentation – Arnaud Claudel Aug 20 '19 at 20:45
  • Thanks but that answer is in the context of a Jenkins pipeline, which I don't think I'm using, but I will check. – chrisinmtown Aug 20 '19 at 20:49
  • Jenkinsfile is also frequently used for a single job. It allows you to version your jenkins configuration in your project. It's also more expressive than the UI, take a look at it, I think that it will solve your problem – Arnaud Claudel Aug 20 '19 at 20:56
  • I'm generating these jobs via the Jenkins Job Builder from a messy yaml file, not directing the steps via a Jenkins file, so this still does not seem like the solution. – chrisinmtown Aug 20 '19 at 21:54
  • I was saying that *switching to a Jenkinsfile* could be a solution. It's up to you – Arnaud Claudel Aug 21 '19 at 07:57
  • Oh good point but that choice is not available to me, that would require total revamp of our entire CI. :/ – chrisinmtown Aug 21 '19 at 10:15
  • You can separate out job based on sub folders and use filter to checkout in you SCM configuration so only sub folder that you want for that job will get cloned in your workspace. As your first step of your build use batch/shell command to move all file from sub folder to workspace. and then run all the steps that you want. – Mike Aug 21 '19 at 19:58
  • @Mike rearranging folders is a good workaround, please post this as an answer and I'll accept. – chrisinmtown Aug 22 '19 at 13:46

1 Answers1

0

You can separate out job based on sub folders and use filter to checkout in you SCM configuration so only sub folder that you want for that job will get cloned in your workspace. As your first step of your build use batch/shell command to move all file from sub folder to workspace. and then run all the steps that you want.

Mike
  • 512
  • 3
  • 16
  • Thanks for this workaround. Yeah I can do a git sparse checkout but that's not much of a help, the subdirectory remains nested, not at the root. It sounds like the real answer is (1) use a Jenkins file stored in your own repo and (2) within that Jenkins file pipeline use the dir() command. But I'm stuck with YAML-based Jenkins Job Builder definitions, and working around this apparent Jenkins limitation. – chrisinmtown Aug 23 '19 at 15:11