Automation is love. We all love automating repetitive things. There is one such thing called Probot. Probot is one of the most popular frameworks for developing GitHub Apps using Javascript. It is easy to set up as most of the things like setting up authentication, registering webhooks, managing permissions, are all handled by Probot itself. You just need to write your code for responding to different events that happen on GitHub.
In this article, you’ll build a serverless bot and deploy it to Netlify Functions. The advantage of using Netlify Functions is that it is free for up to 125,000 requests per month which is more than enough for a startup or a small organization.
Contents
Prerequisites
- You can follow this guide on how to set up Probot.
Writing Application Logic
Once your Probot is set up, you need to do some changes to your directory structure to deploy it to Netlify Functions.
Create an src directory and add the following code in a new file - app.js:
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
app.log.info('App has loaded');
app.on('issues.opened', async (context) => {
context.octokit.issues.createComment(
context.issue({
body: 'Thanks for opening this issue!',
})
);
});
};
In the above code whenever a new issue is opened, it creates an issue comment thanking the issue author.
Netlify Functions are AWS Lambda functions but their deployment to AWS is handled by Netlify. For deploying your Probot on Netlify, you can use the AWS Lambda adapter for Probot.
In your terminal, execute the following command:
npm install @probot/adapter-aws-lambda-serverless
The next thing you need to do is to create a functions directory that will be used by Netlify to deploy your serverless functions. Every JS file in the functions directory is deployed as an individual function which can be accessed at <domain>/.netlify/functions/<function_name>
.
In the functions directory, create an index.js file and add the following code:
const { createLambdaFunction, createProbot } = require('@probot/adapter-aws-lambda-serverless');
const app = require('../src/app');
module.exports.handler = createLambdaFunction(app, {
probot: createProbot(),
});
At this point, your code is finally done and the next step is to deploy your application to Netlify.
Deploying on Netlify Functions
Before proceeding with setup for deployment, there are some issues to be addressed. You need to create a configuration file, netlify.toml at the root of the project, and tell some important things to Netlify to consider when deploying your bot.
Add the following code in the netlify.toml:
[build]
command = "npm install --production"
functions = "./functions"
You are telling Netlify to run npm install
before deploying your functions which are present in the functions directory.
To deploy on Netlify, you can use Netlify Dev. For that you need to install netlify-cli by doing:
npm install netlify-cli -g
Next, log in to your Netlify account by doing:
netlify login
Once you are logged in, connect your local project directory to Netlify Functions. You can either connect to an existing one or create a new one by doing:
netlify init
Once your site is connected, you can build your site locally and deploy it to Netlify by doing:
netlify build
netlify deploy --prod
You can also connect your GitHub Repository to your Netlify project or use GitHub Actions to deploy your bot to Netlify. {.alert alert-info}
Updating Webhook URL
Once you have deployed your Probot, you need to update the Webhook URL to tell Github where to send the event payloads. To do so, visit [https://github.com/settings/apps/<app-name>](https://github.com/settings/apps/<app-name>)
and update the Webhook URL with your Netlify website URL.
Results
You are done with developing and deploying your Probot and now need to test it by creating an issue on a repository where you installed your GitHub app and see whether your bot responds back or not.
Awesome! You can see that your bot welcomed you with a message that you wrote in your code.
There are many things to automate on Github like auto assigning developers to the issues, auto assigning reviewers to a pull request, auto merging pull requests created by dependabot alerts, and much more.