add readme, support multiple repositories

AuthorKonata <konata@posteo.jp>
Date
Commit927124c28b22cecc56406da3a50f2329bdc1018f
Parentfa7c9b0
4 files changed, 33 insertions(+), 9 deletions(-)
M.env
@@ -1,3 +1,3 @@
11 PUSH_KEY=
22 PUSH_DELAY=
3-PUSH_URL=
3+PUSH_URLS=
AREADME.md
@@ -0,0 +1,20 @@
1+# git-autopush
2+
3+Script+container image that periodically mirrors a local repository to a remote location, e.g. keep a gitlab/github mirror up-to-date with a repo hosted on a private server
4+
5+Example `docker run` command:
6+`docker run --rm --env-file .env -v ./ssh:/ssh:ro -v ./git:/git:ro "$(docker build -q .)"`
7+
8+`./ssh` should contain your key for pushing
9+`./git` should contain the repositores you want to push
10+
11+### .env parameters
12+* PUSH_KEY: file name of the private ssh key in the `ssh` directory to be used for pushing, e.g. `id_rsa`
13+* PUSH_DELAY: how long to sleep between pushes, should be a string compatible with sleep(1)
14+* PUSH_URLS: a `|` and `space` separated list of urls and directory names, e.g. `git@someserver:myrepo myrepo.git|git@someserver:myotherrepo myrepo2.git`
15+ * `git@someserver:myrepo` and `git@someserver:myotherrepo` are the urls passed to git as a push destination
16+ * `myrepo.git` and `myrepo2.git` are directory names relates to `/git` inside the container
17+
18+### TODO
19+* allow custom refspecs to be added (for e.g. git-bug)
20+* port script to python and use json config instead of environment variables
DTODO.md
-2
@@ -1,2 +0,0 @@
1-* allow custom refspecs to be added (for e.g. git-bug)
2-* allow multiple repositories
Mautopush.sh
@@ -10,18 +10,24 @@ if [ -z "$PUSH_DELAY" ]; then
1010 exit 1
1111 fi
1212
13-if [ -z "$PUSH_URL" ]; then
14- echo "Please set PUSH_DELAY in .env to a string compatible with sleep(1)"
13+if [ -z "$PUSH_URLS" ]; then
14+ echo "Please set PUSH_URLS in .env"
1515 exit 1
1616 fi
1717
1818 cd /git
19-git rev-parse --git-dir || (echo "No repository detected at /git" && exit 1)
20-
2119 #use global config to not modify the repository
2220 git config --global core.sshCommand "/usr/bin/ssh -o 'StrictHostKeyChecking no' -i /ssh/$PUSH_KEY"
2321 while true; do
24- #using --mirror causes errors when pushing, but --prune,--force, plus these refspecs should catch most stuff
25- git push --force --prune "$PUSH_URL" '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' '+refs/change/*:refs/change/*' || true
22+ IFS="|"
23+ for PAIR in $PUSH_URLS; do
24+ #URL comes first, because they can't contain spaces, but directories can
25+ URL="$(echo $PAIR | cut -d ' ' -f 1)"
26+ DIR="$(echo $PAIR | cut -d ' ' -f 2-)"
27+ cd "$DIR"
28+ #using --mirror causes errors when pushing, but --prune,--force, plus these refspecs should catch most stuff
29+ git push --force --prune "$URL" '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' '+refs/change/*:refs/change/*' || true
30+ cd - > /dev/null
31+ done
2632 sleep "$PUSH_DELAY"
2733 done