Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BUILDBOT : How to know the branch that triggered the build?

Tags:

git

buildbot

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)
like image 469
hexaJer Avatar asked Oct 17 '25 11:10

hexaJer


1 Answers

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

like image 100
Fenn-CS Avatar answered Oct 19 '25 01:10

Fenn-CS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!