【问题标题】:How do I run multiple sh commands in a function in a Jenkinsfile?如何在 Jenkinsfile 的函数中运行多个 sh 命令?
【发布时间】:2017-07-11 15:30:49
【问题描述】:

鉴于以下 Jenkinsfile:

@NonCPS
void touchFiles() {
    sh "touch foo"
    sh "touch bar"
}

node('general') {
    touchFiles()

    sh "ls"
}

我在 Jenkins 的控制台日志中看到以下内容:

Running on box35 in /internal/slave/build/workspace/1499786637.EXAMPLE_test-repo_test_jenkinsfile
[Pipeline] {
[Pipeline] sh
[1499786637.EXAMPLE_test-repo_test_jenkinsfile] Running shell script
+ touch foo
[Pipeline] sh
[1499786637.EXAMPLE_test-repo_test_jenkinsfile] Running shell script
+ ls
foo

请注意,'bar' 尚未创建。

为什么只有第一个sh 在这里运行?如何在 Jenkinsfile 中分解出一系列 sh 调用?

(我知道我可以写 sh "touch foo; touch bar",但在更复杂的 Jenkinsfile 中,我有时需要单独的 sh 调用。)

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    除了@arifCee 的回答,您还可以这样做:

    @NonCPS
    void touchFiles() {
        sh '''
        touch foo
        touch bar
        '''
    }
    
    node('general') {
        touchFiles()
    
        sh "ls"
    }
    

    【讨论】:

      【解决方案2】:

      尝试一个 sh 命令并连接你的参数:

      @NonCPS
      void touchFiles() {
          sh "touch foo;" +
             "touch bar"
      }
      
      node('general') {
          touchFiles()
      
          sh "ls"
      }
      

      【讨论】:

      • 这是真的,但有时我需要运行单独的sh 命令。
      猜你喜欢
      • 1970-01-01
      • 2019-08-06
      • 2018-06-29
      • 2021-12-08
      • 2018-02-26
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多