|
| 1 | +<?php |
| 2 | +/* |
| 3 | +## Installing |
| 4 | +
|
| 5 | +Microsoft is retiring the legacy "Incoming Webhook" connector. New |
| 6 | +integrations must use the **Workflows** app (Power Automate). This |
| 7 | +recipe targets the Workflows "Post to a channel when a webhook request |
| 8 | +is received" template. |
| 9 | +
|
| 10 | +Setup: |
| 11 | +1. Open MS Teams |
| 12 | +2. Navigate to Teams section |
| 13 | +3. Select existing or create new team |
| 14 | +4. Select existing or create new channel |
| 15 | +5. Click the three dots on the channel, choose "Workflows" |
| 16 | +6. Pick the template **Post to a channel when a webhook request is received** |
| 17 | +7. Follow the wizard, then copy the generated HTTPS POST URL |
| 18 | +8. Setup deploy.php |
| 19 | + Add in header: |
| 20 | +```php |
| 21 | +require 'contrib/ms-teams-workflows.php'; |
| 22 | +set('teams_workflows_webhook', 'https://prod-XX.westeurope.logic.azure.com:443/workflows/...'); |
| 23 | +``` |
| 24 | +Add in content: |
| 25 | +```php |
| 26 | +before('deploy', 'teams-workflows:notify'); |
| 27 | +after('deploy:success', 'teams-workflows:notify:success'); |
| 28 | +after('deploy:failed', 'teams-workflows:notify:failure'); |
| 29 | +``` |
| 30 | +9.) Sip your coffee |
| 31 | +
|
| 32 | +## Configuration |
| 33 | +
|
| 34 | +- `teams_workflows_webhook` – workflow HTTPS POST URL, **required** |
| 35 | + ``` |
| 36 | + set('teams_workflows_webhook', 'https://prod-XX.westeurope.logic.azure.com:443/workflows/...'); |
| 37 | + ``` |
| 38 | +- `teams_workflows_title` – the title of application, default `{{application}}` |
| 39 | +- `teams_workflows_text` – notification message template |
| 40 | + ``` |
| 41 | + set('teams_workflows_text', '_{{user}}_ deploying `{{what}}` to *{{where}}*'); |
| 42 | + ``` |
| 43 | +- `teams_workflows_success_text` – success template, default: |
| 44 | + ``` |
| 45 | + set('teams_workflows_success_text', 'Deploy to *{{where}}* successful'); |
| 46 | + ``` |
| 47 | +- `teams_workflows_failure_text` – failure template, default: |
| 48 | + ``` |
| 49 | + set('teams_workflows_failure_text', 'Deploy to *{{where}}* failed'); |
| 50 | + ``` |
| 51 | +- `teams_workflows_failure_continue` – if `true`, errors talking to the |
| 52 | + workflow endpoint are downgraded to warnings instead of aborting the |
| 53 | + deploy. Default `false`. |
| 54 | +
|
| 55 | +## Usage |
| 56 | +
|
| 57 | +If you want to notify only about beginning of deployment add this line only: |
| 58 | +
|
| 59 | +```php |
| 60 | +before('deploy', 'teams-workflows:notify'); |
| 61 | +``` |
| 62 | +
|
| 63 | +If you want to notify about successful end of deployment add this too: |
| 64 | +
|
| 65 | +```php |
| 66 | +after('deploy:success', 'teams-workflows:notify:success'); |
| 67 | +``` |
| 68 | +
|
| 69 | +If you want to notify about failed deployment add this too: |
| 70 | +
|
| 71 | +```php |
| 72 | +after('deploy:failed', 'teams-workflows:notify:failure'); |
| 73 | +``` |
| 74 | + */ |
| 75 | + |
| 76 | +namespace Deployer; |
| 77 | + |
| 78 | +use Deployer\Utility\Httpie; |
| 79 | + |
| 80 | +// Title of project |
| 81 | +set('teams_workflows_title', function () { |
| 82 | + return get('application', 'Project'); |
| 83 | +}); |
| 84 | + |
| 85 | +// Allow Continue on Failure |
| 86 | +set('teams_workflows_failure_continue', false); |
| 87 | + |
| 88 | +// Deploy message |
| 89 | +set('teams_workflows_text', '_{{user}}_ deploying `{{what}}` to *{{where}}*'); |
| 90 | +set('teams_workflows_success_text', 'Deploy to *{{where}}* successful'); |
| 91 | +set('teams_workflows_failure_text', 'Deploy to *{{where}}* failed'); |
| 92 | + |
| 93 | +desc('Notifies Teams (Workflows)'); |
| 94 | +task('teams-workflows:notify', function () { |
| 95 | + if (!get('teams_workflows_webhook', false)) { |
| 96 | + warning('No MS Teams Workflows webhook configured'); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + Httpie::post(get('teams_workflows_webhook'))->jsonBody([ |
| 102 | + 'text' => get('teams_workflows_text'), |
| 103 | + ])->send(); |
| 104 | + } catch (\Exception $e) { |
| 105 | + if (get('teams_workflows_failure_continue', false)) { |
| 106 | + warning('Error sending Teams Workflows Notification: ' . $e->getMessage()); |
| 107 | + } else { |
| 108 | + throw $e; |
| 109 | + } |
| 110 | + } |
| 111 | +}) |
| 112 | + ->once() |
| 113 | + ->hidden(); |
| 114 | + |
| 115 | +desc('Notifies Teams (Workflows) about deploy finish'); |
| 116 | +task('teams-workflows:notify:success', function () { |
| 117 | + if (!get('teams_workflows_webhook', false)) { |
| 118 | + warning('No MS Teams Workflows webhook configured'); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + try { |
| 123 | + Httpie::post(get('teams_workflows_webhook'))->jsonBody([ |
| 124 | + 'text' => get('teams_workflows_success_text'), |
| 125 | + ])->send(); |
| 126 | + } catch (\Exception $e) { |
| 127 | + if (get('teams_workflows_failure_continue', false)) { |
| 128 | + warning('Error sending Teams Workflows Notification: ' . $e->getMessage()); |
| 129 | + } else { |
| 130 | + throw $e; |
| 131 | + } |
| 132 | + } |
| 133 | +}) |
| 134 | + ->once() |
| 135 | + ->hidden(); |
| 136 | + |
| 137 | +desc('Notifies Teams (Workflows) about deploy failure'); |
| 138 | +task('teams-workflows:notify:failure', function () { |
| 139 | + if (!get('teams_workflows_webhook', false)) { |
| 140 | + warning('No MS Teams Workflows webhook configured'); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + Httpie::post(get('teams_workflows_webhook'))->jsonBody([ |
| 146 | + 'text' => get('teams_workflows_failure_text'), |
| 147 | + ])->send(); |
| 148 | + } catch (\Exception $e) { |
| 149 | + if (get('teams_workflows_failure_continue', false)) { |
| 150 | + warning('Error sending Teams Workflows Notification: ' . $e->getMessage()); |
| 151 | + } else { |
| 152 | + throw $e; |
| 153 | + } |
| 154 | + } |
| 155 | +}) |
| 156 | + ->once() |
| 157 | + ->hidden(); |
0 commit comments