I love Slack. It’s an amazing tool that helps teams effectively communicate and get things done.
Some parts of the software development workflow, such as project deployment, include repeatable sequences, which are usually prone to mistakes, and are simply meant to be automated away.
My team works on separate feature branches, so I wanted my command to take a branch name as a parameter.
The slash command should look something like this:
/deploy-website [branch name]
After invoking that command, I want that branch checked out from the version control and all the latest changes pulled down to the development server.
Enter Hookdoo — your hooks in the cloud
When you invoke the slash command from the Slack’s message box, it will hit a defined hook endpoint with the necessary payload, so let’s go ahead and create our hook!
Go to my.hookdoo.com and log in, or create an account if you don’t have one. If you are a new user, you will receive a free month of the Micro plan so you can try Hookdoo out, with no strings attached and no payment details required.
After logging in, I am greeted with the “Hook invocation history” page. It is basically a dashboard that contains my recent hook invocations. Since I don’t have any hooks defined yet, it is empty, so let’s go ahead and set up a hook that will handle our Slack slash command.
Step 1 — Create the hook that will handle the Slash command
Upon clicking the “Hooks” link in the header I am presented with the Hooks list page. From there, I click on the “Create a new hook” button and it leads me to the form where I can set up when and how my hook will be executed, what context values should be available to my shell commands, and the shell commands that I want to execute.
I type in the hook name, set the concurrency strategy to ignore all subsequent requests until the script finishes the execution, and then trigger the next execution with the last incoming webhook payload. So if someone triggers the Slash command while a previous hook is executing, it will not start a parallel deployment process that will interfere with the current one.
Now, remember I want to deploy provided branch name from the command. Slack documentation says that the slash command webhook will send parameter named text which will include everything after the slash command. To use that value from the request in our script, we will use environment variables.
Click on “Add environment variable” button opens up a popup where I can define environment variables that will contain our request values.
In the “Environment variable name” field I type in FEATURE_BRANCH.
From the “Value” options, I select “Request value”, which then expands itself to allow me to specify which value should be extracted.
Since the Slash command’s payload will contain our feature branch in the text parameter, in the “Request value key” I type in “text”, from the “Source” I choose “payload” and for the “Value type” I choose “string”.
Finally I click on the “Add environment variable” to save my input.
FEATURE_BRANCH environment variable will now be available in our shell script and will contain the supplied branch name. Cool!
Now we’re only left to write the shell commands to pull the changes from the remote repository and execute the script that will perform the necessary build & deploy steps. I could have also copied the contents of that script right there in Hookdoo, but to me it’s more convenient to have that script in the repository.
Clicking on “Submit” saves our hook.
For that hook to do anything meaningful, we need to add a server where it will be executed.
In Hookdoo, it’s possible to assign a single hook to multiple servers so if, for example, you have multiple servers behind a load balancer, you can execute defined shell scripts on all of them simultaneously.
Step 2 — Adding our server
Simple click to the “Servers” link in the header presents me with a Servers list page.
Clicking “Add a new server” button I get to fill out a simple form with SSH credentials that will be used to connect to the server and execute my custom shell scripts.
Now, since we know that we want to execute our “Deploy feature branch” hook on this server, we can assign it immediately, so from the “Assign hooks” section, we select the “Deploy feature branch” hook, and click on the “Add hook” button.
By clicking the “Submit” button, my server is ready to use right away, and the hook has been assigned to it!
Now we’re ready for the last step!
Step 3 — Adding custom slash command to Slack
Go to your Slack, and click on your Slack team name to bring up the menu.
From the menu popup, select “Manage apps”, and you will be taken to the page which will let you manage Slack integrations.
In the search box located in the top header, type in “slash commands”, and then select the “Slash Commands” integration.
Click on the “Add Configuration” button to define a new “Slash command”.
Type in the slash command name and click on the “Add Slash Command Integration” button.
Now let’s quickly go back to Hookdoo, open up the “Hooks” page and click on the “View endpoint” of the hook that we want our command to trigger. This will reveal the HTTP endpoint that Hookdoo has assigned to our hook.
Click “Copy to clipboard”, and paste that in the “URL” field of the “Integration settings”.
Scroll down, and enable the “Show this command in the autocomplete list” option to display our Slash command in the Slack.
Click on the “Save Integration” button and we’re all done!
Now I can type “/deploy-website development” in my Slack chat box and get the latest code from development branch live on my server!
Pretty neat, huh?
Check out more about Hookdoo at https://www.hookdoo.com/