-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·109 lines (95 loc) · 2.47 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env sh
set -euo pipefail
CRONTAB="* * * * *"
REPOSITORY=""
DIRECTORY=""
CLONE_OPTS=""
PULL_OPTS=""
function usage() {
echo "usage:"
echo " docker run [docker-options] arslivinski/cron-git [options] <repository>"
echo
echo "description:"
echo " A Docker image that clone and periodically pull a Git repository."
echo
echo "options:"
echo " --crontab, -c:"
echo " A crontab expression that defines the peridiocity of the repository pulling. Default \"* * * * *\"".
echo " --directory, -d:"
echo " The directory where the repository will be cloned. If not set, the last segment of the repository's URL will be used."
echo " --clone-opts:"
echo " Additional arguments to be used on git-clone."
echo " --pull-opts:"
echo " Additional arguments to be used on git-pull."
echo " --help, -h:"
echo " Print this text."
echo " <repository>"
echo " The repository's GIT URL."
}
OPTIONS="c:d:h"
LONGOPTIONS="crontab:,directory:,clone-opts:,pull-opts:,help"
PARSED=$(getopt -n "$0" -o "$OPTIONS" -l "$LONGOPTIONS" -- "$@")
eval set -- "$PARSED"
while true; do
case "$1" in
-c|--crontab)
CRONTAB="$2"
shift 2
;;
-d|--directory)
DIRECTORY="$2"
shift 2
;;
--clone-opts)
CLONE_OPTS="$2"
shift 2
;;
--pull-opts)
PULL_OPTS="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
*)
>&2 echo "Error"
exit 2
esac
done
# Validate required arguments
if [[ $# -ne 1 ]]; then
>&2 echo "A repository GIT URL is required."
exit 3
else
REPOSITORY="$1"
fi
# If the directory is not set, use the last segment of the repository's URL
if [ -z "$DIRECTORY" ]; then
DIRECTORY="/$(echo "$REPOSITORY" | sed -E "s/^.*\/(.+)\.git$/\1/")"
fi
echo "Cloning repository \"$REPOSITORY\" into \"$DIRECTORY\""
# WARNING!
# If the directory exists, the contents will be removed.
if [ -d "$DIRECTORY" ]; then
echo "Directory is not empty, removing contents"
find "$DIRECTORY" -mindepth 1 -delete
else
echo "Creating \"$DIRECTORY\""
mkdir -p "$DIRECTORY"
fi
if [ -n "$CLONE_OPTS" ]; then
git clone "$CLONE_OPTS" "$REPOSITORY" "$DIRECTORY"
else
git clone "$REPOSITORY" "$DIRECTORY"
fi
if [ -n "$PULL_OPTS" ]; then
echo "$CRONTAB cd $DIRECTORY && git pull $PULL_OPTS" | crontab -
else
echo "$CRONTAB cd $DIRECTORY && git pull" | crontab -
fi
crond -f -L /dev/stdout & wait