如何让 Jenkins 2.0 在与结帐相同的目录中执行 sh 命令? [英] How do I make Jenkins 2.0 execute a sh command in the same directory as the checkout?
问题描述
这是我的 Jenkins 2.x 管道:
Here's my Jenkins 2.x pipeline:
node ('master'){
stage 'Checkout'
checkout scm
stage "Build Pex"
sh('build.sh')
}
当我运行此管道时,结帐会按预期将代码放入工作区,但不是期望在工作区/中找到脚本(它真的在那里!),它会在一个不相关的目录中查找:workspace@tmp/durable-d812f509.
When I run this pipeline the checkout puts the code into to the workspace as expected, however instead of expecting to find the script in workspace/ (it's really there!), it looks in an unrelated directory: workspace@tmp/durable-d812f509.
Entering stage Build Pex
Proceeding
[Pipeline] sh
[workspace] Running shell script
+ build.sh
/home/conmonsysdev/deployments/jenkins_ci_2016_interns/jenkins_home/jobs/pex/branches/master/workspace@tmp/durable-d812f509/script.sh: line 2: build.sh: command not found
如何修改此 Jenkinsfile,以便 build.sh 在与我签出项目源代码的目录完全相同的目录中执行?
How do I modify this Jenkinsfile so that build.sh is executed in the exact same directory as where I checked out the project source code?
推荐答案
您可以将您的操作包含在 dir
块中.
You can enclose your actions in dir
block.
checkout scm
stage "Build Pex"
dir ('<your new directory>') {
sh('./build.sh')
}
... or ..
checkout scm
stage "Build Pex"
sh(""" <path to your new directory>/build.sh""")
...
<你的新目录>
是你的实际目录的占位符.默认情况下,它是工作空间的相对路径.如果您确定代理上存在绝对路径,则可以定义绝对路径.
<your new directory>
is place holder your actual directory. By default it is a relative path to workspace. You can define absolute path, if you are sure this is present on the agent.
这篇关于如何让 Jenkins 2.0 在与结帐相同的目录中执行 sh 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!