groovy Jenkins阶段不调用自定义方法

kiz8lqtg  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(168)

我有一个Jenkins管道,它可以在不同的环境中执行一些代码筛选。我有一个筛选方法,我根据传递的参数调用它。但是,在我的构建过程中,调用该方法的阶段什么也不做,也不返回任何结果。在我看来,一切都是正常的。下面是我的代码,以及显示空结果的阶段。
Results:

IAMMap = [
  "west": [
    account: "XXXXXXXX",
  ],
  "east": [
    account: "YYYYYYYYY",
  ],
]

pipeline {
  options {
    ansiColor('xterm')
  }

  parameters {
    booleanParam(
      name: 'WEST',
      description: 'Whether to lint code from west account or not. Defaults to "false"',
      defaultValue: false
    )
    booleanParam(
      name: 'EAST',
      description: 'Whether to lint code from east account or not. Defaults to "false"',
      defaultValue: true
    )
    booleanParam(
      name: 'LINT',
      description: 'Whether to perform linting. This should always default to "true"', 
      defaultValue: true
    )
  }

  environment {
    CODE_DIR             = "/code"
  }

  stages {
    stage('Start Lint') {
      steps {
        script {
          if (params.WEST && params.LINT) {
            codeLint("west")
          }

          if (params.EAST && params.LINT) {
            codeLint("east")
          }
        }
      }
    }
  }

  post {
    always {
      cleanWs disableDeferredWipeout: true, deleteDirs: true
    }
  }
}

def codeLint(account) {
  return {
    stage('Code Lint') {
      dir(env.CODE_DIR) {
        withAWS(IAMMap[account]) {
          sh script: "./lint.sh"
        }
      }
    }
  }
}

Results:

15:00:20  [Pipeline] { (Start Lint)
15:00:20  [Pipeline] script
15:00:20  [Pipeline] {
15:00:20  [Pipeline] }
15:00:20  [Pipeline] // script
15:00:20  [Pipeline] }
15:00:20  [Pipeline] // stage
15:00:20  [Pipeline] stage
15:00:20  [Pipeline] { (Declarative: Post Actions)
15:00:20  [Pipeline] cleanWs
15:00:20  [WS-CLEANUP] Deleting project workspace...
15:00:20  [WS-CLEANUP] Deferred wipeout is disabled by the job configuration...
15:00:20  [WS-CLEANUP] done

正如你所看到的,没有执行任何东西。我向你保证,当在控制台中运行Build with Parameters时,我正在检查所需的参数。据我所知,这是声明性管道的正确语法。

kgqe7b3p

kgqe7b3p1#

不要返回Stage,只需在codeLint函数中执行它。

def codeLint(account) {
    stage('Code Lint') {
      dir(env.CODE_DIR) {
        withAWS(IAMMap[account]) {
          sh script: "./lint.sh"
        }
      }
    }
}

或者,一旦返回舞台,您就可以运行它。这可能需要脚本批准。

codeLint("west").run()

相关问题