Skip to content

Commit 1784024

Browse files
committed
added helper functions, improvements to Envoy, updated to publish Envoy.blade.php, Makefile
- added helper functions dumpVariables(), dumpVariablesCli() - updated Envoy to allow configuration of composer path, disabling logging, check for git -j flag support
1 parent 7a87a88 commit 1784024

File tree

5 files changed

+130
-13
lines changed

5 files changed

+130
-13
lines changed

.env.include

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ DEPLOY_SERVER=server.example.org
2929
# path on server to deploy to
3030
DEPLOY_PATH=/var/www
3131

32-
# repository to deploy (used from server if possible)
32+
# repository to deploy (used from server if not empty)
3333
DEPLOY_REPOSITORY=git@git.example.org:user/repo.git
3434

3535
# report deployments to Slack

Envoy.blade.php

+38-10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
$path = getenv('DEPLOY_PATH');
8484
$slack = getenv('DEPLOY_SLACK_WEBHOOK');
8585

86+
$composer = getenv('DEPLOY_COMPOSER') ?: 'composer';
87+
$disable_log = getenv('DEPLOY_DISABLE_LOG');
88+
$disable_log = 'false' === $disable_log ? false : (boolean) $disable_log;
8689
$maxmind_dl = getenv('DEPLOY_DL_MAXMIND');
8790
$maxmind_dl = 'false' === $maxmind_dl ? false : (boolean) $maxmind_dl;
8891
$opcache_reset = getenv('DEPLOY_OPCACHE_RESET');
@@ -106,7 +109,7 @@
106109
$current_release_dir = last($output);
107110
fprintf(STDERR, " => %s\n", $current_release_dir); fflush(STDERR);
108111

109-
define('CMD_LOG_START', 'exec> >(sh -c "while read line;do echo \`date\` \"\$line\";done|tee -a ' . escapeshellarg($log_file) . '") 2> >(sh -c "while read line;do echo \`date\` stderr: \"\$line\";done|tee -a ' . escapeshellarg($log_file) . '" >&2)');
112+
define('CMD_LOG_START', $disable_log ? '' : 'exec> >(sh -c "while read line;do echo \`date\` \"\$line\";done|tee -a ' . escapeshellarg($log_file) . '") 2> >(sh -c "while read line;do echo \`date\` stderr: \"\$line\";done|tee -a ' . escapeshellarg($log_file) . '" >&2)');
110113

