Some notes on how I am getting my first discord bot Spellbook setup on my old Raspberry Pi.
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
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
Because this isn't a tutorial per se, I'm going to assume you know how to install software from the command line.
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.
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
git init --bare ~/botland/spellbook.git
git clone ~/botland/spellbook.git ~/botland/prod/spellbook
touch ~botland/prod/spellbook/.git/hooks/post-receive
touch ~botland/spellbook.git/hooks/post-receive
I then added a modified version of the post-receive script from the "Friendly Guide" below.
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
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.
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.
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!
- Add Testing
- Automate testing
- Prevent production deployment if tests fail
- 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