持續集成利器-PIPELINE(二)-Multibranch Pipeline 實現feature branch的持續集成

實戰操作-創建第一個pipeline JOB

由於初到BB,擔心錯誤操作,所以本地部署了一個jenkins 系統(只有master),見上篇http://blog.csdn.net/hubanbei2010/article/details/77368207
在本地的Jenkins上做實驗,就不怕了
repo採用的fork的一個實驗repo
SCM我司採用bitbucket

案例:
創建一個muti-branch pipeline ,執行任務“打印env信息”
pipeline 名字:mutibranch-test-pipeline
repo:ssh://[email protected]/~ema/minimalpipelineproject.git
branch: 2個 master feature/fclausen
觸發方式:選2,時長 1min

0,準備工作,本地的jenkins-master需要與bitbucket通過ssh通信,保證jenkins從SCM上可以pull代碼。
ssh前提-scm與jenkins鑑權操作

  • 登錄jenkins-master 機器上,切換到jenkins用戶 ,執行命令ssh-keygen -t rsa 生成私鑰公鑰對
  • 在jenkins中 創建credential 全局信息時添加私鑰信息,(可用於build/ pipeline/test等任何類型的job)
  • 在對應的bitbucket的賬戶設置中,添加公鑰信息。

1,新建Multibran Pipeline 類型的job
配置pipeline name/repo/觸發時間間隔
會自動拉取該repo在scm服務器上的所有branch(master和feature/fclausen) 並分別創建job,如下
2,新建分支test

emambp:minimalpipelineproject ema$ git checkout -b test
Switched to a new branch 'test'

3,vim修改Jenkinsfile

#!groovy
// -*- mode: groovy -*-
node {
sh 'env > env.txt'
sh 'echo hello'
readFile('env.txt').split("\r?\n").each {
println it
}
}

4,修改後的Jenkinsfile commit後,push到遠端,

emambp:minimalpipelineproject ema$ git push origin test
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for test:
remote:   https://stash.bbpd.io/users/ema/repos/minimalpipelineproject/compare/commits?sourceBranch=refs/heads/test
remote:
To ssh://stash.bbpd.io/~ema/minimalpipelineproject.git
 * [new branch]      test -> test
emambp:minimalpipelineproject ema$

由於test分支爲新建分支,
等到1min定時器觸發,則mutibranch-test-pipeline job下新生成一個build job
在status頁面可以看到 branch部分顯示3個
這裏寫圖片描述

5,查看mutibranch-test-pipeline job 運行日誌“Scan Multibranch Pipeline Log”

查看test job的日誌
這裏寫圖片描述

由此可見:env 信息最後是體現在job 日誌中,而不在pipeline日誌中
pipeline日誌中包含:

  • 掃描changelog的信息
  • 是否需要新建或刪除feature branch的對應job

6,刪除遠端分支test (git push origin –delete test)

emambp:minimalpipelineproject ema$ git branch -a
* master
test
remotes/origin/HEAD -> origin/master
remotes/origin/feature/fclausen
remotes/origin/master
remotes/origin/test
emambp:minimalpipelineproject ema$ git remote -v
origin ssh://[email protected]/~ema/minimalpipelineproject.git (fetch)
origin ssh://[email protected]/~ema/minimalpipelineproject.git (push)
upstream ssh://[email protected]/bbcloud-sandbox/minimalpipelineproject.git (fetch)
upstream ssh://[email protected]/bbcloud-sandbox/minimalpipelineproject.git (push)
emambp:minimalpipelineproject ema$ git push origin --delete test
To ssh://stash.bbpd.io/~ema/minimalpipelineproject.git
- [deleted] test
emambp:minimalpipelineproject ema$ git branch -a
* master
test
remotes/origin/HEAD -> origin/master
remotes/origin/feature/fclausen
remotes/origin/master

1min後,查看 pipeline status信息,發現test分支對應的job 暫時不可用,而不是直接刪除掉,奇怪。。。。
這裏寫圖片描述

7,試想是不是本地branch還存在的原因,但理論上,不應該啊 ,jenkins只會檢測remote的分支。但還是嘗試一下,然後再刪除本地test分支,此時只能強制刪除

emambp:minimalpipelineproject ema$ git branch -D test
Deleted branch test (was 63ffc0c).

1min後,發現test job還在。。。。。。。
查看log:

[Wed Sep 06 09:46:34 CST 2017] Finished branch indexing. Indexing took 12 sec
Evaluating orphaned items in mutibranch-test-pipeline
Will not remove msf as it is only #1 in the list
Will not remove test as it is only #2 in the list
Will not remove msf because it is new
Will not remove test because it is new

反覆嘗試新建/刪除好多次,仍舊如此。。臨近下班,本想開開心心的完成blog,結果最後沒吃晚飯,熬到晚上八點 也沒得到答案。準備第二天來求助。。有時候troubleshooting 不能一時太堅持,應該稍休息,說不定第二天思路一下就打開了。

第二天到公司,求助老美mentor ,他也是給了我一些簡單的debug,無解。
後建議可以嘗試Google一把。。

我抱着忐忑的心情,google一下“Will not remove because it is new pipeline jenkins”,結果很多類似的issue。
嘗試第一條答案https://issues.jenkins-ci.org/browse/JENKINS-35173,pipeline的配置,關於“Discard old items” ,應該設置爲0.否則,只要有歷史job存在,jenkins系統就不會刪除該feature branch
的job。只是置爲disable。
8,修改pipeline配置, 改變爲空
修改前:
這裏寫圖片描述
修改後:
這裏寫圖片描述
再次運行pipeline,查看status,終於正常了,雖然有點小插曲。
這裏寫圖片描述

這就是用multibranch pipeline 實現feature branch的持續集成精髓。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章