首页 > 解决方案 > 如何从 Jenkinsfile 步骤中删除脚本中的脚本

问题描述

我不确定如何在声明性 Jenkinsfile 中解开这一步。有人告诉我你不能在脚本中包含脚本,所以我正在寻求有关如何重构它的帮助。

stage('Publish Procurement') {
  steps {
      script: '''
        if (0 != sh(script: "git diff --exit-code modules/procurement", returnStatus: true)) {
          node_modules/.bin/ng build test-library
          cd modules/test-library/dist/
          node_modules/.bin/ng test test-library
        }
      '''
  }
}

我感谢提供的任何帮助。

标签: jenkins

解决方案


尝试这个 :

stage('Publish Procurement') {
  steps {
      returnCode = sh(script: "git diff --exit-code modules/procurement", returnStatus: true)
      if ( 0 != returnCode){
          script: '''
                    node_modules/.bin/ng build test-library
                    cd modules/test-library/dist/
                    node_modules/.bin/ng test test-library
                '''
      }
  }
}

您始终可以将 sh 的输出存储到变量中,然后使用该变量执行任何类型的有效操作。


推荐阅读