4

I try to configure buildbot for multiple projects, git repositories and git branches.

For each, I added a GitPoller with branches=[master, develop, release]. It's work but I don't know what I have to put in steps.Git branch argument. How to know the branch that triggered the build ?

see generic_factory(package) function (13th line)

def add_git_poller(c, package, pollinterval=-2):
    name = package.get_uid()
    build_data = package.get_build_data()
    # get branches
    try:
        branches = build_data['branches']
    except KeyError:
        branches = ['master']
    workdir = os.path.join("gitpoller",util.safeTranslate(name))
    dwlogging.info("Add git poller '%s' (workdir=%s, project=%s, branches=%s, pollinterval=%i)" %
                   (name, workdir, name, branches, pollinterval))
    poller = plugins.changes.GitPoller(repourl=package.get_cvs_url(),
                                       project=name,
                                       workdir=workdir, 
                                       branches=branches,
                                       pollinterval=pollinterval)
    c['change_source'].append(poller)

def schedule_single_branch(c, package):
    name = package.get_uid()
    build_data = package.get_build_data()
    # get branches
    if 'branches' in build_data:
        branches = build_data['branches']
    else:
        branches = ['master']
    for branch in branches:
        s_name="%s_%s" % (branch, name)
        builders=[name]
        dwlogging.info("Add single branch scheduler '%s' (project=%s, branch=%s, builders=%s)" %
                       (s_name, name, branch, str(builders)))
        change_filter = plugins.util.ChangeFilter(branch=branch, project=name)
        scheduler = plugins.schedulers.SingleBranchScheduler(
                                        name=s_name,
                                        change_filter=change_filter,
                                        builderNames=builders)
        c['schedulers'].append(scheduler)

# ...

def add_builder(c, package):
    name = package.get_uid()
    dwlogging.trace("add builder for package %s" % name)
    available_slaves = slaves_config.keys()
    build_data = package.get_build_data()
    slavenames = build_data['slaves']
    for slavename in slavenames:
        if slavename not in available_slaves:
            raise Exception("Package '%s' : slave '%s' not exists" % (name, slavename))

    if len(slavenames) > 0:
        builddir = os.path.join("build",util.safeTranslate(name))
        tags = [package.get_repository().get_uid()]
        dwlogging.info("Add builder '%s' (builddir=%s, slaves=%s)" %
                       (name, builddir, str(slavenames)))
        f = generic_factory(package)
        builder_config = plugins.util.BuilderConfig(
                                        name=name,
                                        builddir=builddir,
                                        slavenames=slavenames,
                                        factory=f,
                                        tags=tags)
        c['builders'].append(builder_config)

def generic_factory(package):
    name = package.get_uid()
    dwlogging.info("Make build factory for '%s' package" % name)
    build_data = package.get_build_data()
    testdir = os.path.join("test",util.safeTranslate(name))
    factory = plugins.util.BuildFactory()
    # check out the source
    if build_data['cvs']['type'] == 'git':
        submodules = build_data['cvs'].get('submodules',False)
        dwlogging.info("Add git step (submodules=%s)" % str(submodules))
        factory.addStep(plugins.steps.Git(repourl=package.get_cvs_url(), 
                                          mode='full',
                                          submodules=submodules,
                                          branch='%%BRANCH%%')) # ???
    else:
        dwlogging.error("%s not implemented yet" % build_data['cvs']['type'])

    if 'steps' in build_data and build_data['steps']:
        for step_data in build_data['steps']:
            step_type = step_data.get('type')
            step_args = step_data.get('args')
            if not isinstance(step_args, (list, tuple)):
                step_args = [step_args]
            workdir = step_data.get('workdir', None)
            dwlogging.info("Add step %s (%s workdir:%s)" % (step_type, step_args, workdir))
            if step_type == 'command':
                factory.addStep(plugins.steps.ShellCommand(name="command (%s)" % step_args[0],
                                                           command=step_args,
                                                           workdir=workdir))
            elif step_type == 'compile':
                factory.addStep(plugins.steps.Compile(command=step_args,
                                                      warningPattern="^(.\*?):([0-9]+): [Ww]arning: (.\*)$",
                                                      warningExtractor=plugins.steps.Compile.warnExtractFromRegexpGroups,
                                                      workdir=workdir))
            elif step_type == 'pyflakes':
                command = ["pyflakes"] + step_args
                factory.addStep(plugins.steps.PyFlakes(command=command))
            elif step_type == 'pylint':
                for path in step_args:
                    factory.addStep(plugins.steps.PyLint(name="pylint (%s)" % path,
                                                         command=["pylint", path]))
            elif step_type == 'trial':
                for path in step_args:
                    factory.addStep(plugins.steps.Trial(name="trial (%s)" % path,
                                                        tests=path, 
                                                        testpath="python"))
            elif step_type == 'mkdir':
                factory.addStep(plugins.steps.MakeDirectory(dir=step_args[0]))

    factory.addStep(plugins.steps.RemovePYCs())
    return factory

for package_uid in packages:
    package = pkg_manager.get_package(package_uid)
    add_git_poller(c, package, pollinterval=300)
    schedule_single_branch(c, package)
    schedule_forced(c, package)
    schedule_try(c, package, schedule_try_port, [('sampleuser','samplepass')])
    schedule_try_port += 1
    add_builder(c, package)
hexaJer
  • 825
  • 7
  • 12

2 Answers2

1

Actually you can write an extremely simple function to return the name of a branch on which a builder's step is running.

def getBranch(step):
    return step.getProperty("branch")

# Additionally, you can make additional checks for specific brancges like 

def isMaster(step):
    return step.getProperty("branch") == "master"

So you would change this

 factory.addStep(plugins.steps.Git(repourl=package.get_cvs_url(), 
                                          mode='full',
                                          submodules=submodules,
                                          branch='%%BRANCH%%'))

to

 factory.addStep(plugins.steps.Git(repourl=package.get_cvs_url(), 
                                              mode='full',
                                              submodules=submodules,
                                              branch=lambda step:getBranch(step))

This is as of builbot 2.x

Fenn-CS
  • 863
  • 1
  • 13
  • 30
0

Buildbot documentation for Git step says:

branch

(optional): this specifies the name of the branch to use when a Build does not provide one of its own. If this this parameter is not specified, and the Build does not provide a branch, the master branch will be used.

which I interpret to mean that the Git step by default 'inherits' the branch whose change triggered the build. Therefore, if you just leave that parameter alone it should probably just work.

Community
  • 1
  • 1
Aleš Erjavec
  • 1,086
  • 7
  • 11