def build_project(self, project_id): log = ["Build started at %s" % datetime.now()] status = BuildService.Failure scm_status = ScmResult.Failed project = self.repository.get(project_id) build = Build(datetime.now(), status, scm_status, "", project) scm_creation_result = self.scm.create_or_update(project) build.scm_status = scm_creation_result.status if scm_creation_result.status == ScmResult.Failed: log.append(scm_creation_result.log) status = BuildService.Failure else: log.append("Downloaded code from %s" % project.scm_repository) execute_result = self.executer.execute(project.build_script, scm_creation_result.repository_path) log.append("Executed %s" % project.build_script) log.append("Exit Code: %s" % execute_result.exit_code) log.append("Run Log:") log.append(execute_result.run_log) status = execute_result.exit_code == 0 and BuildService.Success or BuildService.Failure build.status = status build.log = "\n".join(log) self.repository.update(project) return build
def test_get_last_build_number(self): project = Project() project.name = "Test" project.scm_repository = "git" project.build_script = "make test" self.assertEquals(project.get_last_build_number(), 0) project.builds = [] build = Build() build.number = 1 build.date = None build.status = None build.scm_status = None build.log = None build.project = project project.builds.append(build) self.assertEquals(project.get_last_build_number(), 1)
def build_project(self, project_id): ctx = SkinkContext.current() log = ["Build started at %s" % datetime.now()] status = BuildService.Failure scm_status = ScmResult.Failed project = self.repository.get(project_id) PluginEvents.on_before_build(project) ctx.projects_being_built.append(project_id) last_build_number = project.get_last_build_number() build = Build() build.date = datetime.now() build.status = status build.scm_status = scm_status build.log = "" build.project = project scm_creation_result = self.scm.create_or_update(project) build.scm_status = scm_creation_result.status if scm_creation_result.status == ScmResult.Failed: log.append(scm_creation_result.log) status = BuildService.Failure else: log.append("Downloaded code from %s (%s)" % (project.scm_repository, scm_creation_result.status)) execute_result = self.executer.execute(project.build_script, scm_creation_result.repository_path, timeout=ctx.build_timeout) log.append("Executed %s" % project.build_script) log.append("Exit Code: %s" % execute_result.exit_code) log.append("Run Log:") log.append(execute_result.run_log) status = execute_result.exit_code == 0 and BuildService.Success or BuildService.Failure for command in project.tabs: build_tab = BuildTab(name=command.name, command=command.command, content_type=command.content_type, build=build) result = self.executer.execute(command.command, scm_creation_result.repository_path) build_tab.log = result.run_log for file_locator in project.file_locators: file_locator_path = join(self.base_path, project.name, file_locator.locator) print "Finding files for locator %s" % file_locator_path files = glob.glob(file_locator_path) print "%d files found" % len(files) for f in files: print "Adding file %s" % f filename = split(f)[-1] stream = open(f, 'rb') content = stream.read() stream.close() build_file = BuildFile(name=filename, original_path=f, content=content, build=build) build.number = last_build_number + 1 build.status = status build.log = "\n".join(log) build.commit_number = force_unicode(scm_creation_result.last_commit["commit_number"]) build.commit_author = force_unicode(scm_creation_result.last_commit["author"]) build.commit_committer = force_unicode(scm_creation_result.last_commit["committer"]) build.commit_author_date = scm_creation_result.last_commit["author_date"] build.commit_committer_date = scm_creation_result.last_commit["committer_date"] build.commit_text = force_unicode(scm_creation_result.last_commit["subject"]) self.repository.update(project, project.tabs, [locator.locator for locator in project.file_locators]) ctx.projects_being_built.remove(project_id) if (build.status == BuildService.Success): PluginEvents.on_build_successful(project, build) self.process_pipelines_for(project) else: PluginEvents.on_build_failed(project, build) return build