1 | <?php |
||
2 | /* (c) beeete2 <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer; |
||
9 | |||
10 | use Deployer\Utility\Httpie; |
||
11 | |||
12 | |||
13 | desc('Create Grafana annotation of deployment'); |
||
14 | task('grafana:annotation', function () { |
||
15 | $defaultConfig = [ |
||
16 | 'url' => null, |
||
17 | 'token' => null, |
||
18 | 'time' => round(microtime(true) * 1000), |
||
19 | 'tags' => [], |
||
20 | 'text' => null, |
||
21 | ]; |
||
22 | |||
23 | $config = array_merge($defaultConfig, (array) get('grafana')); |
||
24 | if (!is_array($config) || !isset($config['url']) || !isset($config['token'])) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
25 | throw new \RuntimeException("Please configure Grafana: set('grafana', ['url' => 'https://localhost/api/annotations', token' => 'eyJrIjo...']);"); |
||
26 | } |
||
27 | |||
28 | $params = [ |
||
29 | 'time' => $config['time'], |
||
30 | 'isRegion' => false, |
||
31 | 'tags' => $config['tags'], |
||
32 | 'text' => $config['text'], |
||
33 | ]; |
||
34 | if (!isset($params['text'])) { |
||
35 | $params['text'] = 'Deployed ' . trim(runLocally('git log -n 1 --format="%h"')); |
||
36 | } |
||
37 | |||
38 | Httpie::post($config['url']) |
||
39 | ->header('Authorization: Bearer ' . $config['token']) |
||
40 | ->header('Content-type: application/json') |
||
41 | ->body($params) |
||
42 | ->send(); |
||
43 | }); |
||
44 |