111114
if (!$repository) {
112115
// make temporary checkout & copy to deployment server
@@ -123,9 +126,20 @@
123126
$command = sprintf('ssh %s mkdir -p "%s"', escapeshellarg($server), escapeshellarg($new_release_dir));
124127
exec($command, $output);
125128

129+
// checking if git supports '-j' flag
130+
$use_j = true;
131+
$command = 'git clone -j8 2>&1';
132+
$prefix = get_prefix($hostname);
133+
134+
fprintf(STDERR, "{$prefix}executing command: %s\n", $command); fflush(STDERR);
135+
if (strpos(`$command`, 'error: unknown switch')) {
136+
fprintf(STDERR, "{$prefix} => Warning: this host does not support 'git clone -j'!\n", $command); fflush(STDERR);
137+
$use_j = false;
138+
}
139+
126140
// clone repository
127-
$command_format = 'sh -c "(%1$s umask 002; %1$s git clone --recursive -j8 -b %2$s . %3$s && %1$s rsync -zaSHx %3$s/ %4$s/); %1$s rm -rf %3$s" 2>&1';
128-
$command = sprintf($command_format, $runner, escapeshellarg($branch), escapeshellarg($temp_checkout_dir), escapeshellarg($destination));
141+
$command_format = 'sh -c "(%1$s umask 002; %1$s git clone --recursive %2$s -b %3$s . %4$s && %1$s rsync --exclude=.git\\* --exclude=Envoy.blade.php --exclude=Makefile -zaSHx %4$s/ %5$s/); %1$s rm -rf %4$s" 2>&1';
142+
$command = sprintf($command_format, $runner, $use_j ? '-j8' : '', escapeshellarg($branch), escapeshellarg($temp_checkout_dir), escapeshellarg($destination));
129143

130144
$prefix = get_prefix($hostname);
131145
fprintf(STDERR, "{$prefix}executing command: %s\n", $command); fflush(STDERR);
@@ -199,6 +213,7 @@ function get_prefix($server)
199213
{{ CMD_LOG_START }}
200214
echo " - Initializing of '{{ $app_name }}' started on {{ $date }} on environment: {{ $env }}: {{ $server }}"
201215
if [ ! -d {{ $current_dir }} ]; then
216+
[ -d {{ $releases_dir }} ] || {{ $runner }} mkdir -p {{ $releases_dir }}
202217
{{ $runner }} mv {{ $new_release_dir }}/storage {{ $app_dir }}/storage
203218
{{ $runner }} cp {{ $new_release_dir }}/.env.example {{ $app_dir }}/.env
204219
echo " +- deployment path initialised, configure {{ $app_dir }}/.env and run 'envoy run deploy --env={{ $env }}' to deploy."
@@ -212,11 +227,24 @@ function get_prefix($server)
212227
{{ CMD_LOG_START }}
213228
echo "Starting deployment task '{{ $task }}' of '{{ $app_name }}' on {{ $date }} on {{ $env }}: {{ $server }}{{ $dry_run ? ' [DRY-RUN]' : '' }}"
214229
echo " - Cloning repository ..."
215-
[ -d {{ $releases_dir }} ] || {{ $runner }} mkdir {{ $releases_dir }}
230+
[ -d {{ $releases_dir }} ] || {{ $runner }} mkdir -p {{ $releases_dir }}
231+
echo " +- created release directory ..."
216232
@if ($repository)
217-
{{ $runner }} git clone --recursive -j8 --depth 1 -b {{ $branch }} {{ $repository }} {{ $new_release_dir }}
218-
echo " +- repository cloned"
233+
use_j=true
234+
if git clone -j8 2>&1 | fgrep 'error: unknown switch' > /dev/null 2>&1; then
235+
echo " +- warning: environment '{{ $env }}' does not support 'git clone -j' !!!"
236+
use_j=false
237+
else
238+
echo " +- checked that git supports '-j' flag ..."
239+
fi
240+
if $use_j; then
241+
{{ $runner }} git clone --recursive -j8 --depth 1 -b {{ $branch }} {{ $repository }} {{ $new_release_dir }}
242+
else
243+
{{ $runner }} git clone --recursive --depth 1 -b {{ $branch }} {{ $repository }} {{ $new_release_dir }}
244+
fi
245+
echo " +- repository cloned"
219246
@endif
247+
chmod 751 {{ $new_release_dir }}
220248
@endtask
221249

222250
@task('deployment_links')
@@ -239,12 +267,12 @@ function get_prefix($server)
239267
{{ CMD_LOG_START }}
240268
echo " - Running composer ..."
241269
@if ( $current_release_dir )
242-
{{ $runner }} cp -a {{ $current_release_dir }}/vendor {{ $new_release_dir }}/
270+
{{ $runner }} rsync -aSHAX {{ $current_release_dir }}/vendor {{ $new_release_dir }}/ || true
243271
echo " +- copied vendor"
244272
@endif
245273
{{ $runner }} cd {{ $new_release_dir }}
246274
echo " +- running composer ..."
247-
{{ $runner }} composer install --no-interaction {{ $is_dev ? '--no-dev' : '' }} --prefer-dist --no-scripts -q -o
275+
{{ $runner }} {{ $composer }} install --no-interaction {{ $is_dev ? '--no-dev' : '' }} --prefer-dist --no-scripts -q -o
248276
echo " +- composer installed"
249277
@endtask
250278

@@ -294,9 +322,9 @@ function get_prefix($server)
294322
{{ $runner }} php {{ $current_dir }}/artisan route:cache || true # Create a route cache file for faster route registration (fails with Closure routes)
295323
{{ $runner }} php {{ $current_dir }}/artisan config:cache # Create a cache file for faster configuration loading
296324
{{ $runner }} php {{ $current_dir }}/artisan config:cache # 2nd time to fix incorrect database credentials???
297-
{{ $runner }} php {{ $current_dir }}/artisan storage:link # link storage/public
325+
{{ $runner }} php {{ $current_dir }}/artisan storage:link || true # link storage/public (might fail for older Laravel versions)
298326
echo " +- cache generated"
299-
echo " +- published '{{ $app_name }}'{{ '@' }}{{ $branch }} to {{ $env }}"
327+
echo " +- published '{{ $app_name }}'{{ '@' }}{{ $branch }} to {{ $env }} at `date`"
300328
@endtask
301329

302330
@task('deployment_cleanup')

composer.json

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"keywords": ["laravel"],
66
"homepage": "https://github.com/Magentron/laravel-scripts",
7-
"time": "2018-03-28 18:46:37",
7+
"time": "2018-11-26 16:54:04",
88
"license": "GPL-3.0-or-later",
99
"authors": [
1010
{
@@ -15,5 +15,20 @@
1515
}
1616
],
1717
"minimum-stability": "dev",
18-
"require": {}
18+
"require": {},
19+
"autoload": {
20+
"psr-4": {
21+
"Magentron\\LaravelScripts\\": "src"
22+
},
23+
"files": [
24+
"helpers.php"
25+
]
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"Magentron\\LaravelScripts\\Providers\\LaravelScriptsServiceProvider"
31+
]
32+
}
33+
}
1934
}

