Jenkins Pipeline CPS global lib resource file for

2019-09-16 13:29发布

I have a shell script that is common for many projects in side Jenkins, is it correct to put it into the resources folder and load it as below:

def script = libraryResource 'build/package_indexes/build_push.sh'

while this works:

sh script

but once I start making the build_push.sh script to accept arguments, it won't work since script is not a file...

Second question is that is it correct to output that script variable into a temp file such as

writeFile script '/tmp/tmp.sh'
sh "/tmp/tmp.sh 'cmd_arg'"

1条回答
smile是对你的礼貌
2楼-- · 2019-09-16 13:51

bash -c will do the trick. just remember that the first param is $0 and not $1

for example:

my script -

 echo hello $0!

in Jenkinsfile

 def script = libraryResource 'hello.sh'
 sh "bash -c '$script' world"

and the result is hello world!

查看更多
登录 后发表回答