Skip to content

Commit 94c5ebc

Browse files
authored
Add Tideways deployment notification task
This file contains a task to notify Tideways of deployments, including configuration options and usage instructions.
1 parent 8206661 commit 94c5ebc

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

contrib/tideways.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/*
3+
4+
### Configuration options
5+
6+
- **api_key** *(required)*: Tideways API key for authentication.
7+
- **version** *(required)*: A version identifier for this release. Can be a version number, a commit hash etc. (Default is set to git log -n 1 --format="%h".)
8+
- **environment** *(optional)*: The environment you're deploying to. Defaults to 'production'.
9+
- **service** *(optional)*: The service name for the release. Defaults to 'web'.
10+
- **compare_after_minutes** *(optional)*: Time in minutes to compare performance before/after release. Defaults to 90.
11+
- **project** *(optional)*: Project name/path for the description field.
12+
- **description** *(optional)*: Custom description for the release.
13+
- **tideways_server** *(optional)*: Tideways server URL. Defaults to 'https://app.tideways.io'.
14+
- **git_version_command** *(optional)*: The command that retrieves the git version information. Defaults to 'git log -n 1 --format="%h"'.
15+
16+
```php
17+
// deploy.php
18+
19+
set('tideways', [
20+
'api_key' => 'your-api-key',
21+
'version' => '',
22+
'environment' => 'production',
23+
'service' => 'web',
24+
'compare_after_minutes' => 90,
25+
]);
26+
```
27+
28+
### Suggested Usage
29+
30+
Since you should only notify Tideways of a successful deployment, the deploy:tideways task should be executed right at the end.
31+
32+
```php
33+
// deploy.php
34+
35+
after('deploy:success', 'deploy:tideways');
36+
```
37+
38+
*/
39+
40+
namespace Deployer;
41+
42+
use Deployer\Utility\Httpie;
43+
44+
desc('Notifies Tideways of deployment');
45+
task(
46+
'deploy:tideways',
47+
static function () {
48+
$defaultConfig = [
49+
'version' => getReleaseGitRef(),
50+
'environment' => 'production',
51+
'service' => 'web',
52+
'compare_after_minutes' => 90,
53+
'project' => null,
54+
'description' => null,
55+
'tideways_server' => 'https://app.tideways.io',
56+
];
57+
58+
$config = array_merge($defaultConfig, (array) get('tideways'));
59+
array_walk(
60+
$config,
61+
static function (&$value) use ($config) {
62+
if (is_callable($value)) {
63+
$value = $value($config);
64+
}
65+
},
66+
);
67+
68+
if (!isset($config['api_key']) || empty($config['api_key'])) {
69+
writeln('<comment>Skipping Tideways release creation: api_key not set</comment>');
70+
return;
71+
}
72+
73+
if (!isset($config['version']) || empty($config['version'])) {
74+
throw new \RuntimeException(
75+
<<<EXAMPLE
76+
Required data missing. Please configure tideways:
77+
set(
78+
'tideways',
79+
[
80+
'api_key' => 'your-api-key',
81+
'version' => '1.0.0',
82+
]
83+
);
84+
EXAMPLE,
85+
);
86+
}
87+
88+
$payload = [
89+
'apiKey' => $config['api_key'],
90+
'name' => $config['version'],
91+
'type' => 'release',
92+
'environment' => $config['environment'],
93+
'service' => $config['service'],
94+
'compareAfterMinutes' => (int) $config['compare_after_minutes'],
95+
];
96+
97+
// Add description if provided or generate from project
98+
if (!empty($config['description'])) {
99+
$payload['description'] = $config['description'];
100+
} elseif (!empty($config['project'])) {
101+
$payload['description'] = "Release {$config['version']} for project {$config['project']}";
102+
}
103+
104+
$eventsApiUrl = $config['tideways_server'] . '/api/events';
105+
106+
try {
107+
Httpie::post($eventsApiUrl)
108+
->setopt(CURLOPT_TIMEOUT, 10)
109+
->header('Content-Type', 'application/json')
110+
->body(json_encode($payload))
111+
->send();
112+
113+
writeln(
114+
sprintf(
115+
'<info>Tideways:</info> Release <comment>%s</comment> ' .
116+
'for environment <comment>%s</comment> and service <comment>%s</comment> created successfully.',
117+
$config['version'],
118+
$config['environment'],
119+
$config['service'],
120+
),
121+
);
122+
} catch (\Throwable $e) {
123+
writeln('<error>Failed to create Tideways release: ' . $e->getMessage() . '</error>');
124+
throw $e;
125+
}
126+
},
127+
);
128+
129+
function getReleaseGitRef(): \Closure
130+
{
131+
return static function ($config = []): string {
132+
if (get('update_code_strategy') === 'archive') {
133+
if (isset($config['git_version_command'])) {
134+
cd('{{deploy_path}}/.dep/repo');
135+
136+
return trim(run($config['git_version_command']));
137+
}
138+
139+
return run('cat {{current_path}}/REVISION');
140+
}
141+
142+
cd('{{release_path}}');
143+
144+
if (isset($config['git_version_command'])) {
145+
return trim(run($config['git_version_command']));
146+
}
147+
148+
return trim(run('git log -n 1 --format="%h"'));
149+
};
150+
}
151+

0 commit comments

Comments
 (0)