自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

總結(jié):四個Pipeline腳本式與聲明式語法差異

開發(fā) 前端
如果您閱讀此博客文章,則很有可能正在尋找有關(guān)腳本化和聲明性管道之間的實際差異的信息,對嗎?那你找不到更好的地方了。我將向您展示這兩者之間的四個最實際的區(qū)別。和我待幾分鐘,享受旅程!

[[374535]]

 如果您閱讀此博客文章,則很有可能正在尋找有關(guān)腳本化和聲明性管道之間的實際差異的信息,對嗎?那你找不到更好的地方了。我將向您展示這兩者之間的四個最實際的區(qū)別。和我待幾分鐘,享受旅程!

為什么要有兩種管道類型?

  • 腳本化管道是Jenkins中作為代碼的管道的第一個實現(xiàn)。即使它使用底層的管道子系統(tǒng),它還是或多或少地設(shè)計為使用Groovy構(gòu)建的通用DSL。這意味著它不具有固定的結(jié)構(gòu),并且由您決定如何定義管道邏輯。
  • 聲明性管道更自以為是,其結(jié)構(gòu)是明確定義的??赡芸雌饋碛行┚窒?。

但實際上,您可以使用腳本化或聲明性管道來實現(xiàn)相同的目的。那么選擇哪一個呢?如果您問我這個問題,我會說使用聲明性管道。以下內(nèi)容這就是為什么。

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.     } 

如果我們嘗試運(yùn)行以下管道,則驗證將很快使構(gòu)建失敗。該日志顯示只能與觸發(fā)String參數(shù),所以我們得到這樣的錯誤。


請注意,管道沒有執(zhí)行任何階段,只是失敗了。這可能為我們節(jié)省了很多時間-想象一下執(zhí)行Build階段幾分鐘,而只是獲取echo步驟希望得到的信息java.lang.String而不是java.lang.Integer。

現(xiàn)在,讓我們看一下與該示例等效的腳本管道。

  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.     } 

該管道執(zhí)行相同的階段和相同的步驟。但是,有一個明顯的區(qū)別。讓我們執(zhí)行它,看看它產(chǎn)生什么結(jié)果。


它按預(yù)期失敗。但是這次是執(zhí)行Build階段,也是Test階段的第一步。如您所見,沒有驗證管道代碼。在這種情況下,聲明式管道可以更好地處理此類用例。

2.從指定步驟重新開始

聲明式管道具有的另一個很酷的功能是“從階段重新啟動”。讓我們修復(fù)上一個示例中的管道,看看是否只能重新啟動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.     } 

讓我們執(zhí)行它。


在這里您可以看到已選擇測試階段。在右側(cè)的步驟列表上方,有一個名為“重新啟動測試”的選項。讓我們單擊它并查看結(jié)果。


如您所見,Jenkins跳過了Build階段(它使用了先前構(gòu)建中的工作空間),并從Test階段開始了下一個管道執(zhí)行。當(dāng)您執(zhí)行一些外部測試并且由于遠(yuǎn)程環(huán)境的某些問題而導(dǎo)致測試失敗時,這可能會很有用。您可以使用測試環(huán)境解決問題,然后重新運(yùn)行該階段,而無需重建所有工件。(在這種情況下,應(yīng)用程序的代碼未更改。)

現(xiàn)在,讓我們看一下腳本化管道示例。

  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塊

兩種管道類型都支持第三個功能,但是我認(rèn)為聲明性管道更好地處理了它。假設(shè)我們將以下功能添加到上一個管道中。

  • 控制臺日志中的時間戳。
  • ANSI顏色輸出。
  • 在1分鐘的超時構(gòu)建階段,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.     } 

讓我們運(yùn)行它。


這是控制臺日志。

  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選項,但它需要一個不同的代碼。這是使用腳本化管道表達(dá)的相同管道。

  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聲明性管道支持的塊。讓我們改進(jìn)前面的示例并添加以下條件:

  • 僅在等于時執(zhí)行測試階段。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.     } 

然后執(zhí)行它。


該測試如預(yù)期階段被跳過。現(xiàn)在,讓我們嘗試在腳本化管道示例中執(zhí)行相同的操作。

  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階段。(不幸的是,這并不是真正的跳過。)讓我們運(yùn)行它,看看結(jié)果如何。


這是不同的結(jié)果。在腳本化管道用例中,甚至不會呈現(xiàn)“ 測試”階段。在我看來,這可能會帶來一些不必要的混亂,聲明性管道會更好地處理它。聲明式與腳本式,4:0。

結(jié)論

這是我在聲明性和腳本化Jenkins管道之間的四大區(qū)別。這些不是唯一的區(qū)別,我想您的列表可能看起來有些不同。你的選擇是什么?您更喜歡聲明性管道還是腳本化管道?

責(zé)任編輯:姜華 來源: DevOps云學(xué)堂
相關(guān)推薦

2020-12-17 07:59:46

聲明式代碼命令式代碼代碼

2022-10-08 15:32:24

Python開發(fā)技巧

2019-03-22 08:25:47

沙箱網(wǎng)絡(luò)安全惡意軟件

2021-08-24 10:16:51

人工智能機(jī)器人工具

2024-06-04 00:00:06

Python初學(xué)者優(yōu)化

2009-06-22 09:01:57

Spring聲明式事務(wù)

2022-06-21 08:12:17

K8sAPI對象Kubernetes

2011-07-03 21:22:05

2024-01-29 14:46:22

分布式計算云計算邊緣計算

2023-08-11 17:26:51

Pandas數(shù)據(jù)分析Python

2023-11-15 16:37:30

ChatGPT人工智能

2013-06-27 09:31:37

聲明式編程命令式編程編程

2022-02-23 15:09:18

數(shù)字化轉(zhuǎn)型國有企業(yè)數(shù)據(jù)

2009-10-29 10:44:18

ADO.NET Dat

2021-10-08 19:00:28

NMState網(wǎng)絡(luò)配置工具系統(tǒng)運(yùn)維

2019-05-21 14:14:18

KafkaRabbitMQRocketMQ

2023-04-12 09:00:17

KafkaConsumerMQ

2009-06-22 11:01:12

2015-10-19 11:41:30

分布式存儲HDFSGFS

2024-02-23 18:17:57

Python腳本開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號