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 ;
2017-08-12 11:56:35 +03:00
/**
2017-08-14 14:43:31 +02:00
* Get current git HEAD branch as default branch to deploy .
2017-08-12 11:56:35 +03:00
*/
set ( 'branch' , function () {
try {
2017-08-12 18:06:21 +03:00
$branch = runLocally ( 'git rev-parse --abbrev-ref HEAD' );
2017-08-12 11:56:35 +03:00
} 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
2017-08-12 11:56:35 +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' : '' ;
2019-03-28 14:26:42 +01:00
$dissociate = get ( 'git_clone_dissociate' , true ) ? '--dissociate' : '' ;
2018-10-05 08:20:49 +02:00
$quiet = isQuiet () ? '-q' : '' ;
2016-11-19 15:13:32 +07:00
$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
$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 = '' ;
}
}
2017-11-27 11:18:22 +01:00
2018-09-05 14:03:04 +02:00
// Enter deploy_path if present
if ( has ( 'deploy_path' )) {
cd ( '{{deploy_path}}' );
}
2017-04-08 21:06:15 +07:00
if ( $gitCache && has ( 'previous_release' )) {
2016-11-19 15:13:32 +07:00
try {
2019-03-28 14:26:42 +01:00
run ( " $git clone $at $recursive $quiet --reference { { previous_release}} $dissociate $repository { { release_path}} 2>&1 " , $options );
2017-09-01 12:39:09 +07:00
} 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
2018-10-05 08:20:49 +02:00
run ( " $git clone $at $recursive $quiet $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.
2018-10-05 08:20:49 +02:00
run ( " $git clone $at $depth $recursive $quiet $repository { { release_path}} 2>&1 " , $options );
2016-11-19 15:13:32 +07:00
}
if ( ! empty ( $revision )) {
run ( " cd { { release_path}} && $git checkout $revision " );
}
});