脚本管道中的何时条件?

时间:2019-09-03 09:50:25

标签: jenkins jenkins-pipeline

在声明性管道中,我们具有when条件,例如:

pipeline {
    agent any
    stages {
        stage('Example Build') {
            when {
                changeset 'service1\**'
            }
            steps {
                echo 'Building'
            }
        }
    }
}

有什么方法可以在脚本化管道中做某事?

1 个答案:

答案 0 :(得分:0)

您可以像这样(未经测试)检查与某种模式匹配的文件

创建一个函数来检查所需目录中的更改...

@NonCPS
boolean isMyDirChanged() {
  for (changeLogSet in currentBuild.changeSets) { 
    for (entry in changeLogSet.getItems()) { // for each commit in the detected changes
      for (file in entry.getAffectedFiles()) {
        if (file.getPath() ==~ /^service1/) {
          return true
        }
      }
    }
  }
  return false
}

...然后使用该函数确定是否应执行某些代码

if (isMyDirChanged()) {
  print ('Building')
}