/** * Get the build id for the queued build * * @param string $repo * @param string $branch * @return int */ protected function createBuild($repo, $branch) { $buildCache = BuildCache::load($repo, $branch); $queueLocation = $buildCache->getQueueLocation(); if ($queueLocation) { dump("Loading from previous queue " . $queueLocation); #TODO: Switch to using native Verbosity param } else { $response = $this->getQueueLocation($repo, $branch); $queueLocation = (string) $response->getHeader('Location'); $buildCache->setQueueLocation($queueLocation)->save(); } if ($buildCache->getJobId()) { dump("Last job found: " . $buildCache->getJobId()); #TODO: Switch to using native Verbosity param return $buildCache->getJobId(); } do { //Get the queue URL for the build $buildUrl = $queueLocation . 'api/json'; try { $buildInfoResponse = $this->doGetRequest($buildUrl); } catch (ClientErrorResponseException $e) { dump("Previous queue not found, restarting build"); $buildCache->clear($repo, $branch); return $this->createBuild($repo, $branch); } $buildInfoResponse = json_decode($buildInfoResponse->getBody(true)); $jsonWhy = $buildInfoResponse->why; $executable = isset($buildInfoResponse->executable) ? $buildInfoResponse->executable : null; $sleepFor = 5; if (isset($buildInfoResponse->buildableStartMilliseconds)) { $sleepFor = ceil(($buildInfoResponse->buildableStartMilliseconds / 1000 - time()) / 100); } $sleepFor = max(5, $sleepFor); dump("Sleeping for {$sleepFor} seconds because: " . $jsonWhy); #TODO: Switch to using native Verbosity param sleep($sleepFor); } while ($jsonWhy != null && $executable == null); $jobId = $buildInfoResponse->executable->number; $buildCache->setJobId($jobId)->save(); return $jobId; }
/** * @inheritDoc */ protected function execute(InputInterface $input, OutputInterface $output) { $repo = $input->getArgument('repo'); if (!$repo) { while (!($repo = $this->getRepoName($input, $output))) { } } $branch = $input->getArgument('branch'); if (!$branch) { while (!($branch = $this->getBuildType($input, $output))) { } } $this->firstRun($output); $buildResult = $this->jenkins->buildRepo($repo, $branch); $deployResult = new DeployResult(); $buildSuccess = $buildResult->getResult() == BuildResult::SUCCESS; if ($buildSuccess) { $deployResult = $this->deployService->deploy($repo, $buildResult->getBuildId(), $output); } else { $output->writeln("<error>Build failed. Skipping deployment.</error>"); } if (!$buildSuccess) { BuildCache::clear($repo, $branch); } $deploySuccess = $deployResult->getDeployResult() == DeployResult::SUCCESS; if ($buildSuccess && $deploySuccess) { BuildCache::clear($repo, $branch); return 0; } return 1; }