1+ @servers ([' web' => $user . ' @' . $host ,' localhost' => ' 127.0.0.1' ] )
2+
3+ @setup
4+ // Sanity checks
5+ if (empty($host)) {
6+ exit('ERROR: $host var empty or not defined');
7+ }
8+ if (empty($user)) {
9+ exit('ERROR: $user var empty or not defined');
10+ }
11+ if (empty($path)) {
12+ exit('ERROR: $path var empty or not defined');
13+ }
14+ if (empty($build)) {
15+ exit('ERROR: $build var empty or not defined');
16+ }
17+ if (empty($commit)) {
18+ exit('ERROR: $commit var empty or not defined');
19+ }
20+
21+ if (file_exists($path) || is_writable($path)) {
22+ exit("ERROR: cannot access $path");
23+ }
24+
25+ // Ensure given $path is a potential web directory (/home/* or /var/www/*)
26+ if (!(preg_match("/(\/home\/|\/var\/www\/)/i", $path) === 1)) {
27+ exit('ERROR: $path provided doesn\'t look like a web directory path?');
28+ }
29+
30+ $current_release_dir = $path . '/current';
31+ $releases_dir = $path . '/releases';
32+ $new_release_dir = $releases_dir . '/' . $build . '_' . $commit;
33+
34+ $remote = $user . '@' . $host . ':' . $new_release_dir;
35+
36+ // Command or path to invoke PHP
37+ $php = empty($php) ? 'php' : $php;
38+ @endsetup
39+
40+ @story (' deploy' )
41+ rsync
42+ manifest_file
43+ setup_symlinks
44+ verify_install
45+ activate_release
46+ optimise
47+ migrate
48+ cleanup
49+ @endstory
50+
51+ @task (' debug' , [' on' => ' localhost' ] )
52+ ls -la {{ $dir } }
53+ @endtask
54+
55+ @task (' rsync' , [' on' => ' localhost' ] )
56+ echo "* Deploying code from {{ $dir } } to {{ $remote } } *"
57+ # https://explainshell.com/explain?cmd=rsync+-zrSlh+--exclude-from%3Ddeployment-exclude-list.txt+.%2F.+%7B%7B+%24remote+%7D%7D
58+ rsync -zrSlh --stats --exclude-from=deployment-exclude-list.txt {{ $dir } } / {{ $remote } }
59+ @endtask
60+
61+ @task (' manifest_file' , [' on' => ' web' ] )
62+ echo "* Writing deploy manifest file *"
63+ echo -e "{\"build\":\""{{ $build } } "\", \"commit\":\""{{ $commit } } "\", \"branch\":\""{{ $branch } } "\"}" > {{ $new_release_dir } } /deploy-manifest.json
64+ @endtask
65+
66+ @task (' setup_symlinks' , [' on' => ' web' ] )
67+ echo "* Linking .env file to new release dir ({{ $path } } /.env -> {{ $new_release_dir } } /.env) *"
68+ ln -nfs {{ $path } } /.env {{ $new_release_dir } } /.env
69+
70+ if [ -f {{ $new_release_dir } } /storage ]; then
71+ echo "* Moving existing storage dir *"
72+ mv {{ $new_release_dir } } /storage {{ $new_release_dir } } /storage.orig 2>/dev/null
73+ fi
74+
75+ echo "* Linking storage directory to new release dir ({{ $path } } /storage -> {{ $new_release_dir } } /storage) *"
76+ ln -nfs {{ $path } } /storage {{ $new_release_dir } } /storage
77+ @endtask
78+
79+ @task (' verify_install' , [' on' => ' web' ] )
80+ echo "* Verifying install ({{ $new_release_dir } } ) *"
81+ cd {{ $new_release_dir } }
82+ {{ $php } } artisan --version
83+ @endtask
84+
85+ @task (' activate_release' , [' on' => ' web' ] )
86+ echo "* Activating new release ({{ $new_release_dir } } -> {{ $current_release_dir } } ) *"
87+ ln -nfs {{ $new_release_dir } } {{ $current_release_dir } }
88+ @endtask
89+
90+ @task (' migrate' , [' on' => ' web' ] )
91+ echo '* Running migrations *'
92+ cd {{ $new_release_dir } }
93+ {{ $php } } artisan migrate --force
94+ @endtask
95+
96+ @task (' optimise' , [' on' => ' web' ] )
97+ echo '* Clearing cache and optimising *'
98+ cd {{ $new_release_dir } }
99+
100+ {{ $php } } artisan cache:clear
101+ {{ $php } } artisan config:clear
102+ {{ $php } } artisan route:clear
103+ {{ $php } } artisan view:clear
104+
105+ # https://laravel.com/docs/5.5/deployment#optimization
106+ {{ $php } } artisan config:cache
107+ # Only use when no closure used in routes
108+ #{{ $php } } artisan route:cache
109+
110+ #echo '* Reloading php-fpm *'
111+ #sudo -S service php7.1-fpm reload
112+
113+ #echo '* Gracefully terminating Laravel Horizon *'
114+ #{{ $php } } artisan horizon:terminate
115+ #sudo supervisorctl stop horizon # workaround
116+ #sudo supervisorctl start horizon # workaround
117+ @endtask
118+
119+ @task (' cleanup' , [' on' => ' web' ] )
120+ echo "* Executing cleanup command in {{ $releases_dir } } *"
121+ ls -dt {{ $releases_dir } } /*/ | tail -n +4 | xargs rm -rf
122+ @endtask
0 commit comments