总结:四个Pipeline脚本式与声明式语法差异

开发 前端
如果您阅读此博客文章,则很有可能正在寻找有关脚本化和声明性管道之间的实际差异的信息,对吗?那你找不到更好的地方了。我将向您展示这两者之间的四个最实际的区别。和我待几分钟,享受旅程!

[[374535]]

 如果您阅读此博客文章,则很有可能正在寻找有关脚本化和声明性管道之间的实际差异的信息,对吗?那你找不到更好的地方了。我将向您展示这两者之间的四个最实际的区别。和我待几分钟,享受旅程!

为什么要有两种管道类型?

  • 脚本化管道是Jenkins中作为代码的管道的第一个实现。即使它使用底层的管道子系统,它还是或多或少地设计为使用Groovy构建的通用DSL。这意味着它不具有固定的结构,并且由您决定如何定义管道逻辑。
  • 声明性管道更自以为是,其结构是明确定义的。可能看起来有些局限。

但实际上,您可以使用脚本化或声明性管道来实现相同的目的。那么选择哪一个呢?如果您问我这个问题,我会说使用声明性管道。以下内容这就是为什么。

1.管道启动时的代码验证

  1. pipeline { 
  2.     agent any 
  3.  
  4.     stages { 
  5.         stage("Build") { 
  6.             steps { 
  7.                 echo "Some code compilation here..." 
  8.             } 
  9.         } 
  10.  
  11.         stage("Test") { 
  12.             steps { 
  13.                 echo "Some tests execution here..." 
  14.                 echo 1 
  15.             } 
  16.         } 
  17.     } 

如果我们尝试运行以下管道,则验证将很快使构建失败。该日志显示只能与触发String参数,所以我们得到这样的错误。


请注意,管道没有执行任何阶段,只是失败了。这可能为我们节省了很多时间-想象一下执行Build阶段几分钟,而只是获取echo步骤希望得到的信息java.lang.String而不是java.lang.Integer

现在,让我们看一下与该示例等效的脚本管道。

  1. node { 
  2.     stage("Build") { 
  3.         echo "Some code compilation here..." 
  4.     } 
  5.  
  6.     stage("Test") { 
  7.         echo "Some tests execution here..." 
  8.         echo 1 
  9.     } 

该管道执行相同的阶段和相同的步骤。但是,有一个明显的区别。让我们执行它,看看它产生什么结果。


它按预期失败。但是这次是执行Build阶段,也是Test阶段的第一步。如您所见,没有验证管道代码。在这种情况下,声明式管道可以更好地处理此类用例。

2.从指定步骤重新开始

声明式管道具有的另一个很酷的功能是“从阶段重新启动”。让我们修复上一个示例中的管道,看看是否只能重新启动Test阶段。

  1. pipeline { 
  2.     agent any 
  3.  
  4.     stages { 
  5.         stage("Build") { 
  6.             steps { 
  7.                 echo "Some code compilation here..." 
  8.             } 
  9.         } 
  10.  
  11.         stage("Test") { 
  12.             steps { 
  13.                 echo "Some tests execution here..." 
  14.             } 
  15.         } 
  16.     } 

让我们执行它。


在这里您可以看到已选择测试阶段。在右侧的步骤列表上方,有一个名为“重新启动测试”的选项。让我们单击它并查看结果。


如您所见,Jenkins跳过了Build阶段(它使用了先前构建中的工作空间),并从Test阶段开始了下一个管道执行。当您执行一些外部测试并且由于远程环境的某些问题而导致测试失败时,这可能会很有用。您可以使用测试环境解决问题,然后重新运行该阶段,而无需重建所有工件。(在这种情况下,应用程序的代码未更改。)

现在,让我们看一下脚本化管道示例。

  1. node { 
  2.     stage("Build") { 
  3.         echo "Some code compilation here..." 
  4.     } 
  5.  
  6.     stage("Test") { 
  7.         echo "Some tests execution here..." 
  8.     } 

 

如您所见,没有重新启动选项。声明式管道与脚本式管道-2:0。

3.声明式管道options块

两种管道类型都支持第三个功能,但是我认为声明性管道更好地处理了它。假设我们将以下功能添加到上一个管道中。

  • 控制台日志中的时间戳。
  • ANSI颜色输出。
  • 在1分钟的超时构建阶段,2分钟超时的测试阶段。

声明式管道如下所示。

  1. pipeline { 
  2.     agent any 
  3.  
  4.     options { 
  5.         timestamps() 
  6.         ansiColor("xterm"
  7.     } 
  8.  
  9.     stages { 
  10.         stage("Build") { 
  11.             options { 
  12.                 timeout(time: 1, unit: "MINUTES"
  13.             } 
  14.             steps { 
  15.                 sh 'printf "\\e[31mSome code compilation here...\\e[0m\\n"' 
  16.             } 
  17.         } 
  18.  
  19.         stage("Test") { 
  20.             options { 
  21.                 timeout(time: 2, unit: "MINUTES"
  22.             } 
  23.             steps { 
  24.                 sh 'printf "\\e[31mSome tests execution here...\\e[0m\\n"' 
  25.             } 
  26.         } 
  27.     } 

让我们运行它。


这是控制台日志。

  1. Started by user Szymon Stepniak 
  2. Running in Durability level: MAX_SURVIVABILITY 
  3. [Pipeline] Start of Pipeline 
  4. [Pipeline] node 
  5. Running on Jenkins in /home/wololock/.jenkins/workspace/pipeline-sandbox 
  6. [Pipeline] { 
  7. [Pipeline] timestamps 
  8. [Pipeline] { 
  9. [Pipeline] ansiColor 
  10. [Pipeline] { 
  11. [Pipeline] stage 
  12. [Pipeline] { (Build) 
  13. [Pipeline] timeout 
  14. 15:10:04  Timeout set to expire in 1 min 0 sec 
  15. [Pipeline] { 
  16. [Pipeline] sh 
  17. 15:10:04  + printf '\e[31mSome code compilation here...\e[0m\n' 
  18. 15:10:04  Some code compilation here... 
  19. [Pipeline] } 
  20. [Pipeline] // timeout 
  21. [Pipeline] } 
  22. [Pipeline] // stage 
  23. [Pipeline] stage 
  24. [Pipeline] { (Test) 
  25. [Pipeline] timeout 
  26. 15:10:04  Timeout set to expire in 2 min 0 sec 
  27. [Pipeline] { 
  28. [Pipeline] sh 
  29. 15:10:05  + printf '\e[31mSome tests execution here...\e[0m\n' 
  30. 15:10:05  Some tests execution here... 
  31. [Pipeline] } 
  32. [Pipeline] // timeout 
  33. [Pipeline] } 
  34. [Pipeline] // stage 
  35. [Pipeline] } 
  36. [Pipeline] // ansiColor 
  37. [Pipeline] } 
  38. [Pipeline] // timestamps 
  39. [Pipeline] } 
  40. [Pipeline] // node 
  41. [Pipeline] End of Pipeline 
  42. Finished: SUCCESS 

在声明性管道中,选项与管道脚本逻辑分开。该脚本管道也支持timestamps,ansiColor和timeout选项,但它需要一个不同的代码。这是使用脚本化管道表达的相同管道。

  1. node { 
  2.     timestamps { 
  3.         ansiColor("xterm") { 
  4.             stage("Build") { 
  5.                 timeout(time: 1, unit: "MINUTES") { 
  6.                     sh 'printf "\\e[31mSome code compilation here...\\e[0m\\n"' 
  7.                 } 
  8.             } 
  9.             stage("Test") { 
  10.                 timeout(time: 2, unit: "MINUTES") { 
  11.                     sh 'printf "\\e[31mSome tests execution here...\\e[0m\\n"' 
  12.                 } 
  13.             } 
  14.         } 
  15.     } 

我想你看到了问题。在这里,我们仅使用timestamps和ansiColorJenkins插件。想象再添加一个或两个插件。声明式与脚本式,3:0。

4.用when块跳过阶段。

在此博客文章中我最后要提到的是when声明性管道支持的块。让我们改进前面的示例并添加以下条件:

  • 仅在等于时执行测试阶段。env.FOO``bar

这是声明性管道代码的外观。

  1. pipeline { 
  2.     agent any 
  3.  
  4.     options { 
  5.         timestamps() 
  6.         ansiColor("xterm"
  7.     } 
  8.  
  9.     stages { 
  10.         stage("Build") { 
  11.             options { 
  12.                 timeout(time: 1, unit: "MINUTES"
  13.             } 
  14.             steps { 
  15.                 sh 'printf "\\e[31mSome code compilation here...\\e[0m\\n"' 
  16.             } 
  17.         } 
  18.  
  19.         stage("Test") { 
  20.             when { 
  21.                 environment name"FOO", value: "bar" 
  22.             } 
  23.             options { 
  24.                 timeout(time: 2, unit: "MINUTES"
  25.             } 
  26.             steps { 
  27.                 sh 'printf "\\e[31mSome tests execution here...\\e[0m\\n"' 
  28.             } 
  29.         } 
  30.     } 

然后执行它。


该测试如预期阶段被跳过。现在,让我们尝试在脚本化管道示例中执行相同的操作。

  1. node { 
  2.     timestamps { 
  3.         ansiColor("xterm") { 
  4.             stage("Build") { 
  5.                 timeout(time: 1, unit: "MINUTES") { 
  6.                     sh 'printf "\\e[31mSome code compilation here...\\e[0m\\n"' 
  7.                 } 
  8.             } 
  9.             if (env.FOO == "bar") { 
  10.                 stage("Test") { 
  11.                     timeout(time: 2, unit: "MINUTES") { 
  12.                         sh 'printf "\\e[31mSome tests execution here...\\e[0m\\n"' 
  13.                     } 
  14.                 } 
  15.             } 
  16.         } 
  17.     } 

如您所见,我们必须使用if-condition来检查是否env.FOO等于bar,然后才添加Test阶段。(不幸的是,这并不是真正的跳过。)让我们运行它,看看结果如何。


这是不同的结果。在脚本化管道用例中,甚至不会呈现“ 测试”阶段。在我看来,这可能会带来一些不必要的混乱,声明性管道会更好地处理它。声明式与脚本式,4:0。

结论

这是我在声明性和脚本化Jenkins管道之间的四大区别。这些不是唯一的区别,我想您的列表可能看起来有些不同。你的选择是什么?您更喜欢声明性管道还是脚本化管道?

责任编辑:姜华 来源: DevOps云学堂
相关推荐

2020-12-17 07:59:46

声明式代码命令式代码代码

2022-10-08 15:32:24

Python开发技巧

2019-03-22 08:25:47

沙箱网络安全恶意软件

2021-08-24 10:16:51

人工智能机器人工具

2009-06-22 09:01:57

Spring声明式事务

2022-06-21 08:12:17

K8sAPI对象Kubernetes

2024-01-29 14:46:22

分布式计算云计算边缘计算

2023-08-11 17:26:51

Pandas数据分析Python

2023-11-15 16:37:30

ChatGPT人工智能

2013-06-27 09:31:37

声明式编程命令式编程编程

2021-10-08 19:00:28

NMState网络配置工具系统运维

2009-06-22 11:01:12

2011-07-03 21:22:05

2015-10-19 11:41:30

分布式存储HDFSGFS

2020-09-04 06:27:22

编码命令式声明式代码

2024-02-23 18:17:57

Python脚本开发

2019-05-21 14:14:18

KafkaRabbitMQRocketMQ

2023-04-12 09:00:17

KafkaConsumerMQ

2009-10-29 10:44:18

ADO.NET Dat

2013-03-18 13:31:28

点赞
收藏

51CTO技术栈公众号