1 | <?php |
||
2 | namespace Deployer; |
||
3 | |||
4 | require_once __DIR__ . '/common.php'; |
||
5 | |||
6 | set('shared_dirs', ['storage']); |
||
7 | set('shared_files', ['.env']); |
||
8 | set('writable_dirs', [ |
||
9 | 'bootstrap/cache', |
||
10 | 'storage', |
||
11 | 'storage/app', |
||
12 | 'storage/app/public', |
||
13 | 'storage/framework', |
||
14 | 'storage/framework/cache', |
||
15 | 'storage/framework/sessions', |
||
16 | 'storage/framework/views', |
||
17 | 'storage/logs', |
||
18 | ]); |
||
19 | set('log_files', 'storage/logs/*.log'); |
||
20 | set('laravel_version', function () { |
||
21 | $result = run('{{bin/php}} {{release_path}}/artisan --version'); |
||
22 | preg_match_all('/(\d+\.?)+/', $result, $matches); |
||
23 | return $matches[0][0] ?? 5.5; |
||
24 | }); |
||
25 | |||
26 | /** |
||
27 | * Run an artisan command. |
||
28 | * |
||
29 | * Supported options: |
||
30 | * - 'min' => #.#: The minimum Laravel version required (included). |
||
31 | * - 'max' => #.#: The maximum Laravel version required (included). |
||
32 | * - 'skipIfNoEnv': Skip and warn the user if `.env` file is inexistant or empty. |
||
33 | * - 'failIfNoEnv': Fail the command if `.env` file is inexistant or empty. |
||
34 | * - 'runInCurrent': Run the artisan command in the current directory. |
||
35 | * - 'showOutput': Show the output of the command if given. |
||
36 | * |
||
37 | * @param string $command The artisan command (with cli options if any). |
||
38 | * @param array $options The options that define the behaviour of the command. |
||
39 | * @return callable A function that can be used as a task. |
||
40 | */ |
||
41 | function artisan($command, $options = []) |
||
42 | { |
||
43 | return function () use ($command, $options) { |
||
44 | $versionTooEarly = array_key_exists('min', $options) |
||
45 | && laravel_version_compare($options['min'], '<'); |
||
46 | |||
47 | $versionTooLate = array_key_exists('max', $options) |
||
48 | && laravel_version_compare($options['max'], '>'); |
||
49 | |||
50 | if ($versionTooEarly || $versionTooLate) { |
||
51 | return; |
||
52 | } |
||
53 | if (in_array('failIfNoEnv', $options) && !test('[ -s {{release_path}}/.env ]')) { |
||
54 | throw new \Exception('Your .env file is empty! Cannot proceed.'); |
||
55 | } |
||
56 | if (in_array('skipIfNoEnv', $options) && !test('[ -s {{release_path}}/.env ]')) { |
||
57 | warning("Your .env file is empty! Skipping...</>"); |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | $artisan = in_array('runInCurrent', $options) |
||
62 | ? '{{deploy_path}}/current/artisan' |
||
63 | : '{{release_path}}/artisan'; |
||
64 | |||
65 | $output = run("{{bin/php}} $artisan $command"); |
||
66 | |||
67 | if (in_array('showOutput', $options)) { |
||
68 | writeln("<info>$output</info>"); |
||
69 | } |
||
70 | }; |
||
71 | } |
||
72 | |||
73 | function laravel_version_compare($version, $comparator) |
||
74 | { |
||
75 | return version_compare(get('laravel_version'), $version, $comparator); |
||
76 | } |
||
77 | |||
78 | desc('Disable maintenance mode'); |
||
79 | task('artisan:up', artisan('up', ['runInCurrent', 'showOutput'])); |
||
80 | |||
81 | desc('Enable maintenance mode'); |
||
82 | task('artisan:down', artisan('down', ['runInCurrent', 'showOutput'])); |
||
83 | |||
84 | desc('Execute artisan migrate'); |
||
85 | task('artisan:migrate', artisan('migrate --force', ['skipIfNoEnv']))->once(); |
||
86 | |||
87 | desc('Execute artisan migrate:fresh'); |
||
88 | task('artisan:migrate:fresh', artisan('migrate:fresh --force')); |
||
89 | |||
90 | desc('Execute artisan migrate:rollback'); |
||
91 | task('artisan:migrate:rollback', artisan('migrate:rollback --force', ['showOutput'])); |
||
92 | |||
93 | desc('Execute artisan migrate:status'); |
||
94 | task('artisan:migrate:status', artisan('migrate:status', ['showOutput'])); |
||
95 | |||
96 | desc('Execute artisan db:seed'); |
||
97 | task('artisan:db:seed', artisan('db:seed --force', ['showOutput'])); |
||
98 | |||
99 | desc('Execute artisan cache:clear'); |
||
100 | task('artisan:cache:clear', artisan('cache:clear')); |
||
101 | |||
102 | desc('Execute artisan config:clear'); |
||
103 | task('artisan:config:clear', artisan('config:clear')); |
||
104 | |||
105 | desc('Execute artisan config:cache'); |
||
106 | task('artisan:config:cache', artisan('config:cache')); |
||
107 | |||
108 | desc('Execute artisan route:cache'); |
||
109 | task('artisan:route:cache', artisan('route:cache')); |
||
110 | |||
111 | desc('Execute artisan view:clear'); |
||
112 | task('artisan:view:clear', artisan('view:clear')); |
||
113 | |||
114 | desc('Execute artisan view:cache'); |
||
115 | task('artisan:view:cache', artisan('view:cache', ['min' => 5.6])); |
||
116 | |||
117 | desc('Execute artisan optimize'); |
||
118 | task('artisan:optimize', artisan('optimize', ['max' => 5.7])); |
||
119 | |||
120 | desc('Execute artisan optimize:clear'); |
||
121 | task('artisan:optimize:clear', artisan('optimize:clear', ['min' => 5.7])); |
||
122 | |||
123 | desc('Execute artisan queue:restart'); |
||
124 | task('artisan:queue:restart', artisan('queue:restart')); |
||
125 | |||
126 | desc('Execute artisan storage:link'); |
||
127 | task('artisan:storage:link', artisan('storage:link', ['min' => 5.3])); |
||
128 | |||
129 | desc('Execute artisan horizon:assets'); |
||
130 | task('artisan:horizon:assets', artisan('horizon:assets')); |
||
131 | |||
132 | desc('Execute artisan horizon:publish'); |
||
133 | task('artisan:horizon:publish', artisan('horizon:publish')); |
||
134 | |||
135 | desc('Execute artisan horizon:terminate'); |
||
136 | task('artisan:horizon:terminate', artisan('horizon:terminate')); |
||
137 | |||
138 | desc('Execute artisan telescope:clear'); |
||
139 | task('artisan:telescope:clear', artisan('telescope:clear')); |
||
140 | |||
141 | desc('Execute artisan telescope:prune'); |
||
142 | task('artisan:telescope:prune', artisan('telescope:prune')); |
||
143 | |||
144 | desc('Execute artisan telescope:publish'); |
||
145 | task('artisan:telescope:publish', artisan('telescope:publish')); |
||
146 | |||
147 | desc('Execute artisan nova:publish'); |
||
148 | task('artisan:nova:publish', artisan('nova:publish')); |
||
149 | |||
150 | desc('Execute artisan event:clear'); |
||
151 | task('artisan:event:clear', artisan('event:clear', ['min' => '5.8.9'])); |
||
152 | |||
153 | desc('Execute artisan event:cache'); |
||
154 | task('artisan:event:cache', artisan('event:cache', ['min' => '5.8.9'])); |
||
155 | |||
156 | /** |
||
157 | * Task deploy:public_disk support the public disk. |
||
158 | * To run this task automatically, please add below line to your deploy.php file |
||
159 | * |
||
160 | * before('deploy:symlink', 'deploy:public_disk'); |
||
161 | * |
||
162 | * [Laravel filesystem configuration](https://laravel.com/docs/5.2/filesystem#configuration) |
||
163 | */ |
||
164 | desc('Make symlink for public disk'); |
||
165 | task('deploy:public_disk', function () { |
||
166 | // Remove from source. |
||
167 | run('if [ -d $(echo {{release_path}}/public/storage) ]; then rm -rf {{release_path}}/public/storage; fi'); |
||
168 | |||
169 | // Create shared dir if it does not exist. |
||
170 | run('mkdir -p {{deploy_path}}/shared/storage/app/public'); |
||
171 | |||
172 | // Symlink shared dir to release dir |
||
173 | run('{{bin/symlink}} {{deploy_path}}/shared/storage/app/public {{release_path}}/public/storage'); |
||
174 | }); |
||
175 | |||
176 | /** |
||
177 | * Main deploy task. |
||
178 | */ |
||
179 | desc('Deploy your project'); |
||
180 | task('deploy', [ |
||
181 | 'deploy:prepare', |
||
182 | 'deploy:vendors', |
||
183 | 'artisan:storage:link', |
||
184 | 'artisan:view:cache', |
||
185 | 'artisan:config:cache', |
||
186 | 'deploy:publish', |
||
187 | ]); |
||
188 |