21
votes

I have an external tool that should be called as build-step in one of my jenkins jobs. Unfortunately, this tool has some issues with quoting commands to avoid problems with whitespaces in the path that is called from.

Jenkins is installed in C:\Program Files (x86)\Jenkins. Hence I'm having trouble with jenkins calling the external tool.

What I tried is to set "Workspace Root Directory" in Jenkins->configuration to C:\jenkins_workspace in order to avoid any whitespaces. This works for Freestyle Projects but my Multibranch Pipeline Project is still checked out and built under C:\Program Files (x86)\Jenkins\workspace.

One solution would be to move the whole jenkins installation to e.g. C:\jenkins. This I would like to avoid. Is there a proper way to just tell Jenkins Pipeline jobs to use the "Workspace Root Directory" as well?

Thanks for any help

2
Thanks Olivier for the improvements..Michael

2 Answers

41
votes

the ws instruction sets the workspace for the commands inside it. for declarative pipelines, it's like this:

ws("C:\jenkins") {
  echo "awesome commands here instead of echo"
}

You can also call a script to build the customWorkspace to use:

# if the current branch is master, this helpfully sets your workspace to /tmp/ma
partOfBranch = sh(returnStdout: true, script: 'echo $BRANCH_NAME | sed -e "s/ster//g"')
path = "/tmp/${partOfBranch}"
sh "mkdir ${path}"
ws(path) {
  sh "pwd"
}

you can also set it globally by using the agent block (generally at the top of the pipeline block), by applying it to a node at that level:

pipeline {
  agent {
    node {
      label 'my-defined-label'
      customWorkspace '/some/other/path'
    }
  }
  stages {
    stage('Example Build') {
      steps {
        sh 'mvn -B clean verify'
      }
    }
  }
}

Another node instruction later on might override it. Search for customWorkspace at https://jenkins.io/doc/book/pipeline/syntax/. You can also it use it with the docker and dockerfile instructions.

1
votes

Try this syntax instead:

pipeline {
    agent { 
        label {
            label 'EB_TEST_SEL'
            customWorkspace "/home/jenkins/workspace/ReleaseBuild/${BUILD_NUMBER}/"
        }
    }
}