[From] https://testerhome.com/topics/10328
前言
最近由於項目需要,接觸到了Jenkins 2.0版本,其中最重要的特性就是提供了對pipeline的支持。
簡單的來說,就是把Jenkins1.0版本中,Project中的相關配置信息,如SVN/Git的配置,Parameter的配置等都變成Code,即Pipeline as Code。
這樣的優勢為可以通過寫代碼的形式配置Project,且Jenkins中內置了常用的steps。實現了構建步驟代碼化、構建過程視圖化。
其他的Jenkins基礎這里不多說了,這里主要介紹最近遇到的問題及其處理方法。一方面是自己總結和整理一下,另一方面也可以供他人參考,少踩坑。
選擇Declarative Pipeline還是Scripted Pipeline
最開始的Pipeline plugin,支持的只有一種腳本類型,就是Scripted Pipeline;
Declarative Pipeline為Pipeline plugin在2.5版本之后新增的一種腳本類型,與原先的Scripted Pipeline一樣,都可以用來編寫腳本。
使用哪一種腳本格式呢,我又糾結了,也查詢了些資料。
https://stackoverflow.com/questions/43484979/jenkins-scripted-pipeline-or-declarative-pipeline
http://jenkins-ci.361315.n4.nabble.com/Declarative-pipelines-vs-scripted-td4891792.html
最后,我還是選擇了Declarative Pipeline,這也是后續Open Blue Ocean所支持的類型。
相對而言,Declarative Pipeline比較簡單,如果Groovy很熟的,用Scripted Pipeline可能更順手。
另外,Declarative Pipeline中,是可以內嵌Scripted Pipeline代碼的。
設置和獲取執行參數
原先在Jenkins 1.0的時候,常用的一個設置就是“ "This build is parameterized",通過獲取參數值,執行后續相關的判斷及操作。
在pipeline中,可以這樣設置:
#!/usr/bin/env groovy pipeline{ agent none options{ disableConcurrentBuilds() skipDefaultCheckout() timeout(time: 1, unit: 'HOURS') timestamps() } parameters{ string(name: 'PERSON', defaultValue: 'among中文', description: '請輸入中文') booleanParam(name: 'YESORNO', defaultValue: true, description: '是否發布') } stages{ stage('test stage') { agent { label 'master' } steps { echo 'Hello, stage1' echo "Hello ${params.PERSON}" echo "Hello ${env.PERSON}" scrip { def input = params.YESORNO if (input) { echo "you input is ${input},to do sth" } else { echo "you input is ${input},nothing to do" } } } } }
環境變量的問題
通過Jenkins 執行相關sh的時候,環境變量中,不會默認繼承/etc/profile 和 ~/.profile 等環境變量。
這個時候就很麻煩了,尤其在一些依賴環境變量操作的sh腳本時。
可以這樣來做,一是在增加node節點時,自己設置環境變量,如:
也可以在代碼中這么寫。寫 withEnv ,或是直接在shell中先source profile文件。然后在執行相關命令。
steps { withEnv(['TPS=amtps']) { // do sth } // sh 'source /etc/profile && source ~/.bash_profile && env' dir('/root') { sh '(source /etc/profile;source ~/.bash_profile;sh ./ee.sh)' } }
Jenkins中nohup后進程還是起不來的問題
在普通的shell環境中,nohup,並且& 某個程序后,會拋到后台執行,在退出當前shell環境后,程序依然可以執行。
但是在Jenkins中,通過nohup,且使用&之后,step結束后,執行的程序還是會退出,導致程序起不來。
嘗試和驗證了很多方法,后面都是這樣解決的。
修改JENKINS_NODE_COOKIE的值,這樣后續結束的時候,后面的sh程序就不會被kill掉了。
適用版本:Jenkins 2.46版本,版本如差異較大,可能不一致。當時為了解決這個問題,折騰了很久,找的資料也比較老了,很多都沒用,特定記錄一下。
steps { sh 'JENKINS_NODE_COOKIE=dontKillMe nohup python3 /home/among/pj/my_py/monitor/amon/amon.py >/tmp/run.log 2>&1 &' }
shell出錯后繼續,取shell輸出值。
這2個比較簡單,看例子就知道了。
steps { sh returnStatus: true, script: "ps -ef|grep amon|grep -v grep|awk '{print \$2}'|xargs kill -9" script { def pid = sh returnStdout: true ,script: "ps -ef|grep amon|grep -v grep|awk '{print \$2}'" pid = pid.trim() echo "you input pid is ${pid},to do sth" sh "kill -9 ${pid}" } }
以上就是最近遇到的一些問題,后續遇到了,我再補充吧。
一些地方有可能存在問題或有更好的解決方法,歡迎大家提出和完善。

