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

送分小仙女□ 提交于 2019-12-03 10:30:19

问题


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?


回答1:


You can enclose your actions in dir block.

checkout scm
stage "Build Pex"
dir ('your new directory') { 
    sh('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.




回答2:


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.




回答3:


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

dir(srcDir){
  sh 'pwd; la -l; jenkins.sh "build.sh"'
}

(the pwd and la -l statements were added just for debugging. Issue exists w/o them.) With them I get output like:

+ pwd
/jenkins/workspace/aws-perf-test@tmp/repos/2
+ ls -l
total 72
-rw-r--r-- 1 jenkins jenkins   394 May 19 12:20 README.md
drwxr-xr-x 3 jenkins jenkins  4096 May 19 12:20 api-automation
-rwxr-xr-x 1 jenkins jenkins   174 May 19 12:20 build-it.sh
-rwxr-xr-x 1 jenkins jenkins   433 May 19 12:20 build-release.sh
-rwxr-xr-x 1 jenkins jenkins   322 May 19 12:20 build.sh
drwxr-xr-x 3 jenkins jenkins  4096 May 19 12:20 ix-core
drwxr-xr-x 3 jenkins jenkins  4096 May 19 12:20 ix-java-client
drwxr-xr-x 3 jenkins jenkins  4096 May 19 12:20 ix-rest-models
drwxr-xr-x 4 jenkins jenkins  4096 May 19 12:20 ix-service
drwxr-xr-x 7 jenkins jenkins  4096 May 19 12:20 ixternal
drwxr-xr-x 5 jenkins jenkins  4096 May 19 12:20 ixtraneous-stuff
-rwxr-xr-x 1 jenkins jenkins   472 May 19 12:20 jenkins.sh
-rw-r--r-- 1 jenkins jenkins 16847 May 19 12:20 pom.xml
+ jenkins.sh build.sh
/home/jenkins/workspace/aws-perf-test@tmp/repos/2@tmp/durable-a3ec0501/script.sh: line 2: jenkins.sh: command not found

I ultimately did this:

dir(srcDir){
  sh 'cdr=$(pwd); $cdr/jenkins.sh "build.sh"'
}



回答4:


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 execute
sh "chmod +x -R ${env.WORKSPACE}/../${env.JOB_NAME}@script"

// Call SH
sh "${env.WORKSPACE}/../${env.JOB_NAME}@script/script.sh"


来源:https://stackoverflow.com/questions/38143485/how-do-i-make-jenkins-2-0-execute-a-sh-command-in-the-same-directory-as-the-chec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!