Jenkins shell script not working when in a .sh file

10,288

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.

Share:
10,288
Matt Humphrey
Author by

Matt Humphrey

PHP, DevOps Ruby, open source and git enthusiast.

Updated on June 07, 2022

Comments

  • Matt Humphrey
    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 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
    
    • Jonathan Leffler
      Jonathan Leffler over 11 years
      Change the /tmp script so that it does: sh -x /var/lib/jenkins/scripts/deploy.sh ... and then report the output of that. The cd: 1: ... part is odd; normally, I'd expect to see sh: 1: .... There may be some information in that, but I'm not sure what. I assume there's no chroot stuff going on...you'd probably know if there was.
    • Matt Humphrey
      Matt Humphrey over 11 years
      Question updated.. I'm so confused
    • Jonathan Leffler
      Jonathan Leffler over 11 years
      Hmmm...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 to cd into the directory'? Maybe add id (or id -a) to the script before the cd, and maybe add ls -ld / /home /home/repos /home/repos/magento too.
    • Matt Humphrey
      Matt Humphrey over 11 years
      User anonymous is a jenkins user, not a ubuntu user.
    • Jonathan Leffler
      Jonathan Leffler over 11 years
      OK; then do the id and ls stuff...
    • Matt Humphrey
      Matt Humphrey over 11 years
      Even if I hard code ANY directory in the deploy.sh I still get the same "can't cd to ..."
    • Jonathan Leffler
      Jonathan Leffler over 11 years