1 | <?php |
||
2 | /* |
||
3 | ## Installing |
||
4 | |||
5 | Add to your _deploy.php_ |
||
6 | |||
7 | ```php |
||
8 | require 'contrib/grafana.php'; |
||
9 | ``` |
||
10 | |||
11 | ## Configuration options |
||
12 | |||
13 | - **url** *(required)*: the URL to the creates annotation api endpoint. |
||
14 | - **token** *(required)*: authentication token. Can be created at Grafana Console. |
||
15 | - **time** *(optional)* – set deploy time of annotation. specify epoch milliseconds. (Defaults is set to the current time in epoch milliseconds.) |
||
16 | - **tags** *(optional)* – set tag of annotation. |
||
17 | - **text** *(optional)* – set text of annotation. (Defaults is set to "Deployed " + git log -n 1 --format="%h") |
||
18 | |||
19 | ```php |
||
20 | // deploy.php |
||
21 | |||
22 | set('grafana', [ |
||
23 | 'token' => 'eyJrIj...', |
||
24 | 'url' => 'http://grafana/api/annotations', |
||
25 | 'tags' => ['deploy', 'production'], |
||
26 | ]); |
||
27 | |||
28 | ``` |
||
29 | |||
30 | ## Usage |
||
31 | |||
32 | If you want to create annotation about successful end of deployment. |
||
33 | |||
34 | ```php |
||
35 | after('success', 'grafana:annotation'); |
||
36 | ``` |
||
37 | |||
38 | */ |
||
39 | namespace Deployer; |
||
40 | |||
41 | use Deployer\Utility\Httpie; |
||
42 | |||
43 | |||
44 | desc('Create Grafana annotation of deployment'); |
||
45 | task('grafana:annotation', function () { |
||
46 | $defaultConfig = [ |
||
47 | 'url' => null, |
||
48 | 'token' => null, |
||
49 | 'time' => round(microtime(true) * 1000), |
||
50 | 'tags' => [], |
||
51 | 'text' => null, |
||
52 | ]; |
||
53 | |||
54 | $config = array_merge($defaultConfig, (array) get('grafana')); |
||
55 | if (!is_array($config) || !isset($config['url']) || !isset($config['token'])) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
56 | throw new \RuntimeException("Please configure Grafana: set('grafana', ['url' => 'https://localhost/api/annotations', token' => 'eyJrIjo...']);"); |
||
57 | } |
||
58 | |||
59 | $params = [ |
||
60 | 'time' => $config['time'], |
||
61 | 'isRegion' => false, |
||
62 | 'tags' => $config['tags'], |
||
63 | 'text' => $config['text'], |
||
64 | ]; |
||
65 | if (!isset($params['text'])) { |
||
66 | $params['text'] = 'Deployed ' . trim(runLocally('git log -n 1 --format="%h"')); |
||
67 | } |
||
68 | |||
69 | Httpie::post($config['url']) |
||
70 | ->header('Authorization: Bearer ' . $config['token']) |
||
71 | ->header('Content-type: application/json') |
||
72 | ->body($params) |
||
73 | ->send(); |
||
74 | }); |
||
75 |