|
| 1 | +<?php |
| 2 | +/* |
| 3 | +
|
| 4 | +### Configuration options |
| 5 | +
|
| 6 | +- **api_key** *(required)*: Check to "Project Settings" to find the API key necessary to authenticate the Create Event request. |
| 7 | +- **name** *(optional)*: Name of the release, defaults to Deployer release name |
| 8 | +- **name_prefix** *(optional)*: a string prefixed to version. |
| 9 | +- **type** *(optional)*: Type of the event. Currently supports `release` and `marker`, defaults to `release`. |
| 10 | +- **description** *(optional)*: More details about the release. |
| 11 | +- **environment** *(optional)*: The environment this release is performed on, otherwise the default environment is used. |
| 12 | +- **service** *(optional)*: The service this release is performed on, otherwise the default service of the project is used. |
| 13 | +- **compareAfterMinutes** *(optional)*: Defaults to 90 minutes. Specifies the timeframes around the event for which the performance will be compared. Supported values are between 5 minutes and 1440 minutes (one day). |
| 14 | +
|
| 15 | +```php |
| 16 | +// deploy.php |
| 17 | +
|
| 18 | +set('tideways', [ |
| 19 | + 'api_key' => 'Qm8JxL2...' |
| 20 | +]); |
| 21 | +``` |
| 22 | +
|
| 23 | +### Suggested Usage |
| 24 | +
|
| 25 | +Since you should only notify Tideways of a successful deployment, the deploy:tideways task should be executed right at the end. |
| 26 | +
|
| 27 | +```php |
| 28 | +// deploy.php |
| 29 | +
|
| 30 | +after('deploy', 'deploy:tideways'); |
| 31 | +``` |
| 32 | +
|
| 33 | + */ |
| 34 | + |
| 35 | +namespace Deployer; |
| 36 | + |
| 37 | +use Deployer\Utility\Httpie; |
| 38 | + |
| 39 | +desc('Notifies Tideways of deployment'); |
| 40 | +task( |
| 41 | + 'deploy:tideways', |
| 42 | + static function () { |
| 43 | + $defaultConfig = [ |
| 44 | + 'name' => get('release_name'), |
| 45 | + 'type' => 'release', |
| 46 | + 'description' => null, |
| 47 | + 'environment' => null, |
| 48 | + 'service' => null, |
| 49 | + 'compareAfterMinutes' => 90 |
| 50 | + ]; |
| 51 | + |
| 52 | + $config = array_merge($defaultConfig, (array) get('tideways')); |
| 53 | + array_walk( |
| 54 | + $config, |
| 55 | + static function (&$value) use ($config) { |
| 56 | + if (is_callable($value)) { |
| 57 | + $value = $value($config); |
| 58 | + } |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + if ( |
| 63 | + !isset($config['api_key']) |
| 64 | + || empty($config['api_key']) |
| 65 | + ) { |
| 66 | + throw new \RuntimeException( |
| 67 | + <<<EXAMPLE |
| 68 | + Required data missing. Please configure tideways: |
| 69 | + set( |
| 70 | + 'tideways', |
| 71 | + [ |
| 72 | + 'api_key' => 'Qm8JxL2...', |
| 73 | + ] |
| 74 | + );" |
| 75 | + EXAMPLE, |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + $releaseData = array_filter( |
| 80 | + [ |
| 81 | + 'apiKey' => $config['api_key'], |
| 82 | + 'name' => ($config['name_prefix'] ?? '') . $config['name'], |
| 83 | + 'description' => $config['description'], |
| 84 | + 'type' => $config['type'], |
| 85 | + 'environment' => $config['environment'], |
| 86 | + 'service' => $config['service'], |
| 87 | + 'compareAfterMinutes' => $config['compareAfterMinutes'] |
| 88 | + ], |
| 89 | + ); |
| 90 | + |
| 91 | + $response = Httpie::post('https://app.tideways.io/api/events') |
| 92 | + ->setopt(CURLOPT_TIMEOUT, 10) |
| 93 | + ->jsonBody($releaseData) |
| 94 | + ->getJson(); |
| 95 | + |
| 96 | + if (!isset($response['ok'], $response['id']) || $response['ok'] !== true) { |
| 97 | + throw new \RuntimeException(sprintf('Unable to create a release: %s', print_r($response, true))); |
| 98 | + } |
| 99 | + |
| 100 | + writeln( |
| 101 | + sprintf( |
| 102 | + '<info>Tideways:</info> Release of version <comment>%s</comment> ' |
| 103 | + . 'for project: <comment>%s</comment> created successfully.', |
| 104 | + $releaseData['name'], |
| 105 | + $response['name'], |
| 106 | + ), |
| 107 | + ); |
| 108 | + }, |
| 109 | +); |
0 commit comments