如何在 Jenkinsfile 中执行 Shell 脚本? [英] How can I execute Shell script in Jenkinsfile?

查看:78
本文介绍了如何在 Jenkinsfile 中执行 Shell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 /var/lib/jenkins 中保存了一个名为 urltest.sh 的 shell 脚本文件,并从 jenkins 构建执行该文件.

I am keeping a shell script file named urltest.sh in /var/lib/jenkins and executing the file from jenkins build.

当我执行构建时,它失败了.

When I execute the build, It fails.

The Environment Variables are - 
HOME -          /var/lib/jenkins ;
JENKINS_HOME -  /var/lib/jenkins

控制台输出如下:

Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/AutoScript
[AutoScript] $ /bin/sh -xe /tmp/hudson2777728063740604479.sh
+ sh urltest.sh
sh: 0: Can't open urltest.sh
Build step 'Execute shell' marked build as failure
Finished: FAILURE

我很困惑我应该在哪里保存 shell 脚本文件以便它被执行.

I am confused where I should keep the shell script file so that it is executed.

推荐答案

如果您看到错误消息,它会说

If you see your error message it says

在工作区/var/lib/jenkins/workspace/AutoScript 中构建

Building in workspace /var/lib/jenkins/workspace/AutoScript

并且根据您的评论,您已将 urltest.sh 放入

and as per your comments you have put urltest.sh in

/var/lib/jenkins

/var/lib/jenkins

因此 Jenkins 无法找到该文件.在你的构建步骤中做这件事,它会起作用

Hence Jenkins is not able to find the file. In your build step do this thing, it will work

cd             # which will point to /var/lib/jenkins
./urltest.sh   # it will run your script

如果仍然失败,请尝试 chown 文件,因为 jenkin 用户可能没有文件权限,但我认为如果您执行上述步骤,您将能够运行.

If it still fails try to chown the file as jenkin user may not have file permission, but I think if you do above step you will be able to run.

这篇关于如何在 Jenkinsfile 中执行 Shell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