permission denied when executing the jenkins sh pipeline step

I was getting a similar permissions denied error after following the Jenkins pipeline tutorial for a node project.

./jenkins/test.sh: Permission denied

The original pipeline Test stage looked like the following and returned that error.

stage('Test') {
    steps {
        sh './jenkins/test.sh'
    }
}

I found the following post: https://stackoverflow.com/a/61956744/9109504 and modified the Test stage to the following

stage('Test') {
    steps {
        sh "chmod +x -R ${env.WORKSPACE}"
        sh './jenkins/test.sh'
    }
}

That change fixed the permissions denied error.


I guess you use

stage(name){
   sh ./runSomething
}

Jenkins always uses to user jenkins for running scripts. There are some possibilities:

  1. Jenkins is running with a different user, maybe you start it with some other user.
  2. Something went running when installing jenkins, check that you have a jenkins user

From the terminal you should give permission to this file

sudo chmod -R 777 ./test.sh

and when you push this file it will go with this permissions under the hood, and this way Jenkins will execute this file.