简体   繁体   English

Jenkins shell 脚本在 .sh 文件中不起作用

[英]Jenkins shell script not working when in a .sh file

I have a Jenkins job which runs a shell script.我有一个运行 shell 脚本的 Jenkins 工作。 It works perfectly when the script is placed directly in the Build command field.当脚本直接放置在构建命令字段中时,它可以完美运行。 But if I put the script in a .sh file and then call that fine from the Build command field:但是,如果我将脚本放在 .sh 文件中,然后从 Build 命令字段中调用它:

sh $sh_dir/deploy.sh $repo_dir $name $ref $env $site_dir

$sh_dir is an env variable and the rest are job parameters. $sh_dir是一个环境变量,其余的是作业参数。 It doesn't work, it fails on the first command which is simply它不起作用,它在第一个命令上失败,这只是

cd $1/$2

Again, this works perfectly when put directly in the command field, but not when in the .sh file.同样,这在直接放在命令字段中时非常有效,但在 .sh 文件中时则不然。 The output from the .sh file job is: .sh 文件作业的输出是:

Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/deploy
[deploy] $ /bin/sh -xe /tmp/hudson6229756058999358596.sh

+ /var/lib/jenkins/scripts/deploy.sh /home/repos magento master live /home/sites
cd: 1: can't cd to /home/repos/magento

Yes the directory does exist and yes it is owned by jenkins .是的,目录确实存在,是的,它归jenkins

I'm going out of my mind trying to figure this out.我快疯了,试图弄清楚这一点。

Edit: result of deploy.sh with -x :编辑:deploy.sh 与-x结果:

Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/deploy
[deploy] $ /bin/sh -xe /tmp/hudson3304816709711757032.sh
+ sh -x /var/lib/jenkins/scripts/deploy.sh /home/repos magento develop staging /home/sites
+ cd /home/repos/magento
cd: 1: can't cd to /home/repos/magento

The discussion in the comments was taken to chat.评论中的讨论被带到聊天。

The problem was eventually discovered to be Windows line endings (CRLF) causing confusion.最终发现问题是 Windows 行尾 (CRLF) 导致混淆。 For example, the directory /home/repos/magento\\r with a CR at the end really doesn't exist.例如,在末尾带有 CR 的目录/home/repos/magento\\r确实不存在。 There are Windows CIF shared folders lurking around.有 Windows CIF 共享文件夹潜伏着。 The solution will involve working out how to convert the scripts to native Unix (LF only) line endings.该解决方案将涉及解决如何将脚本转换为原生 Unix(仅限 LF)行尾的问题。

Try this in jenkins shell script在 jenkins shell 脚本中试试这个

sh 'pwd'
sh 'cd ..'
sh 'pwd'

other script其他脚本

sh '''pwd
cd ..
pwd'''

In the same "sh" jenkins's cmd, the working directory is same.在同一个“sh”jenkins的cmd中,工作目录是一样的。

I was running into the same issue as Matt above but I the Jenkins (anonymous user) did not have permission to change to that folder I was trying to change directory (cd) to.我遇到了与上述 Matt 相同的问题,但我 Jenkins(匿名用户)无权更改到我试图将目录(cd)更改为的那个文件夹。 When I switched to /Users/Shared.. folder everything worked fine.当我切换到 /Users/Shared.. 文件夹时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM