Home > other >  How to pull an online GitHub repository every night via CRON?
How to pull an online GitHub repository every night via CRON?

Time:08-12

I have a local web server on my NAS inside my home, with a small VM running Caddy's webserver. I have cloned the repository https://github.com/openspeedtest/Speed-Test to a local folder (/usr/local/www/Speed-Test), and configured Caddy to serve up that folder as a website - so far, so good. I can access the site and test speeds on my LAN without issue.

I'd like to set up a CRON job to pull from that web repository on a nightly basis, and pull down the current files, replacing the existing local ones. Because it's only a bunch of static HTML files, there's no need to configure anything local, and I'll automatically get the latest updates without needing to manually pull them.

I did search online, but most of the results I am looking for involve merging existing changes or such. I'm just looking to overwrite the existing folder with the current repository, and I can't seem to find the code / arguments to do this.

Thank you!

CodePudding user response:

If you just want to have a local one-way mirror you can put the following into a script that is invoked by cron

#!/bin/bash

if [ "$1" = "--random-delay" ]
then
        sleep $(awk "BEGIN { srand(); print rand()*$2*60 }")
fi

cd /usr/local/www/Speed-Test
git checkout main
git fetch origin
git reset --hard origin/main

It will discard any local commits and modifications and reset the files to match the latest content of the main branch of the remote repository.

If you put an entry into crontab on a specific time (as opposed to adding a script to /etc/cron.daily), it is nice to add a bit of randomness into when the script runs (e.g. to avoid that "everyone" starts pulling from github at exactly 1:00 at night for instance) which you can do by invoking it as /some/where/update-speed-test.sh --random-delay 20 for instance.

CodePudding user response:

Of course, about an hour after I posted this, I thought of a solution - use a script called by CRON, instead of just a command:

rm -rf /usr/local/www/Speed-Test/*

rm -rf /usr/local/www/Speed-Test/.*

git clone https://github.com/openspeedtest/Speed-Test /usr/local/www/Speed-Test

I set that up last night, and confirmed it worked as intended this morning.

  • Related