1 | <?php |
||
2 | /* |
||
3 | ### Installing |
||
4 | |||
5 | ```php |
||
6 | // deploy.php |
||
7 | |||
8 | require 'contrib/sentry.php'; |
||
9 | ``` |
||
10 | |||
11 | ### Configuration options |
||
12 | |||
13 | - **organization** *(required)*: the slug of the organization the release belongs to. |
||
14 | - **projects** *(required)*: array of slugs of the projects to create a release for. |
||
15 | - **token** *(required)*: authentication token. Can be created at [https://sentry.io/settings/account/api/auth-tokens/] |
||
16 | - **version** *(required)* – a version identifier for this release. |
||
17 | Can be a version number, a commit hash etc. (Defaults is set to git log -n 1 --format="%h".) |
||
18 | - **version_prefix** *(optional)* - a string prefixed to version. |
||
19 | Releases are global per organization so indipentent projects needs to prefix version number with unique string to avoid conflicts |
||
20 | - **environment** *(optional)* - the environment you’re deploying to. By default framework's environment is used. |
||
21 | For example for symfony, *symfony_env* configuration is read otherwise defaults to 'prod'. |
||
22 | - **ref** *(optional)* – an optional commit reference. This is useful if a tagged version has been provided. |
||
23 | - **refs** *(optional)* - array to indicate the start and end commits for each repository included in a release. |
||
24 | Head commits must include parameters *repository* and *commit*) (the HEAD sha). |
||
25 | They can optionally include *previousCommit* (the sha of the HEAD of the previous release), |
||
26 | which should be specified if this is the first time you’ve sent commit data. |
||
27 | - **commits** *(optional)* - array commits data to be associated with the release. |
||
28 | Commits must include parameters *id* (the sha of the commit), and can optionally include *repository*, |
||
29 | *message*, *author_name*, *author_email* and *timestamp*. By default will send all new commits, |
||
30 | unless it's a first release, then only first 200 will be sent. |
||
31 | - **url** *(optional)* – a URL that points to the release. This can be the path to an online interface to the sourcecode for instance. |
||
32 | - **date_released** *(optional)* – date that indicates when the release went live. If not provided the current time is assumed. |
||
33 | - **sentry_server** *(optional)* – sentry server (if you host it yourself). defaults to hosted sentry service. |
||
34 | - **date_deploy_started** *(optional)* - date that indicates when the deploy started. Defaults to current time. |
||
35 | - **date_deploy_finished** *(optional)* - date that indicates when the deploy ended. If not provided, the current time is used. |
||
36 | - **deploy_name** *(optional)* - name of the deploy |
||
37 | - **git_version_command** *(optional)* - the command that retrieves the git version information (Defaults is set to git log -n 1 --format="%h", other options are git describe --tags --abbrev=0) |
||
38 | |||
39 | ```php |
||
40 | // deploy.php |
||
41 | |||
42 | set('sentry', [ |
||
43 | 'organization' => 'exampleorg', |
||
44 | 'projects' => [ |
||
45 | 'exampleproj' |
||
46 | ], |
||
47 | 'token' => 'd47828...', |
||
48 | 'version' => '0.0.1', |
||
49 | |||
50 | ]); |
||
51 | ``` |
||
52 | |||
53 | ### Suggested Usage |
||
54 | |||
55 | Since you should only notify Sentry of a successful deployment, the deploy:sentry task should be executed right at the end. |
||
56 | |||
57 | ```php |
||
58 | // deploy.php |
||
59 | |||
60 | after('deploy', 'deploy:sentry'); |
||
61 | ``` |
||
62 | |||
63 | */ |
||
64 | namespace Deployer; |
||
65 | |||
66 | use Closure; |
||
67 | use DateTime; |
||
68 | use Deployer\Utility\Httpie; |
||
69 | |||
70 | desc('Notifying Sentry of deployment'); |
||
71 | task( |
||
72 | 'deploy:sentry', |
||
73 | static function () { |
||
74 | $now = date('c'); |
||
75 | |||
76 | $defaultConfig = [ |
||
77 | 'version' => null, |
||
78 | 'version_prefix' => null, |
||
79 | 'refs' => [], |
||
80 | 'ref' => null, |
||
81 | 'commits' => null, |
||
82 | 'url' => null, |
||
83 | 'date_released' => $now, |
||
84 | 'date_deploy_started' => $now, |
||
85 | 'date_deploy_finished' => $now, |
||
86 | 'sentry_server' => 'https://sentry.io', |
||
87 | 'previous_commit' => null, |
||
88 | 'environment' => get('symfony_env', 'prod'), |
||
89 | 'deploy_name' => null, |
||
90 | ]; |
||
91 | |||
92 | if (releaseIsGitDirectory()) { |
||
93 | $defaultConfig['version'] = getReleaseGitRef(); |
||
94 | $defaultConfig['commits'] = getGitCommitsRefs(); |
||
95 | } |
||
96 | |||
97 | $config = array_merge($defaultConfig, (array) get('sentry')); |
||
98 | array_walk( |
||
99 | $config, |
||
100 | static function (&$value) use ($config) { |
||
101 | if (is_callable($value)) { |
||
102 | $value = $value($config); |
||
103 | } |
||
104 | } |
||
105 | ); |
||
106 | |||
107 | if ( |
||
108 | !isset($config['organization'], $config['token'], $config['version']) |
||
109 | || (empty($config['projects']) || !is_array($config['projects'])) |
||
110 | ) { |
||
111 | throw new \RuntimeException( |
||
112 | <<<EXAMPLE |
||
113 | Required data missing. Please configure sentry: |
||
114 | set( |
||
115 | 'sentry', |
||
116 | [ |
||
117 | 'organization' => 'exampleorg', |
||
118 | 'projects' => [ |
||
119 | 'exampleproj', |
||
120 | 'exampleproje2' |
||
121 | ], |
||
122 | 'token' => 'd47828...', |
||
123 | ] |
||
124 | );" |
||
125 | EXAMPLE |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | $releaseData = array_filter( |
||
130 | [ |
||
131 | 'version' => ($config['version_prefix'] ?? '') . $config['version'], |
||
132 | 'refs' => $config['refs'], |
||
133 | 'ref' => $config['ref'], |
||
134 | 'url' => $config['url'], |
||
135 | 'commits' => array_slice($config['commits'] ?? [], 0), // reset keys to serialize as array in json |
||
136 | 'dateReleased' => $config['date_released'], |
||
137 | 'projects' => $config['projects'], |
||
138 | 'previousCommit' => $config['previous_commit'], |
||
139 | ] |
||
140 | ); |
||
141 | |||
142 | $releasesApiUrl = $config['sentry_server'] . '/api/0/organizations/' . $config['organization'] . '/releases/'; |
||
143 | $response = Httpie::post( |
||
144 | $releasesApiUrl |
||
145 | ) |
||
146 | ->header(sprintf('Authorization: Bearer %s', $config['token'])) |
||
147 | ->body($releaseData) |
||
148 | ->getJson(); |
||
149 | |||
150 | if (!isset($response['version'], $response['projects'])) { |
||
151 | throw new \RuntimeException(sprintf('Unable to create a release: %s', print_r($response, true))); |
||
152 | } |
||
153 | |||
154 | writeln( |
||
155 | sprintf( |
||
156 | '<info>Sentry:</info> Release of version <comment>%s</comment> ' . |
||
157 | 'for projects: <comment>%s</comment> created successfully.', |
||
158 | $response['version'], |
||
159 | implode(', ', array_column($response['projects'], 'slug')) |
||
160 | ) |
||
161 | ); |
||
162 | |||
163 | $deployData = array_filter( |
||
164 | [ |
||
165 | 'environment' => $config['environment'], |
||
166 | 'name' => $config['deploy_name'], |
||
167 | 'url' => $config['url'], |
||
168 | 'dateStarted' => $config['date_deploy_started'], |
||
169 | 'dateFinished' => $config['date_deploy_finished'], |
||
170 | ] |
||
171 | ); |
||
172 | |||
173 | $response = Httpie::post( |
||
174 | $releasesApiUrl . $response['version'] . '/deploys/' |
||
175 | ) |
||
176 | ->header(sprintf('Authorization: Bearer %s', $config['token'])) |
||
177 | ->body($deployData) |
||
178 | ->getJson(); |
||
179 | |||
180 | if (!isset($response['id'], $response['environment'])) { |
||
181 | throw new \RuntimeException(sprintf('Unable to create a deployment: %s', print_r($response, true))); |
||
182 | } |
||
183 | |||
184 | writeln( |
||
185 | sprintf( |
||
186 | '<info>Sentry:</info> Deployment <comment>%s</comment> ' . |
||
187 | 'for environment <comment>%s</comment> created successfully', |
||
188 | $response['id'], |
||
189 | $response['environment'] |
||
190 | ) |
||
191 | ); |
||
192 | } |
||
193 | ); |
||
194 | |||
195 | function releaseIsGitDirectory() |
||
196 | { |
||
197 | return (bool) run('cd {{release_path}} && git rev-parse --git-dir > /dev/null 2>&1 && echo 1 || echo 0'); |
||
198 | } |
||
199 | |||
200 | function getReleaseGitRef(): Closure |
||
201 | { |
||
202 | return static function ($config = []): string { |
||
203 | cd('{{release_path}}'); |
||
204 | |||
205 | if (isset($config['git_version_command'])) { |
||
206 | return trim(run($config['git_version_command'])); |
||
207 | } |
||
208 | |||
209 | return trim(run('git log -n 1 --format="%h"')); |
||
210 | }; |
||
211 | } |
||
212 | |||
213 | function getGitCommitsRefs(): Closure |
||
214 | { |
||
215 | return static function ($config = []): array { |
||
0 ignored issues
–
show
|
|||
216 | $previousReleaseRevision = null; |
||
217 | |||
218 | if (has('previous_release')) { |
||
219 | cd('{{previous_release}}'); |
||
220 | $previousReleaseRevision = trim(run('git rev-parse HEAD')); |
||
221 | } |
||
222 | |||
223 | if ($previousReleaseRevision === null) { |
||
224 | $commitRange = 'HEAD'; |
||
225 | } else { |
||
226 | $commitRange = $previousReleaseRevision . '..HEAD'; |
||
227 | } |
||
228 | |||
229 | cd('{{release_path}}'); |
||
230 | |||
231 | try { |
||
232 | $result = run(sprintf('git rev-list --pretty="%s" %s', 'format:%H#%an#%ae#%at#%s', $commitRange)); |
||
233 | $lines = array_filter( |
||
234 | // limit number of commits for first release with many commits |
||
235 | array_map('trim', array_slice(explode("\n", $result), 0, 200)), |
||
236 | static function (string $line): bool { |
||
237 | return !empty($line) && strpos($line, 'commit') !== 0; |
||
238 | } |
||
239 | ); |
||
240 | |||
241 | return array_map( |
||
242 | static function (string $line): array { |
||
243 | list($ref, $authorName, $authorEmail, $timestamp, $message) = explode('#', $line, 5); |
||
244 | |||
245 | return [ |
||
246 | 'id' => $ref, |
||
247 | 'author_name' => $authorName, |
||
248 | 'author_email' => $authorEmail, |
||
249 | 'message' => $message, |
||
250 | 'timestamp' => date(DateTime::ATOM, (int) $timestamp), |
||
251 | ]; |
||
252 | }, |
||
253 | $lines |
||
254 | ); |
||
255 | |||
256 | } catch (\Deployer\Exception\RunException $e) { |
||
257 | writeln($e->getMessage()); |
||
258 | return []; |
||
259 | } |
||
260 | }; |
||
261 | } |
||
262 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.