Jira 集成 Jenkins & BitBucket

Smart Commit使用教程 CI Builds

Posted by zhida on May 21, 2017

前言

本文目的:通过 Jira + Jenkins + BitBucket的集成,按照本文的步骤配置,可以做到 :

  • BitBucket代码提交能够关联相应的Jira issue,并且更改issue的属性和流程
  • BitBucket的代码提交能够触发相应的Jenkins构建
  • Jenkins的构建完成之后,将结果体现在Jira中,并且根据不同的结果更改Jira的issue属性

各个系统的版本如下:

Jira 7.5
Jenkins 2.62.3
BitBucket 5.4.0

BitBucket

申请一个账号,然后建立一个仓库jira_erp,做相应的设置:

  • BitBucket settings - Oauth - add comsumer - generate key
  • BitBucket -> Repository settings -> Access Keys -> Add key
  • BitBucket -> Repository settings -> issue tracker -> public Issue tracker
  • BitBucket -> Repository settings -> Links -> Add new link -> 填写相应的Jira地址(http://jira.paraller.com:9090/browse/\1)
  • BitBucket -> Repository settings -> Links -> 开启 Require issue keys in commit messages
  • BitBucket -> Repository settings -> Webhooks -> ACTIVE jenkin_ci http://jenkins.paraller.com:8081/buildByToken/build?job=jira_test&token=paraller

Jenkins

进入系统设置 ->插件管理 , 安装以下五个插件:

  • JiraTestResultReporter plugin:2.0.4
  • JIRA Issue Updater:1.18
  • Jira plugin:2.4.2
  • build-token-root.hpi: Build Authorization Token Root Plugin:1.4
  • JIRA Integration for Jenkins:3.0.1

注意事项

1、JIRA Integration for Jenkins 这个插件版本要和Jira的 Jenkins Integration for Jira插件版本对应 ,不然在Jira系统的 Jenkins Configuration界面会提示 Jenkins系统没有安装相应的插件

The plugin JIRA Plugin for Jenkins doesn’t seem to be installed on ‘jenkins’. Consider installing it to optimize the integration with JIRA and allow the use of Jenkins side features.Download it HERE and install it using the upload method 

Jenkins Integration for JIRA - Release Notes

2、安装的时候可能会提示 IOException ,no permission to /var/jenkins_home/**.xml这类的错误,删除这个文件即可

3、可能会报Nginx: 413 Request Entity Too Large Error. Add the following line to http or server or location context to increase the size limit in nginx.conf, enter:

client_max_body_size 2M;

4、安装完成之后在 系统管理中会出现灯泡💡图标,显示如下: image

5、点击 💡图标 JIRA Integration for jenkins, 正常情况下会出现JIRA的URL。

设置信息

系统管理 - 系统设置 - JIRA

配置如下:要勾选 Update Relevant Jira issue For All Build result , isssue-key的正则:([a-zA-Z][a-zA-Z0-9_]+)-([1-9][0-9]*)-(\w*)

image

demo项目设置
  • 源码管理: 选择Git,地址填写SSH地址,证书Credentials可以选择用户名密码 / ssh key 验证

  • 构建触发器:触发远程构建构建,设置Token: paraller

image

Jira

基本步骤

1、安装:Jenkins Integration for Jira:v3.0.2

2、进入 Project settings -> Development tools -> connect -> BitBucket Cloud ;填写相应的 UserAccount(账户名不是邮件名)/Key/Secret , 勾选以下两项

  • Auto Link New Repositories : 在 BitBucket中可以跳转到Jira
  • Enable Smart Commits : 开启Issue link Code

3、回到Project检测效果

  • 新建一个issue,右边的面板 有一个development字段
  • 点击create branch,会跳转到 BitBucket界面自动创建一个分支,针对issue的代码在该分支填写
  • 在提交代码的时候, git commit -m 'IssueID #comment' 将会自动关联在一起

Note:如果使用BitBucket云端会有延迟,大概二十分钟的样子才会关联在一起

集成Jenkins系统
  • 在管理 - 应用程序面板中,点击 集成-Jenkins Configuration,点击右上角的 Add Site,弹出一个框,按照提示填写相应的信息
  • RPC URL / Display URL :填写Jenkins的首页地址 : jenkins.paraller.com ; Username / Token : 填写有权限的账号密码
  • Advanced Configuration。 配置Jira的回调地址: jira.paraller.com
  • 要对Enable框进行勾选
Smart Commit 语法

支持三大类语法,分别是 comment / time / transition, 可以混搭

Comment

gcmsg 'ZTP-5-t1 #comment hello world'

time:能够根据这个时间,自动更改Jira的时间状态。

gcmsg 'ZTP-5-t1  #time 2d 5h #comment Task completed ahead of schedule '

work flow transition :这个命令需要和工作流关联在一起,你可以在Jira的工作流中查看你当前的项目有什么工作流, 如果有 TO DO / DONE , 你就可以使用 #done #todo 来更改任务的状态。

gcmsg 'ZTP-5-t1 #done test done'

测试

1、提交代码:注意事项: commit的 Jira issue key 必须是完整的,不能是 MAYD-2这种格式

repo1 git:(MAYD-2-t2):  echo 'l' >> a.md && gaa && gcmsg 'MAYD-2-t2 #comment hello' && git push origin MAYD-2-t2:MAYD-2-t2

2、Jenkins日志输出

Building in workspace /var/jenkins_home/workspace/jira_test
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://112.95.**.**:7990/scm/demo/repo1.git # timeout=10
Fetching upstream changes from http://112.95.**.**:7990/scm/demo/repo1.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress http://112.95.**.**:7990/scm/demo/repo1.git +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/DEM-1-bug1
Seen branch in repository origin/DEM-2-testb
Seen branch in repository origin/DEM-6-test
Seen branch in repository origin/DEM-7-monday
Seen branch in repository origin/DEM-7-mondy
Seen branch in repository origin/DEM-8-mayday
Seen branch in repository origin/MAYD-1-t1
Seen branch in repository origin/MAYD-2-t2
Seen branch in repository origin/master
Seen branch in repository zhida.liao/DEM-1-bug1
Seen branch in repository zhida.liao/DEM-2-testb
Seen branch in repository zhida.liao/DEM-6-test
Seen branch in repository zhida.liao/DEM-7-monday
Seen branch in repository zhida.liao/DEM-7-mondy
Seen branch in repository zhida.liao/DEM-8-mayday
Seen branch in repository zhida.liao/MAYD-1-t1
Seen branch in repository zhida.liao/MAYD-2-t2
Seen branch in repository zhida.liao/master
Seen 18 remote branches
 > git tag -l # timeout=10
Checking out Revision 528b8b953e352ac1a57616cfedb54d4a315cf292 (origin/MAYD-2-t2)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 528b8b953e352ac1a57616cfedb54d4a315cf292
Commit message: "MAYD-2-t2 #comment hello"
 > git rev-list 42efe8ee7188a6d870b26331780b0a1cc36effcd # timeout=10
[jira_test] $ /bin/sh -xe /tmp/jenkins7271595388793139983.sh
+ echo hello
hello
Notified JIRA that a build has completed.
Finished: SUCCESS

3、Jira - 应用程序 - Jenkins Configuration:能自动刷新Jenkins的任务,因为插件的原因。

image

4、Jira issue的 Ci builds会出现构建结果 image

常见问题

  • CI Builds 一直都没有活动信息: 去到 JIRA - JenKins Configuration 右上角的按钮 Actions - Clear Deleted Jobs from Cache ,然后选择 refresh Job List
  • CI Builds 没有自动同步 JenKins 构建信息: 去到 JIRA - JenKins Configuration,手动点击 Sync Job 刷新按钮
  • 是否可以 Jenkins构建完成自动同步? 这块功能不稳定,不会立即同步,但是间隔一段时间之后会自动同步。

参考网站