hubot logo

Hubot Documentation

Deploying to Azure

If you’ve been following along with Getting Started, it’s time to deploy so you can use it beyond just your local machine. Azure is a way to deploy hubot as an alternative to Heroku.

You will need to install the azure-cli via npm after you have follow the initial instructions for your hubot.

% npm install -g azure-cli

Inside your new hubot directory, make sure you’ve created a git repository, and that your work is committed:

% git init
% git add .
% git commit -m "Initial commit"

Then create a GitHub repository for your hubot. This is where Azure will pull your code from instead of needing to deploy directly from your dev machine to Azure.

% git remote add origin _your GitHub repo_
% git push -u origin main

Once you have your GitHub repo, create an Azure website linked to your repo. In Azure, create a website and select integrated source control. When it asks “where is your source control” select GitHub and link this website to your git repo that you created in the previous step. If you have downloaded the Azure PowerShell modules, you can also do this via PowerShell.

% $creds = Get-Credential
% New-AzureWebsite mynewhubot -github -githubrepository yourgithubaccount/yourhubotreponame -githubcredentials $creds

Once you have done this, Azure will deploy your site any time you commit and push to GitHub. Your hubot won’t run quite right yet, though. Next, you need to configure the deployment to tell Azure how to run hubot.

First, run the follow command to add deploy.cmd to your hubot directory. This is the file that Azure uses to know how to deploy your node application.

% azure site deploymentscript --node

Then, edit this file and look for the sections that give you steps 1, 2 and 3. You’re going to add a 4th step:

:: 4. Create Hubot file with a js extension
copy /Y "%DEPLOYMENT_TARGET%\node_modules\hubot\bin\hubot" "%DEPLOYMENT_TARGET%\node_modules\hubot\bin\Hubot.mjs"

Now, create a new file in the base directory of hubot called server.js and put these two lines into it:

module.exports = await import('hubot/bin/Hubot.mjs');

Finally you will need to add the environment variables to the website to make sure it runs properly. You can either do it through the GUI (under configuration) or you can use the Azure PowerShell command line, as follows (example is showing slack as an adapter and mynewhubot as the website name).

% $settings = New-Object Hashtable
% $settings["HUBOT_ADAPTER"] = "Slack"
% $settings["HUBOT_SLACK_TOKEN"] = "yourslackapikey"
% Set-AzureWebsite -AppSettings $settings mynewhubot

Commit your changes in git and push to GitHub and Azure will automatically pick up the changes and deploy them to your website.

% git commit -m "Add Azure settings for hubot"
% git push

Azure offers a marketplace where you can use the default heroku-redis-brain using Redis Cloud provided by Redis Labs. Alternatively, to add an Azure blob storage brain, you will need to create an Azure storage account. Then you can do the following in your base hubot directory.

% npm install hubot-azure-brain --save

Then add the following line in external-scripts.json in the list with the other external scripts

"hubot-azure-brain"

Finally, add one more environment variables to your website. You can do this either via the GUI or the following PowerShell commands.

% $settings = New-Object Hashtable
% $settings["HUBOT_BRAIN_AZURE_CONNSTRING"] = "your Azure blob storage connection string"
% Set-AzureWebsite -AppSettings $settings mynewhubot

Now any scripts that require a brain will function. You should look up other scripts or write your own by looking at the documentation. All of the normal scripts for hubot are compatible with hosting hubot on Azure.

Troubleshooting tips and tricks

Due to Azure being Windows-based, you may run into path length problems. To overcome this issue you can set the environment variable IN_PLACE_DEPLOYMENT to 1 and use custom deployment scripts to take advantage of NPM3 and flat module installation.

If you have your instance of hubot deploying directly from source control to Azure, it will perform various bootstrapping tasks along with calling npm install via kudu. Now that Azure supports npm5, this process generates a package-lock.json file which may result in some of your packages failing to install remotely. You can edit deploy.cmd to include --no-package-lock as part of the “Step 3” install command.

If using the free tier of Azure, you can also add a post-deployment step to ping the server on startup by setting the environment variable POST_DEPLOYMENT_ACTION with a script (relative to the src dir) such as startup.sh

An example of a startup script:

let retrys=0
while : ; do
STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" https://${WEBSITE_SITE_NAME}.azurewebsites.net/heroku/keepalive)
echo $STATUSCODE
[[ $retrys -ne 5 ]] || break
echo $retrys
((retrys++))
[[ $STATUSCODE -ne 200 ]] || break
done

Slack Integration

Currently the Slack integration has trouble finding hubot when deployed to Azure. Adding a NODE_PATH application setting with the value D:\home\site\wwwroot\node_modules will allow the Slack module to find hubot.