Skip to content

Instantly share code, notes, and snippets.

@JasonMadeSomething
Last active August 15, 2020 23:02
Show Gist options
  • Save JasonMadeSomething/4e557989e3b9fbd71a0bd21ec2168aac to your computer and use it in GitHub Desktop.
Save JasonMadeSomething/4e557989e3b9fbd71a0bd21ec2168aac to your computer and use it in GitHub Desktop.
Notes on how I installed my discord bot on a raspberry PI
#!/bin/sh
echo "post-receive: Activated"
cd /home/pi/botland/prod/spellbook
echo "post-receive: Checking out branch"
git --git-dir=/home/pi/botland/spellbook.git --work-tree=/home/pi/botland/prod/spellbook checkout master -f
echo "post-receive: npm install"
npm install
echo "post-receive: installed"
(pm2 delete 'spellbook' || true)
pm2 start npm --name 'spellbook' -- start
echo 'post-receive: End of deploy script. Please confirm bot is running.'

Installing my first Discord bot

Some notes on how I am getting my first discord bot Spellbook setup on my old Raspberry Pi.

Configuration

The raspberry Pi is a model B+ and the contents of /etc/os-release are as follows:

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Other Pi Environment details

node -v 
v10.22.0
git --version
git version 2.20.1
pm2 --version
4.4.0

Intro

So I didn't do this in an optimal order, and as of writing this sentence, the deployment pipeline isn't built or automated, but currently the bot does exist on the PI and can be run at any time indefinitely. Due to the nature of the bot, it doesn't need to allow incoming connections, so I was able to ignore most parts of the tutorials that covered that. I've included all my sources, however not all of them are needed as I had to backtrack a lot. I'll try to write the steps in an order that will achieve the end goal in as few steps as possible

Step 1: Install all prerequsite software

Because this isn't a tutorial per se, I'm going to assume you know how to install software from the command line.

Step 2: Inital deployment

So I went ahead and just downloaded the project file from github and loaded it into pm2. Yay, my bot worked. But then I noticed I had accidentally downloaded the repo to a subfolder of where I wanted it, so when I moved it up a directory and still had some clutter in the now main directory, I realized I didn't have a great way of automatically syncing my production directory with my master branch (ok, lets be real, its that I don't really know how checkout works). So the motivation I have is to set up a git deployment pipline that will allow me to push code to my pi (initially from my local network, and eventually from anywhere)

It was also at this point I realized I didn't set up SSH keys, which would probably add a little friction to the automation process.

Step 3: Create a bare repo for a pipeline

This part is still a little mystical to me, so lets see how it goes:

I want to keep all my bots in a directory ~/botland

  1. git init --bare ~/botland/spellbook.git
  2. git clone ~/botland/spellbook.git ~/botland/prod/spellbook
  3. touch ~botland/prod/spellbook/.git/hooks/post-receive
  4. touch ~botland/spellbook.git/hooks/post-receive

I then added a modified version of the post-receive script from the "Friendly Guide" below.

Step 4 Local Computer setup

My Pi's name on the network is ras, so to connect to it I use ras.local. I would like to expand this later to connect from the internet.

I added the remote per the instructions in the tutorial like so: git remote add prod ssh://pi@ras.local/home/pi/botland/spellbook.git and then ran git push prod master

Step 5: Troubleshooting

When I push to prod, the script is clearly running, and git seems to think something got updated, becasue I have to make a commit because I can push again, however the files don't actually exist anywhere. There must be a problem in my script. The error I keep getting is that ~/botland/spellbook.git isn't a repo. Even when I expicitly set the path I get the error. pm2 isn't launching either as best I can tell (what would it even lauch?). Turns out I didn't have the hook in the right place, so that was keeping it from running, however when it does run, I still get the error. Not sure how the git hook is being triggered when its in something that is not a git repo.

I' forgot the "/" in front oh the path's. Thats how. The script now appears to echo everything it should and it looks like its running in pm2, but it isn't running on discord. This is becasue the .env file with the API key didn't come over.

Step 6: Managing the API Key

Just needed to add .env to the file ~/botland/spellbook.git/info/exclude then run touch ~/botland/prod/spellbook/.env and place the API key in the .env file. Subsequent testing shows that the .env file isn't wiped out.

Step 7. Further Troubleshooting

Now that the files successfully make it to the Pi server, I can begin verifying pm2. Unfortunately, even though pm2 says theres a process named "spellbook" running, discord isn't showing the bot as online. This is leading me to believe that the line pm2 start npm --name 'spellbook' -- start isn't actually starting the program so much as spawning a process named spellbook. On further reseach, it looks like I need to add a line to my package.json file to allow npm to launch the program. "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, Becomes: "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node spellbook.js" }, And now things appear to work!

Step 8

TODO:

  1. Add Testing
  2. Automate testing
  3. Prevent production deployment if tests fail
  4. Add support for multiple bots in prod

Sources:

Run your Node.js application on a headless Raspberry Pi

Stack Overflow Question Related to Above

A Friendly Guide To Automate Deployment For Node.js Apps With Git Hooks

Deploy a website to a remote server with Git push

Simple automated GIT Deployment using GIT Hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment