- Jenkins 2.x实践指南
- 翟志军编著
- 610字
- 2020-08-28 00:01:33
3.5 配置pipeline本身
options指令用于配置整个Jenkins pipeline本身的选项。根据具体的选项不同,可以将其放在pipeline块或stage块中。以下例子若没有特别说明,options被放在pipeline块中。
(本节内容,初学者可跳过。)
接下来我们介绍常用的几个选项。
• buildDiscarder:保存最近历史构建记录的数量。当pipeline执行完成后,会在硬盘上保存制品和构建执行日志,如果长时间不清理会占用大量空间,设置此选项后会自动清理。此选项只能在pipeline下的options中使用。示例如下:

• checkoutToSubdirectory:Jenkins从版本控制库拉取源码时,默认检出到工作空间的根目录中,此选项可以指定检出到工作空间的子目录中。示例如下:

• disableConcurrentBuilds:同一个pipeline,Jenkins默认是可以同时执行多次的,如图3-2所示。此选项是为了禁止pipeline同时执行。示例如下:


图3-2 设置disableConcurrentBuilds选项前
在某些pipeline存在抢占资源或调用冲突的场景下,此选项非常有用。设置此选项后,如图3-3所示。

图3-3 设置disableConcurrentBuilds选项后
• newContainerPerStage:当agent为docker或dockerfile时,指定在同一个Jenkins节点上,每个stage都分别运行在一个新的容器中,而不是所有stage都运行在同一个容器中。

• retry:当发生失败时进行重试,可以指定整个pipeline的重试次数。需要注意的是,这个次数是指总次数,包括第1次失败。以下例子总共会执行4次。当使用retry选项时,options可以被放在stage块中。

• timeout:如果 pipeline 执行时间过长,超出了我们设置的 timeout 时间,Jenkins 将中止pipeline。以下例子中以小时为单位,还可以以 SECONDS(秒)、MINUTES(分钟)为单位。当使用timeout选项时,options可以被放在stage块中。

设置此选项后,强迫团队去处理执行时间过长的pipeline,从而优化pipeline的反馈周期。通常将timeout设置为10分钟就可以了。