Update!

Added github info from https://twitter.com/vistikloft

We’ve been using Slack at the day job for a couple of months now and one of the first things I did was start playing with integrations, mainly bitbucket to post to a slack channel so we can see what everyone is working on.

We are also using Laravel Forge for a custom application and I love the automatic deployments on branch updates. We push changes to a branch and Forge deploys our code to the server without us having to touch it. This is our first foray into Continuous Deployment.

I thought it would be great to get Slack notifications when a new build was deployed so not only do we see the merges happen from bitbucket, but also Forge will update us when the deploy has been completed.

Start off by adding a new incoming web hook to Slack. This will give you a webhook URL complete with a token. You should also select a channel to default post to. I used our “devops” channel which is where we default all our incoming hooks to.

Once you have your incoming web hook configured in Slack, head over to Forge and edit your deploy script. Here a customized version of mine:

cd /home/forge/project.site
git pull origin dev
composer install
php artisan migrate
bower update

Now lets add our web hook:

cd /home/forge/project.site
git pull origin dev
composer install
php artisan migrate
bower update
SHA=$(git rev-parse --verify HEAD)
SHA="https://bitbucket.org/USER/REPO-NAME/commits/$SHA deployed to http://project.site"
curl -X POST --data-urlencode 'payload={"channel": "#devops", "username": "Forge", "text": "'"$SHA"'", "icon_emoji": ":shipit:"}' https://YOUR-SLACK-TEAM.slack.com/services/hooks/incoming-webhook?token=YOUR-UNIQUE-TOKEN

Alternative For Github Users:

cd /home/forge/project.site
git pull origin dev
composer install
php artisan migrate
bower update
SHA=$(git rev-parse --verify HEAD)
SHA="==== NEW DEPLOY ==== \n\n https://github.com/<USERNAME>/<REPO>/commit/$SHA deployed to http://<site.project.com> \n"
curl -X POST --data-urlencode 'payload={"channel": "#devops", "username": "Laravel Forge", "text": "'"$SHA"'", "icon_emoji": ":shipit:"}' https://<USERNAME>.slack.com/services/hooks/incoming-webhook?token=<TOKEN>

In case you are not familiar with what is happening we are running a command to get the current commit hash and store in a variable named SHA. We then concatenate the hash with a generated string to create a custom message with links to the commit on bitbucket, and a link to our project’s URL. Once we have our custom string in the $SHA variable, we use curl to post a JSON payload to our slack webhook URL. The payload has channel name, username, text, and icon options. You can see what we use in the example.

One part that tripped me up was the need to double quote your bash variable in the JSON string, (“‘”$SHA”‘”). A bit of googling and I was able to figure it out.

Now whenever we merge into a branch that Forge is monitoring, we get updates in Slack on deploy with handy links to the commit and the project.

Thanks for reading.