如何让 Jenkins 2.0 在与结帐相同的目录中执行 sh 命令?

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

这是我的 Jenkins 2.x 管道:

node ('master'){
    stage 'Checkout'
    checkout scm
    stage "Build Pex"
    sh('build.sh')
}

当我 运行 这个管道时,检出会按预期将代码放入工作区,但是它并没有期望在工作区 / 中找到脚本(它真的在那里!),而是在一个不相关的目录中查找: 工作区@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

如何修改此 Jenkinsfile 以便 build.sh 在与我检出项目源代码的目录完全相同的目录中执行?

您可以将您的操作包含在 dir 块中。

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

...

<your new directory> 是您实际目录的占位符。默认情况下,它是工作区的相对路径。如果您确定代理上存在绝对路径,您可以定义绝对路径。

我遇到了同样的问题,但 dir 没有帮助,可能是因为我在 tmp dir 本身的子目录中工作(原因与这里无关)。我的代码看起来像这样

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

(添加 pwdla -l 语句只是为了调试。问题存在 w/o 它们。)有了它们,我得到如下输出:

+ 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

我最终是这样做的:

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

您的脚本无法运行的原因是 build.sh 不在您的 PATH 中。

Jenkinsfile 是 运行ning 一个“sh”脚本,其全部内容是字符串 build.shparent 脚本在“@tmp”目录中,并且会一直存在——“@tmp”目录是 Jenkins 保存 Jenkinsfile 的地方,本质上是在 运行 期间。

要解决此问题,请将您的行更改为 sh "./build.sh"sh "bash build.sh",以便 Jenkinsfile 中的 sh 块可以正确定位您所创建的 build.sh 脚本想执行。

Jenkins 在从您的项目克隆时创建一个文件夹,如下所示:

/var/lib/jenkins/workspace/工作名称@script

如果您在 linux 环境中,则必须将文件设置为可执行文件,然后调用 shell 脚本。

像这样:

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

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

我整理了上面的所有答案,对我来说它是这样工作的:

stage('Run Script'){
    steps {
        script {
            sh('cd relativePathToFolder && chmod +x superscript.sh && ./superscript.sh parameter1 paraeter2')
        }
    }
}

感谢@Rafael Manzoni @Keith Mitchell 和@Jayan

我能够使用 Rafael Manzoni 响应的简化派生来执行我的脚本。我想知道整个 "JOB_NAME@script" 事情并发现这是不必要的,至少对于使用我们版本的 Jenkins 的声明。只需设置工作区的访问权限。无需比这更深入。

stage('My Stage') {
    steps {
        sh "chmod +x -R ${env.WORKSPACE}"
        sh "./my-script.sh"
    }
}

使用GIT_CHECKOUT_DIR环境变量

詹金斯文件:

pipeline {
  agent any
  stages {
    stage('Install dependencies') {
      steps {
        dir(GIT_CHECKOUT_DIR) {
          // now everything is executed in your checkout directory
          sh 'yarn'
        }
      }
    }
  }
  post {
    // ...
  }
}

在设置管道时设置 GIT_CHECKOUT_DIR。看到这个 question

更多环境变量列表:

  • ${YOUR_JENKINS_HOST}/env-vars.html
  • 示例:http://localhost:8000/env-vars.html

投票最多的答案是危险而脆弱的。无需在多个地方使用静态操作系统相关的绝对路径,一旦您更改机器就会破坏您的构建。