How do I make Jenkins 2.0 execute a sh command in the same directory as the checkout? How do I make Jenkins 2.0 execute a sh command in the same directory as the checkout? shell shell

How do I make Jenkins 2.0 execute a sh command in the same directory as the checkout?


You can enclose your actions in dir block.

checkout scmstage "Build Pex"dir ('<your new directory>') {     sh('./build.sh')}... or ..checkout scmstage "Build Pex"sh(""" <path to your new directory>/build.sh""")...

<your new directory> is place holder your actual directory. By default it is a relative path to workspace. You can define absolute path, if you are sure this is present on the agent.


The reason that your script doesn't work is because build.sh is not in your PATH.

The Jenkinsfile is running a "sh" script, whose entire contents is the string build.sh. The parent script is in the "@tmp" directory and will always be there - the "@tmp" directory is where Jenkins keeps the Jenkinsfile, essentially, during a run.

To fix the problem, change your line to sh "./build.sh" or sh "bash build.sh", so that the sh block in the Jenkinsfile can correctly locate the build.sh script that you want to execute.


Jenkins create a folder when it make a clone from your project like this:

/var/lib/jenkins/workspace/job-name@script

For this to work you must set the file as executable if you are in a linux environment and then call the shell script.

Something like this:

// Permission to executesh "chmod +x -R ${env.WORKSPACE}/../${env.JOB_NAME}@script"// Call SHsh "${env.WORKSPACE}/../${env.JOB_NAME}@script/script.sh"