从詹金斯管道执行脚本 [英] Execute a script from jenkins pipeline
问题描述
我有一个构建Java工件的jenkins管道, 将其复制到目录中,然后尝试执行外部脚本.
I have a jenkins pipeline that builds a java artifact, copies it to a directory and then attempts to execute a external script.
我正在管道脚本中使用此语法来执行外部脚本
I am using this syntax within the pipeline script to execute the external script
dir('/opt/script-directory') {
sh './run.sh'
}
该脚本只是一个简单的docker构建脚本,但是构建将失败 例外:
The script is just a simple docker build script, but the build will fail with this exception:
java.io.IOException: Failed to mkdirs: /opt/script-directory@tmp/durable-ae56483c
该错误令人困惑,因为该脚本未创建任何目录.它只是构建一个docker映像,并将新构建的java工件放置在该映像中.
The error is confusing because the script does not create any directories. It is just building a docker image and placing the freshly built java artifact in that image.
如果我在jenkins中创建了一个不同的作业,该作业将外部脚本执行为 它唯一的构建步骤,然后使用以下语法从我的管道脚本中调用该作业:
If I create a different job in jenkins that executes the external script as its only build step and then call that job from my pipeline script using this syntax:
build 'docker test build'
一切正常,脚本在其他作业和管道中执行 继续按预期进行.
everything works fine, the script executes within the other job and the pipeline continues as expected.
这是执行工作空间外部脚本的唯一方法吗?
Is this the only way to execute a script that is external to the workspace?
尝试从内部执行脚本时我做错了什么 管道脚本?
What am I doing wrong with my attempt at executing the script from within the pipeline script?
推荐答案
问题是jenkins用户(或运行Jenkins从属进程的任何用户)对/opt
和script-directory@tmp/durable-ae56483c
子目录.
The issue is that the jenkins user (or whatever the user is that runs the Jenkins slave process) does not have write permission on /opt
and the sh
step wants to create the script-directory@tmp/durable-ae56483c
sub-directory there.
要么删除dir
块,然后使用脚本的绝对路径:
Either remove the dir
block and use the absolute path to the script:
sh '/opt/script-directory/run.sh'
或向jenkins用户授予对文件夹/opt
的写许可权(出于安全原因,因此不推荐使用
or give write permission to jenkins user to folder /opt
(not preferred for security reasons)
这篇关于从詹金斯管道执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!