|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Deployer; |
| 4 | + |
| 5 | +function sendHttpRequest($url, $formParams) |
| 6 | +{ |
| 7 | + $formParams = json_encode($formParams); |
| 8 | + |
| 9 | + $ch = curl_init($url); |
| 10 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 11 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $formParams); |
| 12 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 13 | + curl_setopt( |
| 14 | + $ch, |
| 15 | + CURLOPT_HTTPHEADER, |
| 16 | + [ |
| 17 | + 'Content-Type: application/json', |
| 18 | + 'Content-Length: '.strlen($formParams), |
| 19 | + ] |
| 20 | + ); |
| 21 | + |
| 22 | + return curl_exec($ch); |
| 23 | +} |
| 24 | + |
| 25 | +function sendGroupMessage($subject) |
| 26 | +{ |
| 27 | + $url = get('notify_channel_url'); |
| 28 | + |
| 29 | + if (! $url) { |
| 30 | + throw new \InvalidArgumentException('[Laravel-Deployer]Notification is on but channel url is not set!'); |
| 31 | + } |
| 32 | + |
| 33 | + $notifyBy = get('notify_by', 'webhook'); |
| 34 | + |
| 35 | + switch ($notifyBy) { |
| 36 | + case 'wechat_bot': |
| 37 | + $formParams = [ |
| 38 | + 'msgtype' => 'news', |
| 39 | + 'news' => [ |
| 40 | + 'articles' => [ |
| 41 | + [ |
| 42 | + 'title' => get('user').''.$subject, |
| 43 | + 'description' => '在 '.get('environment').' 环境更新 '.get('branch').' 分支 ', |
| 44 | + 'url' => get('app_repo_url', 'https://github.com'), |
| 45 | + 'picurl' => get('pic_url', 'https://picsum.photos/id/'.rand(1, 1000).'/800/600'), |
| 46 | + ], |
| 47 | + ], |
| 48 | + ], |
| 49 | + ]; |
| 50 | + break; |
| 51 | + default: |
| 52 | + $content = implode("\n", [ |
| 53 | + $subject, |
| 54 | + '应用名称: '.get('application'), |
| 55 | + '发布者: '.get('user'), |
| 56 | + '分支名: '.get('branch'), |
| 57 | + '环境: '.get('environment'), |
| 58 | + ]); |
| 59 | + $formParams = ['text' => $content]; |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + return get('group_notify') ? sendHttpRequest($url, $formParams) : writeln($content); |
| 64 | +} |
| 65 | + |
| 66 | +task('success:notify', function () { |
| 67 | + return sendGroupMessage('成功发布新版本!'); |
| 68 | +})->local(); |
| 69 | + |
| 70 | +task('failed:notify', function () { |
| 71 | + return sendGroupMessage('发布新版本失败!'); |
| 72 | +})->local(); |
0 commit comments