我有一个运行 shell 脚本的 Jenkins 工作。当脚本直接放置在构建命令字段中时,它可以完美运行。但是,如果我将脚本放在 .sh 文件中,然后从 Build 命令字段中调用它:
sh $sh_dir/deploy.sh $repo_dir $name $ref $env $site_dir
$sh_dir
是一个环境变量,其余的是作业参数。它不起作用,它在第一个命令上失败,这只是cd $1/$2
同样,这在直接放在命令字段中时非常有效,但在 .sh 文件中时则不然。 .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
是的,目录确实存在,是的,它归
jenkins
所有.我快疯了,试图弄清楚这一点。
编辑: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
最佳答案
评论中的讨论被带到聊天中。
最终发现问题是 Windows 行尾 (CRLF) 导致混淆。例如,目录 /home/repos/magento\r
最后的 CR 真的不存在。有 Windows CIF 共享文件夹潜伏着。该解决方案将涉及研究如何将脚本转换为原生 Unix(仅限 LF)行尾。
关于shell - Jenkins shell 脚本在 .sh 文件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13495782/