1

I am trying to host a Discord bot for my friends. We have a GitHub respository that we all have access to and push the code to. I am trying to do the following

  • Once a push is detected, the server automatically runs git pull in the project folder so the new code from the GitHub is now in the project folder
  • To restart the bot, once a pull is detected run pm2 restart 0 to restart the bot with the new code.

I am running Ubuntu 18.04 Server

Anybody have any suggestions on how to do this?

1 Answers1

2

You're looking for github webhooks. You'll likely need to set up a web server on that Ubuntu box that github can ping when certain events are triggered in your repo.

The server can then verify that the request came from github (see securing your webhooks), and run some script (this can be a basic bash script) that performs your site maintenance (backup, pull, redeploy, etc.).

Bahrom
  • 4,752
  • 32
  • 41
  • Any suggestions on how to this? I’m kind of new to this so I’m not sure how to put this all together – thetechguy61705 Jan 29 '20 at 16:41
  • What tech stack are you familiar with? Github has an example of a sinatra server [here](https://developer.github.com/webhooks/configuring/#writing-the-server). You could also write a basic [node](https://nodejs.org/en/docs/guides/getting-started-guide/) server – Bahrom Jan 29 '20 at 16:44
  • 1
    Could I use a shell script to do this? Like it would listen to the even then run the command? – thetechguy61705 Jan 29 '20 at 16:44
  • Node would be better, I’m better with JavaScript – thetechguy61705 Jan 29 '20 at 16:45
  • I've never looked into writing a server in bash, but it should be [possible](https://github.com/benrady/shinatra). Node should be simpler - create a bash script with the actions you want on a redeploy:`git pull; pm2 restart 0; #etc.`. Then write a server that [executes](https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js) the script when you hit say `/redeploy`. – Bahrom Jan 29 '20 at 16:49
  • Ugh! I thought this would be something easy I could put together in 5 mins. I found a GitHub repository called `git-auto-deploy` which claims to do what I need, but I can’t seem to get it working. My main question about this library is, how do I get GitHub to communicate with it. In the config there is only a space for a repository link, not for a webhook, and it doesn’t seem to be working – thetechguy61705 Jan 29 '20 at 16:55
  • If you're talking about [this](https://github.com/olipo186/Git-Auto-Deploy), it looks like that's what the library is doing under the hood anyway (except the server is written in python). It might be easier to write your own node server. You really just need a basic server that instead of echoing back a `"Hello, world!"` executes a bash script with like 3 commands `cd bot_directory; git pull; pm2 restart 0`. – Bahrom Jan 29 '20 at 17:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206868/discussion-between-thetechguy61705-and-bahrom). – thetechguy61705 Jan 29 '20 at 17:11
  • Yes that’s it. You think a simple node server would be easier? I’m not sure how to put a node server together that talks with GitHub – thetechguy61705 Jan 29 '20 at 17:12