Jenkinsfile如何模仿2个单独的测试和构建作业

时间:2017-02-07 20:01:52

标签: jenkins jenkins-pipeline

在旧配置中,我们有2个工作,测试和构建。

在测试运行成功后运行构建,但如果我们想跳过测试,我们可以手动触发构建。

在我们使用Jenkinsfile切换到多个管道之后,我们不得不将这两个构建作业放到同一个文件中:

stage('Running tests'){
   ...
}

stage('Build'){
   ...
}

所以现在构建步骤仅在成功运行测试后触发,我们无法手动触发构建,而无需注释测试步骤并提交到存储库。

我想知道是否有更好的方法/做法来利用Jenkinsfile克服这个限制?

1 个答案:

答案 0 :(得分:1)

使用管道和Jenkinsfile正成为现在在Jenkins上运行作业的标准和首选方式。所以使用Jenkins文件肯定是要走的路。

解决问题的一种方法是使作业参数化:

// Set the parameter properties, this will be done at the first run so that we can trigger with parameters manually
properties([parameters([booleanParam(defaultValue: true, description: 'Testing will be done if this is checked', name: 'DO_TEST')])])

stage('Running tests'){
  // Putting the check inside of the stage step so that we don't confuse the stage view
  if (params['DO_TEST']) {
    ...
  }
}

stage('Build'){
   ...
}

作业第一次运行时,它会为作业添加一个参数。之后我们可以手动触发并选择是否应该运行测试。当SCM触发默认值时,将使用默认值。

相关问题