如何为詹金斯多分支管道项目设置特定的工作区文件夹 [英] How to set specific workspace folder for jenkins multibranch pipeline projects
问题描述
我有一个外部工具,应该在我的詹金斯工作之一中称为构建步骤.不幸的是,该工具在引用命令方面存在一些问题,以避免在调用路径中出现空格问题.
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安装在C:\Program Files (x86)\Jenkins
中.因此,我无法让詹金斯调用外部工具.
Jenkins is installed in C:\Program Files (x86)\Jenkins
. Hence I'm having trouble with jenkins calling the external tool.
我试图在Jenkins->配置中将工作区根目录"设置为C:\jenkins_workspace
,以避免出现空白.这适用于Freestyle项目,但我的Multibranch Pipeline项目仍已签出并在C:\Program Files (x86)\Jenkins\workspace
下构建.
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
.
一种解决方案是将整个jenkins安装移至例如C:\jenkins
.我想避免这一点.是否有一种适当的方法来告诉Jenkins Pipeline作业也使用工作区根目录"?
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?
感谢您的帮助
推荐答案
ws
指令设置其中的命令的工作区.对于声明性管道,就像这样:
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"
}
您还可以调用脚本来构建要使用的customWorkspace:
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"
}
您还可以通过使用agent
块(通常在pipeline
块的顶部),将其应用于该级别的node
来进行全局设置:
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'
}
}
}
}
稍后的另一条node
指令可能会覆盖它.在 https://jenkins.io/doc/book/pipeline/syntax/中搜索customWorkspace
.您也可以按照docker
和dockerfile
指令使用它.
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.
这篇关于如何为詹金斯多分支管道项目设置特定的工作区文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!