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 ;
desc ( 'Update code' );
task ( 'deploy:update_code' , function () {
$repository = trim ( get ( 'repository' ));
$branch = get ( 'branch' );
$git = get ( 'bin/git' );
$gitCache = get ( 'git_cache' );
$depth = $gitCache ? '' : '--depth 1' ;
2017-03-12 23:04:48 +07:00
$options = [
'tty' => get ( 'git_tty' , false ),
];
2016-11-19 15:13:32 +07:00
// If option `branch` is set.
if ( input () -> hasOption ( 'branch' )) {
$inputBranch = input () -> getOption ( 'branch' );
if ( ! empty ( $inputBranch )) {
$branch = $inputBranch ;
}
}
// Branch may come from option or from configuration.
$at = '' ;
if ( ! empty ( $branch )) {
$at = " -b $branch " ;
}
// If option `tag` is set
if ( input () -> hasOption ( 'tag' )) {
$tag = input () -> getOption ( 'tag' );
if ( ! empty ( $tag )) {
$at = " -b $tag " ;
}
}
// 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 = '' ;
}
}
$releases = get ( 'releases_list' );
if ( $gitCache && isset ( $releases [ 1 ])) {
try {
2017-03-12 23:04:48 +07:00
run ( " $git clone $at --recursive -q --reference { { deploy_path}}/releases/ { $releases [ 1 ] } --dissociate $repository { { release_path}} 2>&1 " , $options );
2016-11-19 15:13:32 +07:00
} catch ( \RuntimeException $exc ) {
// 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
2017-03-12 23:04:48 +07:00
run ( " $git clone $at --recursive -q $repository { { release_path}} 2>&1 " , $options );
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.
2017-03-12 23:04:48 +07:00
run ( " $git clone $at $depth --recursive -q $repository { { release_path}} 2>&1 " , $options );
2016-11-19 15:13:32 +07:00
}
if ( ! empty ( $revision )) {
run ( " cd { { release_path}} && $git checkout $revision " );
}
});