最近在学习buildbot的用法,在不断的尝试和阅读手册的过程中,总结出下面的一些内容,做个备忘,有需要的同学也可以看看。内容是我自己的理解,可能有错的地方,大家可以帮忙纠正。
这个master.cfg的文件是python的写法。主要在里面配置了各种字典信息。分为以下几个部分:workers,protocol,changesources,schedulers,builders,project identity,buildbot services,DB url。
而整个buildbot的动作过程和这个文件密切相关。大体过程就是:
changesource检测到变化,触发对应schedule,schedule指派对应的builder来处理,builder安排自己的手下worker去执行定义好的factory。最后buildbot收集到了结果,通过service 报告。
# -*- python -*-
# ex: set filetype=python:
from buildbot.plugins import *
# It must be installed as 'master.cfg' in your buildmaster's base directory.
# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
worker部分,这个部分定义了buildbot的workers,所有要接到这个master上的workers必须在这里预先定义好。定义workers,需要给workers配置一个名字和密码。
####### WORKERS
# The 'workers' list defines the set of recognized workers. Each element is
# a Worker object, specifying a unique worker name and password. The same
# worker name and password must be configured on the worker.
worker_win10=worker.Worker("win10", "pass")
worker_ubuntu=worker.Worker("ubuntu", "pass")
worker_ubuntu1=worker.Worker("ubuntu1", "pass")
c['workers'] =[]
c['workers']=[worker_win10,worker_ubuntu,worker_ubuntu1]
这个是master用来和worker通讯用的协议。这里我们可以不用做修改,就使用默认值就可以了,只要这个端口是开放的,没有被防火墙拦截。
####### PROTOCOLS
# 'protocols' contains information about protocols which master will use for
# communicating with workers. You must define at least 'port' option that workers
# could connect to your master with this protocol.
# 'port' must match the value configured into the workers (with their
# --master option)
c['protocols'] = {'pb': {'port': 9989}}
changesource是一个触发器。buildbot的build过程大略分两种,一种由changesource监测变化触发,一种是配置文件设定好的周期性的运行。如果是周期性运行,可以不用配置changesource。
一个master可以同时监测多个工程的变化,这个时候我们可以建立多个changesource。注意了,在建立不同的changesource时,我们需要用标记来区分是哪个工程,因此这里引入一个参数project。project的值自己定义,例如下面的“HelloWorld”,只要能够标记不同的工程即可。另外,如果我们通过这种方式来定义不同的changesource的话,我们的repourl就必须写到我们的工程路径,不可以只写到trunk,否则我们这里的“HelloWorld”这个changesource就对应错了。那我们这个project参数,这里定义了以后,Scheduler如何对应上呢?同样在schedule中也加入project参数,参数一致即可。
####### CHANGESOURCES
# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes. Here we point to the buildbot version of a python hello-world project.
changesource_HelloWorld=changes.SVNPoller(repourl="file:///F:/localSVN/trunk/HelloWorld",
svnuser=None, svnpasswd=None,pollInterval=10,
project='HelloWorld',svnbin='C:\\Program Files (x86)\\VisualSVN\\bin\\svn.exe')
changesource_HelloWorld1=changes.SVNPoller(repourl="file:///F:/localSVN/trunk/HelloWorld1",
svnuser=None, svnpasswd=None,pollInterval=10,
project='HelloWorld1',svnbin='C:\\Program Files (x86)\\VisualSVN\\bin\\svn.exe')
c['change_source'] = []
c['change_source']=[changesource_HelloWorld,changesource_HelloWorld1]
changesource只是监测变化,至于有了变化之后该做什么,这就需要schedule来决定。schedule也是可以有多个,每个schedule对应着不同处理,通过project参数,我们可以让buildbot针对不同的changesource去进行不同的动作。force schedule是指在buildbot的网页上生成一个按钮,用来手动执行对应的build动作,这个在进行手动测试时有一定的作用。
####### SCHEDULERS
# Configure the Schedulers, which decide how to react to incoming changes. In this
# case, just kick off a 'runtests' build
#Monitor the trunck change,branch=None means trunk
scheduler_HelloWorld=schedulers.SingleBranchScheduler(name="HelloWorld",
change_filter=util.ChangeFilter(project="HelloWorld",branch=None),
treeStableTimer=10,builderNames=["win10_builder","ubuntu_builder"])
scheduler_ForceHelloWorld=schedulers.ForceScheduler(name="HelloWorld_force",
builderNames=["win10_builder","ubuntu_builder"])
c['schedulers'] = []
c['schedulers']=[scheduler_HelloWorld,scheduler_ForceHelloWorld]
在建立schedule的时候,会提及一个叫做builderNames的参数,这个参数其实就是这里的builders。builders下面挂着一个或者多个workers,并且每个builder都有对应的factory。factory里面包含了很多的step。所以,builder其实就是来指挥worker来执行steps的。
####### BUILDERS
# The 'builders' list defines the Builders, which tell Buildbot how to perform a build:
# what steps, and which workers can execute them. Note that any particular build will
# only take place on one worker.
# step
step_checkout=steps.SVN(repourl='file:///F:/localSVN/trunk/HelloWorld',mode='incremental')
step_build=steps.ShellCommand(command=["MSBuild F:\\sandbox\\win10\\win10_builder\\build\\HelloWorld.sln",
"/t:Rebuild","/p:Configuration=Release"])
# factory
factory_win10= util.BuildFactory()
factory_win10.addSteps([step_checkout,step_build])
factory_ubuntu = util.BuildFactory()
factory_ubuntu.addSteps([step_checkout,step_build])
# builder
builder_win10=util.BuilderConfig(name="win10_builder",workernames=["win10"],factory=factory_win10)
builder_ubuntu=util.BuilderConfig(name="ubuntu_builder",workernames=["ubuntu","ubuntu1"],factory=factory_ubuntu)
c['builders'] = []
c['builders']=[builder_win10,builder_ubuntu]
buildbot除了提供以上的build等功能外,还提供了一些服务,比如这里的邮件通知。我这里的这个例子,是通过指定的邮件地址,登录指定的服务器,然后给指定的接收者发送邮件。fromaddr就是邮件的发送者,mode是指什么情况下发送邮件,有很多mode,比如warning,falling,passing的等等,这里选用all,就是所有的情况都发送。extraRecipients是指邮件的接收人,可以多个接收者。sendToInterestedUsers 设定为false,默认为true(发送给change相关的所有人)。relayhost是指smtp服务器,现在不少企业使用微软的exchange 来收发邮件,可以通过nslookup mx 的指令来查到对应的ip地址。
####### BUILDBOT SERVICES
# 'services' is a list of BuildbotService items like reporter targets. The
# status of each build will be pushed to these targets. buildbot/reporters/*.py
# has a variety to choose from, like IRC bots.
# 172.29.17.2 company smtp address
from buildbot.plugins import reporters
service_mailnotice = reporters.MailNotifier(fromaddr="xxx@xxx.com",mode="all",
extraRecipients=["terra@xxx.com.cn"],sendToInterestedUsers=False,
relayhost="xxx",smtpPort=25, smtpUser="xxx@xxx.com",smtpPassword="xxx")
c['services'] = []
c['services'].append(service_mailnotice)
####### PROJECT IDENTITY
# the 'title' string will appear at the top of this buildbot installation's
# home pages (linked to the 'titleURL').
c['title'] = "Hello World CI"
c['titleURL'] = "file:///F:/localSVN/trunk/HelloWorld"
# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server is visible. This typically uses the port number set in
# the 'www' entry below, but with an externally-visible host name which the
# buildbot cannot figure out without some help.
c['buildbotURL'] = "http://172.29.37.179:8010/"
# minimalistic config to activate new web UI
c['www'] = dict(port=8010,plugins=dict(waterfall_view={}, console_view={}, grid_view={}))
####### DB URL
c['db'] = {
# This specifies what database buildbot uses to store its state. You can leave
# this at its default for all but the largest installations.
'db_url' : "sqlite:///state.sqlite",
}