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