< YALU STUDIO >

Build a bot with Lambda to auto update my GitHub

Word count: 853 / Reading time: 5 min
2019/10/16 Share

In job ads, companies usually request candidates to have an active GitHub account to demonstrate their passion for programming. However, lack of green tiles in the heatmap doesn’t necessarily mean that the person is not coding at all (well, in most of the time it is true). So, I decided to build a bot to contribute for me every day just in case I’m not in the mood of committing anything on GitHub. The idea is pretty simple and straight-forward. I can make some slight modifications in the file and then commit and push to GitHub. That’s it!

The initial bash script is like this:

1
2
3
4
5
6
git clone https://github.com/maoyalu/github_greener_bot.git
cd github_greener_bot
echo Updated: ${date} > log.txt
git add .
git commit -m "Auto update"
git push origin master

It’s just some basic git operations that everyone knows. To make it as fun as possible, I decided to put it on Lambda. One of the reason is that currently I don’t have a server to run my task and I just feel that it’s not an urgent demand for me right now. But the main reason is that I want to explore more about Lambda Function and try to get a taste of it.

The first barrier I met is git command not found. There is no Git installed on Lambda!!! Come to think about it, it’s a function, of course there is no git available. Then how can I use it?

After researching on Google, I found two repos that can help with that:

  • pimterry/lambda-git

    It’s a git binary installed through NPM. Basically, you npm install this package locally and then zip and upload everything on lambda.

  • lambci/git-lambda-layer

    It’s a layer for Lambda that allows your functions to use git and ssh binaries. I decided to go with this since it is a more recommended way and also I can keep my function small.

    Obviously, I then switched the runtime to Node.js. I’m not a master of shell scripting anyway. Then the second problem came. As the new nodejs10.x runtime uses a completely different OS, both methods are not yet compatible with that runtime. At least, at the time of writing (October 16th, 2019), it doesn’t work. So, I have to use the nodejs8.10 instead for now.

After a lot of trial-and-errors, my final script looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const { execSync } = require('child_process');

exports.handler = async(event) => {
// Configure in Lambda environment
let token = process.env.GH_TOKEN;
let email = process.env.GH_EMAIL;
let user = process.env.GH_USERNAME;
let repo = process.env.GH_REPO;

// Customize commit message
let commitMessage = "Lazy...";

execSync('rm -rf /tmp/*')
execSync(`git clone https://github.com/${user}/${repo}.git /tmp/bot`)
execSync('echo Updated: $(date) > /tmp/bot/log.txt')
execSync(`cd /tmp/bot && git remote set-url origin https://${user}:${token}@github.com/${user}/${repo}.git && git config --local user.name ${user} && git config --local user.email ${email} && git add . && git commit -m "${commitMessage}" && git push`)
}

Then I set up the environment variables so that my token is not hard-coded in the script. BTW, authentication took me quite some time as well. I don’t want my password to show up in the script, and then I found that I can generate a token for use in the Developer Settings. Nice! As stated in the documentation, environment variables are encrypted at runtime, so it should be fine. Notice that it is not encrypted when you upload your sensitive information to Lambda, so it is recommended that you use their Key Management Service (KMS) to encrypt the token before you upload it to Lambda. I’m not sure if AWS charged me $0.1 for creating a KMS key. My recommendation is that if your GitHub account is full of rubbish just like me, you can leave anything as default.

Oh, I also try my best to make it easier to customize for other people’s use, although I think nobody except me would do this kind of silly thing. LOL

The last step is to add a trigger to run the function. We have two options, Rate and Cron. If you don’t know how to write them, Schedule Expressions Using Rate or Cron here it has excellent examples that you can directly use with slight adjustments.

And… That’s it! I know it’s a silly thing to do, but I had fun setting everything up.

If I would improve this bot by any chance, maybe it would better if it can commit for random times, or support other operations like pull requests and issues. And if I do commit myself, it can take a rest. I hope HR won’t be mad at me for this when they check my GitHub.

Life is so hard without a PR.

Treat yourself better.

Let the bot keeps your GitHub profile green for you.

PEACE

CATALOG