deployer/recipe/deploy/update_code.php

113 lines
3.3 KiB
PHP
Raw Normal View History

2016-11-19 15:13:32 +07:00
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
2020-04-25 23:00:08 +03:00
use Deployer\Exception\RunException;
/**
2017-08-14 14:43:31 +02:00
* Get current git HEAD branch as default branch to deploy.
*/
set('branch', function () {
try {
$branch = runLocally('git rev-parse --abbrev-ref HEAD');
} catch (\Throwable $exception) {
$branch = null;
}
2017-08-12 16:52:29 +03:00
if ($branch === 'HEAD') {
$branch = null; // Travis-CI fix
}
2017-08-12 16:43:25 +03:00
if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) {
$branch = input()->getOption('branch');
}
return $branch;
});
/**
* Whether to use git cache.
*
* Faster cloning by borrowing objects from existing clones.
*/
set('git_cache', function () {
$gitVersion = run('{{bin/git}} version');
$regs = [];
if (preg_match('/((\d+\.?)+)/', $gitVersion, $regs)) {
$version = $regs[1];
} else {
$version = "1.0.0";
}
return version_compare($version, '2.3', '>=');
});
2016-11-19 15:13:32 +07:00
desc('Update code');
task('deploy:update_code', function () {
2019-08-12 20:59:59 +03:00
$repository = get('repository');
2016-11-19 15:13:32 +07:00
$branch = get('branch');
$git = get('bin/git');
$gitCache = get('git_cache');
2017-08-10 10:22:57 +02:00
$recursive = get('git_recursive', true) ? '--recursive' : '';
$dissociate = get('git_clone_dissociate', true) ? '--dissociate' : '';
2020-04-25 23:00:08 +03:00
$quiet = output()->isQuiet() ? '-q' : '';
2016-11-19 15:13:32 +07:00
$depth = $gitCache ? '' : '--depth 1';
$at = '';
if (!empty($branch)) {
2020-04-20 22:56:31 +02:00
$at = "-b \"$branch\"";
2016-11-19 15:13:32 +07:00
}
// If option `tag` is set
if (input()->hasOption('tag')) {
$tag = input()->getOption('tag');
if (!empty($tag)) {
2020-04-20 22:56:31 +02:00
$at = "-b \"$tag\"";
2016-11-19 15:13:32 +07:00
}
}
// If option `tag` is not set and option `revision` is set
if (empty($tag) && input()->hasOption('revision')) {
$revision = input()->getOption('revision');
if (!empty($revision)) {
$depth = '';
}
}
2017-11-27 11:18:22 +01:00
Added Exception Handling for perhaps missing config param (#1677) * modify wrong ticket url (#1605) * moving the name column to the last position to show full name - bug fix: #1579 * add changelog * update changelog.md * update CHANGELOG.md * modify command to find http user * modify command to find http user * modify wrong ticket url * Remove magento:enable from Magento 2 recipe (#1606) * Remove magento:enable from Magento 2 recipe Magento's module states are stored in app/etc/config.php. This task ignored that and blindly enabled all installed modules. * Mention the removal of magento:enable in the changelog * Extend deploy:cleanup with tty allocation when using sudo. (#1607) * Added option cleanup_tty to allow allocation when using sudo * Added comment for new option cleanup_tty to CHANGELOG * Allow to set template path in Drupal 7 Settings recipe (#1613) * Change drupal:settings task to drupal:configure * Drupal 7 recipe - Set template file path * Changelog * Fixed once() tasks that where being run multiple times with ParallelE… (#1624) * Fixed once() tasks that where being run multiple times with ParallelExecutor * CHANGELOG: reorder issue * Add test case for parallel deploy on ->once() task * Added cleanup_use_sudo option to all commands in deploy:cleanup (#1632) * Added option cleanup_tty to allow allocation when using sudo * Added comment for new option cleanup_tty to CHANGELOG * Added cleanup_use_sudo option to all commands in cleanup.php * Fix return types for fluent interface (#1638) * Add Prestashop 1.6 recipe (#1641) * Add Prestashop 1.6 recipe * Update changelog * Sleep between process status calls to prevent 100% CPU usage (#1654) * Sleep between progress status calls to prevent 100% CPU usage * Add changelog entry * Typo in changelog entry * Update ParallelExecutor.php * Update ParallelExecutor.php * locate binary with less subprocesses (#1634) * Set custom user for CI environments (#1659) * Detect CI user * Update CHANGELOG.md * Update README.md * [deploy:writable] chgrp doesn't need http_user (#1660) * deploy:writable: chgrp doesn't need http_user * Update CHANGELOG.md * Add missing deploy:writable entries (#1662) * Fix missing deploy:shared entries (#1664) * Added Exception Handling for perhaps missing config param Sometimes deploy_path is not set and a cd into this folder is not possible. But the deployment can be successful without it. * style * #1677 fixes according pull-request feedback * #1677 code style fixes * Added Exception Handling for perhaps missing config param Sometimes deploy_path is not set and a cd into this folder is not possible. But the deployment can be successful without it. * style * #1677 fixes according pull-request feedback * #1677 code style fixes * Changelog entry for #1677 * Changelog entry for #1677 * Changelog entry for #1677
2018-09-05 14:03:04 +02:00
// Enter deploy_path if present
if (has('deploy_path')) {
cd('{{deploy_path}}');
}
2020-04-25 23:00:08 +03:00
// Populate known hosts
preg_match('/.*(@|\/\/)([^\/:]+).*/', $repository, $match);
if (isset($match[2])) {
$repositoryHostname = $match[2];
try {
run("ssh-keygen -F $repositoryHostname");
} catch (RunException $exception) {
run("ssh-keyscan -H $repositoryHostname >> ~/.ssh/known_hosts");
}
}
2017-04-08 21:06:15 +07:00
if ($gitCache && has('previous_release')) {
2016-11-19 15:13:32 +07:00
try {
2020-04-25 23:00:08 +03:00
run("$git clone $at $recursive $quiet --reference {{previous_release}} $dissociate $repository {{release_path}} 2>&1");
} catch (\Throwable $exception) {
2016-11-19 15:13:32 +07:00
// If {{deploy_path}}/releases/{$releases[1]} has a failed git clone, is empty, shallow etc, git would throw error and give up. So we're forcing it to act without reference in this situation
2020-04-25 23:00:08 +03:00
run("$git clone $at $recursive $quiet $repository {{release_path}} 2>&1");
2016-11-19 15:13:32 +07:00
}
} else {
// if we're using git cache this would be identical to above code in catch - full clone. If not, it would create shallow clone.
2020-04-25 23:00:08 +03:00
run("$git clone $at $depth $recursive $quiet $repository {{release_path}} 2>&1");
2016-11-19 15:13:32 +07:00
}
if (!empty($revision)) {
run("cd {{release_path}} && $git checkout $revision");
}
});