Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Fixes #3661: Abort deploy if Git repo is inaccessible. #3662

Merged
merged 1 commit into from
May 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/Robo/Commands/Deploy/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ protected function prepareDir() {
* Adds remotes from git.remotes to /deploy repository.
*/
protected function addGitRemotes() {
// Add remotes and fetch upstream refs.
$git_remotes = $this->getConfigValue('git.remotes');
if (empty($git_remotes)) {
throw new BltException("git.remotes is empty. Please define at least one value for git.remotes in blt/blt.yml.");
Expand All @@ -271,9 +270,10 @@ protected function addGitRemotes() {

/**
* Adds a single remote to the /deploy repository.
*
* @param $remote_url
*/
protected function addGitRemote($remote_url) {
$this->say("Fetching from git remote $remote_url");
// Generate an md5 sum of the remote URL to use as remote name.
$remote_name = md5($remote_url);
$this->taskExecStack()
Expand All @@ -290,30 +290,49 @@ protected function addGitRemote($remote_url) {
protected function checkoutLocalDeployBranch() {
$this->taskExecStack()
->dir($this->deployDir)
// Create new branch locally.We intentionally use stopOnFail(FALSE) in
// case the branch already exists. `git checkout -B` does not seem to work
// as advertised.
// @todo perform this in a way that avoid errors completely.
->stopOnFail(FALSE)
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->exec("git checkout -b {$this->branchName}")
->run();
}

/**
* Merges upstream changes into deploy branch.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function mergeUpstreamChanges() {
$git_remotes = $this->getConfigValue('git.remotes');
$remote_url = reset($git_remotes);
$remote_name = md5($remote_url);

$this->say("Merging upstream changes into local artifact...");
$this->taskExecStack()

// Check if remote branch exists before fetching.
$result = $this->taskExecStack()
->dir($this->deployDir)
// This branch may not exist upstream, so we do not fail the build if a
// merge fails.
->stopOnFail(FALSE)
->exec("git ls-remote --exit-code --heads $remote_url {$this->branchName}")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
switch ($result->getExitCode()) {
case 0:
// The remote branch exists, continue and merge it.
break;

case 2:
// The remote branch doesn't exist, bail out.
return;

default:
// Some other error code.
throw new BltException("Unexpected error while searching for remote branch: " . $result->getMessage());
}

// Now we know the remote branch exists, let's fetch and merge it.
$this->taskExecStack()
->dir($this->deployDir)
->stopOnFail()
->exec("git fetch $remote_name {$this->branchName} --depth=1")
->exec("git merge $remote_name/{$this->branchName}")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
Expand Down