Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DT-682: Fixes #3811: Ensure all failing commands throw exceptions. #3846

Merged
merged 1 commit into from
Sep 13, 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
83 changes: 67 additions & 16 deletions src/Robo/Commands/Deploy/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function initialize() {
* @validateGitConfig
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
* @throws \Robo\Exception\TaskException
* @throws \Exception
*/
public function deploy(array $options = [
'branch' => InputOption::VALUE_REQUIRED,
Expand Down Expand Up @@ -170,10 +172,13 @@ public function checkDirty(array $options = ['ignore-dirty' => FALSE]) {
*
* Defaults to the last commit message on the source branch.
*
* @param array $options
* CLI options for command.
*
* @return string
* The commit message.
*/
protected function getCommitMessage($options) {
protected function getCommitMessage(array $options) {
if (!$options['commit-msg']) {
chdir($this->getConfigValue('repo.root'));
$log = explode(' ', shell_exec("git log --oneline -1"), 2);
Expand Down Expand Up @@ -248,6 +253,8 @@ protected function getDefaultBranchName() {

/**
* Creates artifact, cuts new tag, and pushes.
*
* @throws \Exception
*/
protected function deployToTag($options) {
$this->tagName = $this->getTagName($options);
Expand All @@ -274,6 +281,9 @@ protected function deployToTag($options) {

/**
* Creates artifact on branch and pushes.
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function deployToBranch($options) {
$this->branchName = $this->getBranchName($options);
Expand All @@ -288,31 +298,41 @@ protected function deployToBranch($options) {

/**
* Deletes the existing deploy directory and initializes git repo.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
* @throws \Robo\Exception\TaskException
*/
protected function prepareDir() {
$this->say("Preparing artifact directory...");
$deploy_dir = $this->deployDir;
$this->taskDeleteDir($deploy_dir)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
$this->taskFilesystemStack()
$result = $this->taskFilesystemStack()
->mkdir($this->deployDir)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->stopOnFail()
->run();
$this->taskExecStack()
if (!$result->wasSuccessful()) {
throw new BltException('Failed to create deploy directory');
}
$result = $this->taskExecStack()
->dir($deploy_dir)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->stopOnFail()
->exec("git init")
->exec("git config --local core.excludesfile false")
->exec("git config --local core.fileMode true")
->run();
if (!$result->wasSuccessful()) {
throw new BltException('Failed to initialize git repo');
}
$this->say("Global .gitignore file is being disabled for this repository to prevent unexpected behavior.");
}

/**
* Adds remotes from git.remotes to /deploy repository.
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function addGitRemotes() {
$git_remotes = $this->getConfigValue('git.remotes');
Expand All @@ -329,34 +349,45 @@ protected function addGitRemotes() {
*
* @param string $remote_url
* Remote URL.
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function addGitRemote($remote_url) {
// Generate an md5 sum of the remote URL to use as remote name.
$remote_name = md5($remote_url);
$this->taskExecStack()
->stopOnFail()
$result = $this->taskExecStack()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->dir($this->deployDir)
->exec("git remote add $remote_name $remote_url")
->run();
if (!$result->wasSuccessful()) {
throw new BltException('Failed to add remote');
}
}

/**
* Checks out a new, local branch for artifact.
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function checkoutLocalDeployBranch() {
$this->taskExecStack()
$result = $this->taskExecStack()
->dir($this->deployDir)
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->exec("git checkout -b {$this->branchName}")
->run();
if (!$result->wasSuccessful()) {
throw new BltException('Failed to check out branch');
}
}

/**
* Merges upstream changes into deploy branch.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
* @throws \Robo\Exception\TaskException
*/
protected function mergeUpstreamChanges() {
$git_remotes = $this->getConfigValue('git.remotes');
Expand Down Expand Up @@ -387,20 +418,25 @@ protected function mergeUpstreamChanges() {
}

// Now we know the remote branch exists, let's fetch and merge it.
$this->taskExecStack()
$result = $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)
->run();
if (!$result->wasSuccessful()) {
throw new BltException('Failed to merge branch');
}
}

/**
* Builds deployment artifact.
*
* @command artifact:build
* @aliases ab deploy:build
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
public function build() {
$this->say("Generating build artifact...");
Expand Down Expand Up @@ -432,6 +468,9 @@ public function build() {

/**
* Copies files from source repo into artifact.
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function buildCopy() {
$exclude_list_file = $this->getExcludeListFile();
Expand All @@ -440,11 +479,13 @@ protected function buildCopy() {

$this->setMultisiteFilePermissions(0777);
$this->say("Rsyncing files from source repo into the build artifact...");
$this->taskExecStack()->exec("rsync -a --no-g --delete --delete-excluded --exclude-from='$exclude_list_file' '$source/' '$dest/' --filter 'protect /.git/'")
$result = $this->taskExecStack()->exec("rsync -a --no-g --delete --delete-excluded --exclude-from='$exclude_list_file' '$source/' '$dest/' --filter 'protect /.git/'")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->stopOnFail()
->dir($this->getConfigValue('repo.root'))
->run();
if (!$result->wasSuccessful()) {
throw new BltException('Failed to rsync artifact');
}
$this->setMultisiteFilePermissions(0755);

// Remove temporary file that may have been created by
Expand All @@ -467,6 +508,7 @@ protected function buildCopy() {
* Bool.
*
* @throws \Robo\Exception\TaskException
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function composerInstall() {
if (!$this->getConfigValue('deploy.build-dependencies')) {
Expand All @@ -489,7 +531,6 @@ protected function composerInstall() {
$command .= ' --ignore-platform-reqs';
}
$execution_result = $this->taskExecStack()->exec($command)
->stopOnFail()
->dir($this->deployDir)
->run();
if (!$execution_result->wasSuccessful()) {
Expand Down Expand Up @@ -648,6 +689,7 @@ protected function commit() {
* Bool.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
* @throws \Robo\Exception\TaskException
*/
protected function push($identifier, array $options) {
if ($options['dry-run']) {
Expand Down Expand Up @@ -682,8 +724,7 @@ protected function push($identifier, array $options) {
protected function cutTag($repo = 'build') {
$taskGit = $this->taskGit()
->tag($this->tagName, $this->commitMessage)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->stopOnFail();
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE);

if ($repo == 'build') {
$taskGit->dir($this->deployDir);
Expand All @@ -698,6 +739,8 @@ protected function cutTag($repo = 'build') {

/**
* Executes artifact:build:simplesamlphp-config command.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function deploySamlConfig() {
if ($this->getConfigValue('simplesamlphp')) {
Expand All @@ -710,6 +753,8 @@ protected function deploySamlConfig() {
*
* @command artifact:update:drupal
* @aliases aud deploy:update
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
public function update() {
// Disable alias since we are targeting specific uri.
Expand All @@ -722,6 +767,8 @@ public function update() {
*
* @command artifact:update:drupal:all-sites
* @aliases auda
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
public function updateAll() {
// Disable alias since we are targeting specific uri.
Expand All @@ -737,6 +784,8 @@ public function updateAll() {
*
* @param string $multisite
* Multisite.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function updateSite($multisite) {
$this->switchSiteContext($multisite);
Expand All @@ -756,6 +805,8 @@ protected function updateSite($multisite) {
*
* @command artifact:sync:all-sites
* @aliases asas
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
public function syncRefresh() {
// Disable alias since we are targeting specific uri.
Expand Down