'Record a build by hitting the API
This step is skipped if we aren\'t recording the build, or if we don\'t
want to record successful builds yet (if we are running setup commands
for the build)'

def update_build(self, state=None):

    if (not self.record):
        return None
    self.build['project'] = self.project.pk
    self.build['version'] = self.version.pk
    self.build['builder'] = socket.gethostname()
    self.build['state'] = state
    if self.done:
        self.build['success'] = self.successful
        if (self.failure and isinstance(self.failure, BuildEnvironmentException)):
            self.build['exit_code'] = self.failure.status_code
        elif self.commands:
            self.build['exit_code'] = max([cmd.exit_code for cmd in self.commands])
    self.build['setup'] = self.build['setup_error'] = ''
    self.build['output'] = self.build['error'] = ''
    if self.start_time:
        build_length = (datetime.utcnow() - self.start_time)
        self.build['length'] = int(build_length.total_seconds())
    if (self.failure is not None):
        if isinstance(self.failure, (BuildEnvironmentException, BuildEnvironmentWarning)):
            self.build['error'] = str(self.failure)
        else:
            self.build['error'] = ugettext_noop('An    unexpected    error    occurred')
    for (key, val) in list(self.build.items()):
        if isinstance(val, six.binary_type):
            self.build[key] = val.decode('utf-8', 'ignore')
    try:
        api_v2.build(self.build['id']).put(self.build)
    except HttpClientError as e:
        log.error('Unable    to    post    a    new    build:    %s', e.content)
    except Exception:
        log.error('Unknown    build    exception', exc_info=True)
