持续集成利器-PIPELINE(二)-Multibranch Pipeline 实现feature branch的持续集成

来源:互联网 发布:淘宝男模特收入 编辑:程序博客网 时间:2024/06/06 05:29

实战操作-创建第一个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://git@stash.bbpd.io/~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 testSwitched 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 testTotal 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/testremote:To ssh://stash.bbpd.io/~ema/minimalpipelineproject.git * [new branch]      test -> testemambp: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://git@stash.bbpd.io/~ema/minimalpipelineproject.git (fetch)
origin ssh://git@stash.bbpd.io/~ema/minimalpipelineproject.git (push)
upstream ssh://git@stash.bbpd.io/bbcloud-sandbox/minimalpipelineproject.git (fetch)
upstream ssh://git@stash.bbpd.io/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 testDeleted branch test (was 63ffc0c).

1min后,发现test job还在。。。。。。。
查看log:

[Wed Sep 06 09:46:34 CST 2017] Finished branch indexing. Indexing took 12 secEvaluating orphaned items in mutibranch-test-pipelineWill not remove msf as it is only #1 in the listWill not remove test as it is only #2 in the listWill not remove msf because it is newWill 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的持续集成精髓。

原创粉丝点击