Matt Humphrey
Matt Humphrey

Reputation: 1554

Jenkins shell script not working when in a .sh file

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 simply

cd $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

Upvotes: 4

Views: 11327

Answers (3)

Rong-Ting Jhang
Rong-Ting Jhang

Reputation: 11

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.

Upvotes: 1

RL-Perf
RL-Perf

Reputation: 353

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. When I switched to /Users/Shared.. folder everything worked fine.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755036

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.

Upvotes: 3

Related Questions