groovy 如何让Jenkins 2.0在 checkout 的目录中执行sh命令?

m3eecexj  于 8个月前  发布在  Jenkins
关注(0)|答案(8)|浏览(84)

以下是我的Jenkins 2.x管道:

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

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

如何修改这个Jenkinsfile,使build.sh在与我 checkout 项目源代码的目录完全相同的目录中执行?

yqkkidmi

yqkkidmi1#

你可以把你的行为放在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>是您实际目录的占位符保持器。默认情况下,它是工作区的相对路径。您可以定义绝对路径,如果您确定代理上存在绝对路径。

jrcvhitl

jrcvhitl2#

你的脚本不工作的原因是因为build.sh不在你的PATH中。
Jenkinsfile正在运行一个“sh”脚本,其全部内容是字符串build.shparent 脚本位于“@tmp”目录中,并且将始终在那里-“@tmp”目录是Jenkins在运行期间保存Jenkins文件的地方。
要解决此问题,请将行更改为sh "./build.sh"sh "bash build.sh",以便Jenkins文件中的sh块可以正确定位您要执行的build.sh脚本。

o3imoua4

o3imoua43#

Jenkins在从你的项目克隆时会创建一个文件夹,如下所示:
/var/lib/jenkins/workspace/job-name@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"
zynd9foi

zynd9foi4#

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

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

(the添加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"'
}
pprl5pva

pprl5pva5#

我能够让我的脚本执行工作与拉斐尔曼佐尼的React简化衍生。我想知道整个“JOB_NAME@script”的事情,并发现这是不必要的,至少对于使用我们版本的Jenkins的声明性。只需设置工作区的访问权限。不需要再深入了。

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

w6mmgewl6#

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

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

感谢@拉斐尔·曼佐尼@基思·米切尔和@贾扬

8nuwlpux

8nuwlpux7#

如果您可以在SCM存储库中的build.sh上设置可执行位,则可以沿着executable header,例如

#!/bin/bash

那么它也应该是可执行的,在 checkout ,所以简单地

sh './build.sh'

如果你不喜欢在配置管理库中的文件上设置模式位(例如,因为它会给Windows用户带来问题),简单的替代方法包括

sh 'sh build.sh'

sh 'bash build.sh'

(取决于它实际上是一个纯Bourne shell脚本,还是真正的Bash),或者

sh '. build.sh'

(This最后一种习惯用法可能容易出错,因为它将在代理上的默认shell中运行脚本,在许多Linux发行版上将是Bash,但在Debian/Ubuntu上将是“Dash”,它会故意阻止特定于Bash的功能。
如果您不希望将Pipeline脚本硬编码为特定的shell(或其他解释器,如Python),则可以如上所述包含#!头文件,但运行

sh 'chmod a+x build.xml && ./build.xml'
qf9go6mv

qf9go6mv8#

使用GIT_CHECKOUT_DIR环境变量

Jenkinsfile:

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

最受欢迎的答案是危险而脆弱的。不需要在多个地方使用依赖于静态操作系统的ABSOLUTE PATH,一旦您更改机器,这些PATH就会破坏您的构建。

相关问题