private function getBuildEnv(Build $build) { $env = ['BUILD_ID=' . $build->getId(), 'PROJECT_ID=' . $build->getProject()->getId(), 'CHANNEL=' . $build->getChannel(), 'SSH_URL=' . $build->getProject()->getGitUrl(), 'REF=' . $build->getRef(), 'HASH=' . $build->getHash(), 'IS_PULL_REQUEST=' . ($build->isPullRequest() ? 1 : 0), 'SYMFONY_ENV=prod']; $user = $build->getProject()->getUsers()->first(); if ($user->hasProviderAccessToken('github')) { /** * @todo there must be a way to avoid requiring a valid access token * I think the token is only used to avoid hitting github's * API limit through composer, so maybe there's a way to use a * stage1 specific token instead */ $env[] = 'GITHUB_ACCESS_TOKEN=' . $user->getProviderAccessToken('github'); } return $env; }
/** * @param Build $build * * @return Docker\Container */ public function run(Build $build, $timeout = null) { $producer = $this->websocketProducer; $logger = $this->logger; $docker = $this->docker; $em = $this->doctrine->getManager(); $publish = function ($content) use($build, $producer) { $message = new BuildMessage($build, $content); $producer->publish((string) $message); }; // $publish(' build started ('.date('r').')'); if ($this->getOption('dummy')) { $logger->info('dummy build, sleeping', ['duration' => $this->getOption('dummy_duration')]); sleep($this->getOption('dummy_duration')); $build->setPort(42); return true; } $project = $build->getProject(); $logger->info('starting build', ['build' => $build->getId(), 'project' => $project->getFullName(), 'project_id' => $project->getId(), 'ref' => $build->getRef(), 'hash' => $build->getHash(), 'timeout' => $timeout, 'force_local_build_yml' => $build->getForceLocalBuildYml()]); /** Generate build script using Yuhao */ $logger->info('generating build script'); // $publish(' generating build script'.PHP_EOL); $builder = $project->getDockerContextBuilder(); $builder->from('stage1/yuhao'); $docker->build($builder->getContext(), $build->getImageName('yuhao'), false, true, true); $prepareContainer = new PrepareContainer($build); // @todo move inside PrepareContainer if ($build->getForceLocalBuildYml()) { $prepareContainer->addEnv(['FORCE_LOCAL_STAGE1_YML=1']); } $manager = $docker->getContainerManager(); $manager->create($prepareContainer)->start($prepareContainer)->wait($prepareContainer, $timeout); if ($prepareContainer->getExitCode() != 0) { $exitCode = $prepareContainer->getExitCode(); if (isset(Process::$exitCodes[$exitCode])) { $exitCodeLabel = Process::$exitCodes[$exitCode]; } else { $exitCodeLabel = ''; } $message = sprintf('failed to generate build scripts (exit code %d (%s))', $exitCode, $exitCodeLabel); $publish($message . PHP_EOL); $logger->error($message, ['build' => $build->getId(), 'container' => $prepareContainer->getId(), 'container_name' => $prepareContainer->getName(), 'exit_code' => $exitCode, 'exit_code_label' => $exitCodeLabel]); $docker->commit($prepareContainer, ['repo' => $build->getImageName('yuhao'), 'tag' => 'failed']); throw new Exception($message, $prepareContainer->getExitCode()); } // @todo remove yuhao container // $manager->remove($prepareContainer); => 406 ?! $logger->info('yuhao finished executing, retrieving build script', ['build' => $build->getId()]); $output = ''; $manager->attach($prepareContainer, true, false, false, true, true)->readAttach(function ($type, $chunk) use(&$output) { $output .= $chunk; }); $logger->info('got response from yuhao', ['build' => $build->getId(), 'response' => $output, 'parsed_response' => json_decode($output, true)]); $script = BuildScript::fromJson($output); $script->setBuild($build); $options = $project->getDefaultBuildOptions(); $options = $options->resolve($script->getConfig()); $build->setOptions($options); $logger->info('resolved options', ['options' => $options]); $strategy = array_key_exists('path', $options['dockerfile']) ? new DockerfileStrategy($logger, $docker, $em, $this->websocketProducer, $this->redis, $this->options) : new DefaultStrategy($logger, $docker, $em, $this->websocketProducer, $this->options); $logger->info('elected strategy', ['strategy' => get_class($strategy)]); $em->persist($build); $em->persist($script); $em->flush(); $logger->info('starting build'); $strategy->build($build, $script, $timeout); $logger->info('finished build'); $em->persist($build); $em->persist($script); $em->flush(); /** * Launch App container */ $logger->info('starting app container', ['project' => $build->getProject()->getFullName(), 'image' => $build->getImageName()]); $ports = new PortCollection(80); # @todo DefaultStrategy containers should have an entrypoint # so we don't need to provide an actual command $appContainer = new AppContainer($build, $strategy->getCmd()); $appContainer->addEnv($build->getProject()->getContainerEnv()); $appContainer->setExposedPorts($ports); if ($build->getForceLocalBuildYml()) { $appContainer->addEnv(['FORCE_LOCAL_STAGE1_YML=1']); } try { $manager->create($appContainer)->start($appContainer, ['PortBindings' => $ports->toSpec()]); $logger->info('running app container', ['build' => $build->getId(), 'container' => $appContainer->getId()]); } catch (\Docker\Exception\UnexpectedStatusCodeException $e) { if ($e->getCode() === 404) { throw new \RuntimeException('Could not start app container (' . $e->getMessage() . ')', 404, $e); } throw $e; } // $publish(' build finished ('.date('r').')'.PHP_EOL); return $appContainer; }
/** * @param Project $project * @param Build $build * @param string $status */ public function setCommitStatus(Project $project, Build $build, $status) { $client = $this->configureClientForProject($project); $client->setDefaultOption('headers/Accept', 'application/vnd.github.she-hulk-preview+json'); $request = $client->post(['/repos/' . $project->getFullName() . '/statuses/{sha}', ['sha' => $build->getHash()]]); $request->setBody(json_encode(['state' => 'success', 'target_url' => $build->getUrl(), 'description' => 'Stage1 instance ready', 'context' => 'stage1'])); $this->logger->info('sending commit status', ['build' => $build->getId(), 'project' => $project->getGithubFullNAme(), 'sha' => $build->getHash()]); try { $request->send(); } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) { $this->logger->error('error sending commit status', ['exception_class' => get_class($e), 'exception_message' => $e->getMessage(), 'url' => $e->getRequest()->getUrl(), 'response' => (string) $e->getResponse()->getBody()]); } }