Jenkins shell script not working when in a .sh file
Solution 1
The discussion in the comments was taken to chat.
The problem was eventually discovered to be Windows line endings (CRLF) causing confusion. For example, the directory /home/repos/magento\r
with a CR at the end really doesn't exist. There are Windows CIF shared folders lurking around. The solution will involve working out how to convert the scripts to native Unix (LF only) line endings.
Solution 2
Try this in jenkins shell script
sh 'pwd'
sh 'cd ..'
sh 'pwd'
other script
sh '''pwd
cd ..
pwd'''
In the same "sh" jenkins's cmd, the working directory is same.
Comments
-
Matt Humphrey about 2 years
I have a Jenkins job which runs a shell script. 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 $sh_dir/deploy.sh $repo_dir $name $ref $env $site_dir
$sh_dir
is an env variable and the rest are job parameters. It doesn't work, it fails on the first command which is simplycd $1/$2
Again, this works perfectly when put directly in the command field, but not when in the .sh file. The output from the .sh file job is:
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
.I'm going out of my mind trying to figure this out.
Edit: result of deploy.sh with
-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
-
Jonathan Leffler over 11 yearsChange the
/tmp
script so that it does:sh -x /var/lib/jenkins/scripts/deploy.sh ...
and then report the output of that. Thecd: 1: ...
part is odd; normally, I'd expect to seesh: 1: ...
. There may be some information in that, but I'm not sure what. I assume there's nochroot
stuff going on...you'd probably know if there was. -
Matt Humphrey over 11 yearsQuestion updated.. I'm so confused
-
Jonathan Leffler over 11 yearsHmmm...me too. The output starts with 'Started by user anonymous'. Is the problem as simple as 'the permissions on
/home/repos/magento
do not allow user anonymous tocd
into the directory'? Maybe addid
(orid -a
) to the script before thecd
, and maybe addls -ld / /home /home/repos /home/repos/magento
too. -
Matt Humphrey over 11 yearsUser anonymous is a jenkins user, not a ubuntu user.
-
Jonathan Leffler over 11 yearsOK; then do the
id
andls
stuff... -
Matt Humphrey over 11 yearsEven if I hard code ANY directory in the deploy.sh I still get the same "can't cd to ..."
-
Jonathan Leffler over 11 years
-