helpers.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* laravel-scripts - helper functions
4+
*
5+
* @author Jeroen Derks <jeroen@derks.it>
6+
* @since 2018/Nov/26
7+
* @license GPLv3 https://www.gnu.org/licenses/gpl.html
8+
* @copyright Copyright (c) 2018 Jeroen Derks / Derks.IT
9+
* @url https://github.com/Magentron/laravel-scripts/
10+
*
11+
* This file is part of laravel-scripts.
12+
*
13+
* laravel-scripts is free software: you can redistribute it and/or modify
14+
* it under the terms of the GNU General Public License as published by
15+
* the Free Software Foundation, either version 3 of the License, or (at
16+
* your option) any later version.
17+
*
18+
* laravel-scripts is distributed in the hope that it will be useful, but
19+
* WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU General Public License along
24+
* with laravel-scripts. If not, see <http://www.gnu.org/licenses/>.
25+
*/
26+
27+
if (! function_exists('dumpVariables')) {
28+
/**
29+
* Dump the passed variables.
30+
*
31+
* @param mixed
32+
* @return void
33+
*/
34+
function dumpVariables()
35+
{
36+
foreach (func_get_args() as $x) {
37+
(new Dumper)->dump($x);
38+
}
39+
}
40+
41+
/**
42+
* Dump the passed variables for CLI output.
43+
*
44+
* @param mixed
45+
* @return void
46+
*/
47+
function dumpVariablesCli()
48+
{
49+
$dumper = new CliDumper;
50+
foreach (func_get_args() as $x) {
51+
$dumper->dump((new VarCloner)->cloneVar($x));
52+
}
53+
}
54+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Develpr\AlexaApp\Provider;
4+
5+
use Illuminate\Contracts\Http\Kernel;
6+
use Illuminate\Support\ServiceProvider;
7+
use ReflectionClass;
8+
use Route;
9+
10+
class LaravelServiceProvider extends ServiceProvider
11+
{
12+
public function boot()
13+
{
14+
$this->publishes([
15+
realpath(__DIR__ . '/../../Envoy.php') => base_path(),
16+
realpath(__DIR__ . '/../../Makefile') => base_path(),
17+
realpath(__DIR__ . '/../../opcache_reset.php') => public_path(),
18+
], 'deployment');
19+
}
20+
}

0 commit comments

Comments
 (0)