Skip to content

Instantly share code, notes, and snippets.

Created February 1, 2017 12:57

Revisions

  1. @invalid-email-address Anonymous created this gist Feb 1, 2017.
    94 changes: 94 additions & 0 deletions envoy.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    @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