Create pre and post deploy notifications in Laravel Forge using Slack
Pro-Tip when working with Laravel Forge, is to send a notification when a deployment is kicked off. Doing this for deployments will inform the team when a deployment starts and ends, allowing you to be informed of the status without having to keep rechecking Forge. In this example I will use Slack.
First you will need your webhook, you can read about how to configuring it here.
Configuring post deploy webhook
Out of the box Forge comes configured with a post deploy webhook that we can configure. To enable, let's go to our Forge project -> Notifications -> Click the "Add Channel" button, select Slack and paste your code in. Now when our deployment completes, we will automatically get a notification in our Slack Channel that deployment completed.
Configuring pre deploy webhook
Unfortunately Forge does not come with a pre deploy webhook like the post deploy one, however we can add one by going to our Forge Project -> Deployment Hooks -> Click "Add Hook" button. In the Script section, you will add:
1url=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX 2 3payload=' 4{ 5 "text": "Deployed to *{{ name }}*", 6 "attachments": [ 7 { 8 "author_name": "{{ author }}", 9 "text": "Code is now being deployed",10 "color": "#00a3e0"11 }12 ]13}'1415curl -s -X POST -H "Content-type: application/json" --data "$payload" $url
If you have multiple servers configured in Forge, it's important to checkbox one of the servers to not be bombarded with multiple messages. You can configure your notification by using a variation of hook variables which you can read about here.
In this article you learned how to send pre and post deploy notifications utilizing Slack, however additional forms of notifications can be configured which will vary by provider.