Here's my Jenkins 2.x pipeline:
node ('master'){
stage 'Checkout'
checkout scm
stage "Build Pex"
sh('build.sh')
}
When I run this pipeline the checkout puts the code into to the workspace as expected, however instead of expecting to find the script in workspace/ (it's really there!), it looks in an unrelated directory: workspace@tmp/durable-d812f509.
Entering stage Build Pex
Proceeding
[Pipeline] sh
[workspace] Running shell script
+ build.sh
/home/conmonsysdev/deployments/jenkins_ci_2016_interns/jenkins_home/jobs/pex/branches/master/workspace@tmp/durable-d812f509/script.sh: line 2: build.sh: command not found
How do I modify this Jenkinsfile so that build.sh is executed in the exact same directory as where I checked out the project source code?
You can enclose your actions in
dir
block.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.I compiled all the answers above and for me it worked like this:
Thanks to @Rafael Manzoni @Keith Mitchell and @Jayan
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"
orsh "bash build.sh"
, so that thesh
block in the Jenkinsfile can correctly locate thebuild.sh
script that you want to execute.I am having the same issue and dir is not helping, possibly because I am working inside a subdirectory of the tmp dir itself (for reason not germane here). My code looks like this
(the
pwd
andla -l
statements were added just for debugging. Issue exists w/o them.) With them I get output like:I ultimately did this:
I was able to get my script execution working with a simplified derivative of Rafael Manzoni's response. I wondered about the whole "JOB_NAME@script" thing and found that unnecessary, at least for declarative using our version of Jenkins. Simply set the access permissions on the workspace. No need to go any deeper than that.
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: