0

I am hosting a discord bot on a Raspberry Pi. This is my first personal project and wanted to know if there was a way to automate git pull without having to remote in my device. That way I can continue to make the discord bot better without having to worry about updating it. Is there a tool that would be helpful for this task?

Thank you for your time.

2 Answers2

4

You can look into cron jobs to run a task periodically (e.g. git pull). This could look something like this:

*/15 * * * * git -C /home/me/gitprojectdir pull

(taken from Git auto-pull using cronjob)

Vincent
  • 482
  • 4
  • 15
  • 1
    You will want to check which user git is set up as. e.g. if you are using any other using other than root this will fail since crons are ran as root. also `which git` to get full git path – Michael Mano Apr 25 '22 at 23:08
  • 1
    @MichaelMano Cronjobs are not run as root, AFAIK. They are run as the user whose crontab they exist in - there is one per user. – Mark Setchell Apr 25 '22 at 23:12
  • 1
    As mentioned you may want to run the pull from a specific user. Depending on your OS, you will do this differently, but cron will be the basic foundation no matter what system you are on. Oh, also, this is not "On a Rasberry Pi" - it is probably on rasbian, but the platform is independent of running things like git, just as an FYI for future questions. – Cfomodz Apr 25 '22 at 23:23
  • Would this approach work for a task that is run infinitely? The bot would always run in my current approach. I am unsure if it starts a new process. – TheStudentProgrammer Apr 26 '22 at 00:22
  • 1
    You would probably need to restart the bot for any changes to take effect. This can be done by configuring it as a system service and using cron to send a restart command. Other than that this should work. Have a look at this post for reference on how to create such a service: https://medium.com/codex/setup-a-python-script-as-a-service-through-systemctl-systemd-f0cc55a42267 – Vincent Apr 26 '22 at 00:24
1

There are solutions that automatically update. Doing it through git pull and restarting the bot works but it isn't a clean solution. I would recommend to dockerize your Discord bot project and run https://github.com/containrrr/watchtower

Watchtower is responsible to update your docker container once a new docker image is released.
In GitHub, Gitlab etc. it is possible to define CI/CD that automatically updates the docker image once you git push new code to it. You can also run some checkers beforehand to make sure you are only deploying code that works.

Tin Nguyen
  • 5,250
  • 1
  • 12
  • 32
  • This is something I was looking into as well. I have seen a little about CI/CD via git actions and did want to take that approach. I have tried using the recommendations above and those work. However, I think this solution is a bit cleaner as you said. I will try this instead. – TheStudentProgrammer Apr 26 '22 at 17:29