Skip to content

Instantly share code, notes, and snippets.

@hewerthomn
Forked from anonymous/envoy.php
Created February 1, 2017 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hewerthomn/b0d1a115647e36a8f250228c0c5323f8 to your computer and use it in GitHub Desktop.
Save hewerthomn/b0d1a115647e36a8f250228c0c5323f8 to your computer and use it in GitHub Desktop.
@include('envoy.config.php');
@servers($servers)
@setup
$startTime = microtime(true);
$startedAt = date('H:i:s');
if ( ! isset($repo) ) {
throw new Exception('Variável $repo não está definido!');
}
if ( ! isset($servers) ) {
throw new Exception('Variável $servers não está definida!');
}
if ( ! isset($paths) ) {
throw new Exception('Variável $paths não está definida!');
}
if ( ! isset($env)) {
throw new Exception('Variável $env não está definida!');
}
$now = new DateTime();
$date = $now->format('YmdHis');
$branch = isset($branch) ? $branch : 'master';
$path = rtrim($paths[$env], '/');
$pathDeploy = $pathsDeploys[$env];
$pathConfig = $pathsConfigs[$env];
$release = "{$pathDeploy}{$date}/";
$server = $servers[$env];
if ( substr($path, 0, 1) !== '/' ) {
throw new Exception('Cuidado - a variável $path não inicia com /');
}
$notify = function($msg) use($app, $env, $branch){
$title = "{$app}@{$env}^{$branch}";
exec('notify-send -i "${PWD}/img/BrasaoRondonia50x50.png" "'.$title.'" "'.$msg.'"');
};
@endsetup
@task('deploy', ['on' => $env])
<?php $notify("Iniciado deploy:{$env} às {$startedAt}"); ?>
echo "Iniciado deploy em {{ date('H:i:s') }}...";
echo "Env: {{ $env }}";
echo "Server: {{ $server }}";
echo "Path: {{ $path }}";
echo "Release: {{ $release }}";
git clone --verbose --depth=1 --branch={{ $branch }} {{ $repo }} {{ $release }};
if [ ! -d "{{ $release }}" ]; then
echo "Falha ao clonar o repositório";
return;
fi;
cp --verbose {{ $pathConfig }} {{ $release }}constants.php;
if [ -d "{{ $pathAlunoFotos }}" ]; then
rm -rf {{ $release }}escola/aluno/fotos;
fi;
ln -v -s {{ $pathAlunoFotos }} {{ $release }}escola/aluno/fotos;
cd {{ $release }};
echo "$(git log -1 --format='%cD (%aN - SHA %h)')" > last_commit.txt;
echo "Commit: $(git rev-parse HEAD)";
rm -rf {{ $release }}.git;
if [ -d "{{ $path }}" ]; then
rm -v {{ $path }};
fi;
ln -svn {{ $release }} {{ $path }};
if service --status-all | grep -F 'php5.6-fpm'; then
sudo service php5.6-fpm restart;
fi;
echo "Finalizado deploy.";
@endtask
@task('cleanup', ['on' => $env])
cd {{ $pathDeploy }};
find . -maxdepth 1 -name "20*" -mmin +2880 | xargs rm -Rf;
@endtask
@after
$executionTime = (int)(microtime(true) - $startTime);
$notify("Finalizado {$task}@{$env} em {$executionTime} s");
$dateFinish = date("d/m H:i:s");
$msg = "[{$app}][{$env}^{$branch}][{$dateFinish}][{$executionTime} s] Finalizado {$task}.";
@slack($slackHook, '#deploys', $msg)
@endafter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment