在Jenkins配置github的pull request builder

最近做的一个项目中使用了github的enterprise版本,然后之前的一位同事搭建了在github上提交pull request可以出发jenkins builder。

学习了这一过程,记录一下blog。


准备实验项目和jenkins

代码使用openstack的python-cinderclient为例:

下载代码,运行这个项目的单元测试命令tox,保证单元测试能够正常通过,这是一个python的项目,运行了py34 和py27,pep8的检查,运行结果如下:

1
2
3
4
5
6
......
_________________ summary _________________
py34: commands succeeded
py27: commands succeeded
pep8: commands succeeded
congratulations :)

然后准备jenkins,这里的build任务是在slave节点上运行的,slave安装的是centos7。

在jenkins中安装以下插件:

然后在slave节点上安装python的运行环境

1
2
3
4
5
6
7
8
9
10
11
# EPEL
yum -y install epel-release
# Development packages
yum -y groupinstall "Development tools"
yum -y install openssl-devel readline-devel bzip2-devel sqlite-devel zlib-devel ncurses-devel db4-devel expat-devel
yum -y install python-devel mysql-devel libxslt-devel libffi-devel
yum -y install libmemcached-devel
yum -y install libevent-devel
yum -y python-virtualenv
# Python 34
yum -y install python34 python34-devel

在slave节点上,切换到jenkins的账号

1
2
# Enter bash using jenkins users
su -s /bin/bash jenkins

然后需要将github的站点添加到slave node的knowhost里面,通过运行ssh git@github.com可以添加。


在jenkins上配置pull request builder

在jenkins中创建一个freestyle project, 命名为python-cinderclient-pr-builder。

注:我这个项目是创建在github的enterprise中的,可以等同于github。主要原因是我的jenkins没有外网ip,github无法访问,就不能设置webhook,无法实现根据pull request自动trigger build。

  1. 填入project url

  1. 配置pull request build
  • 安装了GitHub Pull Request Builder Plugin之后

  • Go to Manage Jenkins -> Configure System -> GitHub pull requests builder section

    如图下图配置

    需要注意:GitHub API URI是“https://api.github.com”. 而GitHub Enterprise API URL使用路径“/api/v3”。

  1. 添加Personal access tokens

进入github的个人的profile页面,然后添加Personal access tokens的页面中,generate new token,如下图选择,重点是要选admin:repo_hook

  1. 添加credential的配置 生成token以后,在jenkins的creadentials里面添加一个新的证书,如图,password项就填刚才在github上生成的token


添加一个jenkins job

参考以下步骤,这个步骤来自于ghprb插件的README

  • Create a new job.
  • Add the project’s GitHub URL to the GitHub project field (the one you can enter into browser. eg: https://github.com/janinko/ghprb)
  • Select Git SCM.
  • Add your GitHub Repository URL.
  • Under Advanced, set Name to origin and:
    • If you just want to build PRs, set refspec to +refs/pull/*:refs/remotes/origin/pr/*
    • If you want to build PRs and branches, set refspec to +refs/heads/*:refs/remotes/origin/* +refs/pull/*:refs/remotes/origin/pr/* (see note below about parameterized builds)
  • In Branch Specifier, enter ${sha1} instead of the default */master.
  • If you want to use the actual commit in the pull request, use ${ghprbActualCommit} instead of ${sha1}
  • Under Build Triggers, check GitHub pull requests builder.
    • Add admins for this specific job.
    • If you want to use GitHub hooks for automatic testing, read the help for Use github hooks for build triggering in job configuration. Then you can check the checkbox.
    • In Advanced, you can modify:
      • The crontab line for this specific job. This schedules polling to GitHub for new changes in Pull Requests.
      • The whitelisted users for this specific job.
      • The organisation names whose members are considered whitelisted for this specific job.
  • Save to preserve your changes.

更具体的内容可以参考:

完成以上的操作,这个插件就会在github项目的hooks和services里面自动添加一个webhook,如图:


添加build status description

添加一个在post build action中选择set build status on Github commit

然后在每一个build就会有一个status


Mattermost

安装这个插件:mattermost Plugin wiki

然后在matter most -> Integrations -> incoming webhooks, 里面配置一个incoming webhooks, 如图

配置完成以后,就会生成一个webhooks如图:

之后在jenkins的job的post-build action里面配置mattermost插件如图:

之后,如果这个job又build成功的消息的话,就会在mattermost里面产生一个消息如图:

参考:

mattermost-plogin on github

webhooks-incoming


一些有用的jenkins插件


Reference

install jenkins on centos