Create a GitHub bot for automatic contributions

Create a GitHub bot for automatic contributions

Have you ever wondered how non-tech guys have such impressive contribution stats on their Github profiles? Yes, they have set up a GitHub bot that uploads the same file over and over again to the GitHub repo.

This piece of content is a part of my “automation” manifesto, which I declare here: I would automate everything with Python from now on.

Requirements:

  1. Create git repository on GitHub
  2. VPS with Git init folder to the remote repository
  3. GitHub ssh key set up
  4. Proper crontab job for your bot

GitHub bot

Bot consists of 2 files, the main file on Python, which generate the file(will be uploaded to GitHub) and the bash file, which will run this python script and execute the “git commands”(git commit, git push to the remote origin)

How to Create an SSH Key

To use an SSH key with Git, you must first create the key on your computer. If you already have an SSH key, you can skip these steps. To check if you have a key, you can run this command:

 bro@server# ssh-add -l

Follow along with the steps below to create the key and copy its contents. You will need to open your computer’s default terminal application.

  1. Open the terminal app on your computer.
  2. Enter the following command, substituting joe@example.com with your email address:
    bro@server# ssh-keygen -t rsa -b 4096 -C "joe@example.com"
  3. Press Enter to accept the default file location.
  4. Enter a secure passphrase.
  5. Press Enter.
  6. Enter this command to display the contents of your public key:
     bro@server# cat .ssh/id_rsa.pub
  7. Copy the contents of your key to your clipboard (we will need it later).

It’s important to remember that the SSH key has two parts: a private key and a public key. As the name suggests, the private key is only for private use. Never share your private key with anyone (with exceptions made for your system administrator or other trusted people within your organization). This will be important later.

Init your folder on the VPS with git

First, you create a repository in the GitHub profile, then switch to your VPS, select a folder and init it.

How to init a folder on remote VPS:

Init your folder on the VPS with git
Init your folder on the VPS with git

Crontab job

To set a crontab job you can use those commands: crontab -e or nano /etc/crontab

Learn more on how you can properly set up a crontab job with Ubuntu VPS

this is my command from the crontab, each hour on 11 minutes – cron will run rand_gen.sh script, that script will execute python code from rand_gen.py and after git adds/ git commit/ git push to the origin repository.

11 * * * * root /bin/sh /home/pythonscripts/9autocommit/rand_gen.sh

Sources:
GitHub repo with python and bash code: https://github.com/webprice/9autocommit
Github Gist with code: https://gist.github.com/webprice/faeffaae5d7d01747a3cd6a12171d60c


Comments

One response to “Create a GitHub bot for automatic contributions”

Leave a Reply

Your email address will not be published. Required fields are marked *