Scripting in Jenkins

2,208 views
Skip to first unread message

Thor Waway

unread,
Oct 17, 2017, 5:03:56 PM10/17/17
to Jenkins Users
Hello, 

This is a bit of newbie question so I apologize in advance. 

  • I have 2 nodes currently set up; a MASTER and a WORKER node.  

  • One of the steps in my declarative pipeline points to deploy-script.sh. The contents of  deploy-script.sh: 
  • #!/bin/sh

    whoami

While ssh'ed into WORKER node, from the workspace directory, I am able to run
  • sh ./deploy-script.sh
and get the username of the current user (meaning no permission denied errors). Yet when this same command runs (in the content of steps defined in a JenkinsFile) I get a Permission Denied error


Two things I noticed while trying to work through this issue
1). When I call sh and pass it a command directly, for example: 
stage("Trial){
  steps{
   sh "
whoami"
   sh  "
pwd"
  }
}


        I get: 
[feature-jenkinsfile] Running shell script
+ whoami
worker
-agent
[Pipeline] sh
[feature-jenkinsfile] Running shell script
+ pwd
/home/worker-agent/jenkins/workspace/feature-jenkinsfile





2). Yet when I call 'sh' and pass it a path to a file to run, as I do here: 
stage('Trial2'){
  steps{
   sh "pwd"
   sh "./deploy-script.sh"
  }
}


          It appears that Jenkins creates a new temporary folder from within which it runs the script as can be seen by here: 
[Pipeline] sh
[feature-jenkinsfile] Running shell script
+ pwd
/home/worker-agent/jenkins/workspace/feature-jenkinsfile
[Pipeline] sh
[feature-jenkinsfile] Running shell script
+ ./deploy-staging.sh
/home/worker-agent/jenkins/workspace/feature-jenkinsfile@tmp/durable-7e1f206b/script.sh: 2: /home/worker-agent/jenkins/workspace/feature-jenkinsfile@tmp/durable-7e1f206b/script.sh: ./deploy-staging.sh: Permission denied

When I run `ls -l` from 
What am I missing? What am I failing to take into account?  Would appreciate any feedback/explanations. Thanks

John Mellor

unread,
Oct 17, 2017, 5:19:26 PM10/17/17
to jenkins...@googlegroups.com
You are attempting to run a .sh file that does not have execute permission.

Robert Hales

unread,
Oct 17, 2017, 5:27:42 PM10/17/17
to Jenkins Users
I assume that the temp directory and such has something to do with Jenkin's way of making the script pauseable and restartable (just a guess).  

Either way, it is safer to provide the path to your script to execute. Just reference it with $WORKSPACE/deploy-script.sh. 

I'd be interested in hearing the details about how and why that temp directory is created if anyone knows. 

Robert Hales

unread,
Oct 17, 2017, 5:41:34 PM10/17/17
to Jenkins Users
The permission problem may also be that your Shell setting in Jenkins main configuration is set to something invalid. Try removing that altogether so that it defaults to your normal shell. 


On Tuesday, October 17, 2017 at 11:03:56 AM UTC-6, Thor Waway wrote:

dandeliondodgeball

unread,
Oct 17, 2017, 8:33:14 PM10/17/17
to Jenkins Users
Or maybe move what you have in the file, into Jenkinsfile?  Making use of "script" feature.  (Hopefully I am not misunderstanding the script feature.)

pipeline {
agent any
stages {
stage ('...') {
steps {
script {
...
}
}
}
...
Message has been deleted

Thor Waway

unread,
Oct 18, 2017, 1:18:05 PM10/18/17
to Jenkins Users
Morning, 

Just getting back to this problem and resolved the problem in less than 30 mins due to your tips/suggestions :) Thank you 

Issue was solved by implementing 2 steps: 

  1. First, by referencing $WORKSPACE when providing the path to the deploy-script (i.e $WORKSPACE/deploy-script.sh). This by itself, I suspect, would not have solved my issue but it is a fine code enhancement. 
  2. Second (and critical) step is a embarrassing. As John Mellor said, I was attempting to run a .sh file that did not have execute permission. 
    • Initially thought it wasn't a permission issue because when logged into WORKER node I can run `deploy-script` (I had set permissions for `deploy-script` from within the $WORKSPACE directory on WORKER node). Problem was that I failed to recall that the contents of $WORKSPACE are replaced each build therefore permissions for `deploy-script` need to be set from within the directory holding the repo.  
I still don't understand why Jenkins creates a tmp directory when sh is passed a script file but not when sh is passed a command. Will see if I can find an explanation online. Will post if I do.
Reply all
Reply to author
Forward
0 new messages