example_post-receive.sh (2707B)
1 #!/bin/sh 2 # generic git post-receive hook. 3 # change the config options below and call this script in your post-receive 4 # hook or symlink it. 5 # 6 # usage: $0 [name] 7 # 8 # if name is not set the basename of the current directory is used, 9 # this is the directory of the repo when called from the post-receive script. 10 11 # NOTE: needs to be set for correct locale (expects UTF-8) otherwise the 12 # default is LC_CTYPE="POSIX". 13 export LC_CTYPE="en_US.UTF-8" 14 15 name="$1" 16 if test "${name}" = ""; then 17 name=$(basename "$(pwd)") 18 fi 19 20 # config 21 # paths must be absolute. 22 reposdir="/srv/git" 23 dir="${reposdir}/${name}" 24 htmldir="/var/www/localhost/htdocs" 25 stagitdir="/" 26 destdir="${htmldir}${stagitdir}" 27 cachefile=".htmlcache" 28 # /config 29 30 if ! test -d "${dir}"; then 31 echo "${dir} does not exist" >&2 32 exit 1 33 fi 34 cd "${dir}" || exit 1 35 36 # detect git push -f 37 force=0 38 while read -r old new ref; do 39 test "${old}" = "0000000000000000000000000000000000000000" && continue 40 test "${new}" = "0000000000000000000000000000000000000000" && continue 41 42 hasrevs=$(git rev-list "${old}" "^${new}" | sed 1q) 43 if test -n "${hasrevs}"; then 44 force=1 45 break 46 fi 47 done 48 49 # strip .git suffix. 50 r=$(basename "${name}") 51 d=$(basename "${name}" ".git") 52 printf "[%s] stagit HTML pages... " "${d}" 53 54 mkdir -p "${destdir}/${d}" 55 cd "${destdir}/${d}" || exit 1 56 57 # remove commits and ${cachefile} on git push -f, this recreated later on. 58 if test "${force}" = "1"; then 59 rm -f "${cachefile}" 60 rm -rf "commit" 61 fi 62 63 # make index. 64 stagit-index "${reposdir}/"*/ > "${destdir}/index.html" 65 66 # make pages. 67 stagit -c "${cachefile}" -u "https://git.in0rdr.ch/$d/" "${reposdir}/${r}" 68 69 ln -sf log.html index.html 70 ln -sf ../style.css style.css 71 ln -sf ../logo.png logo.png 72 73 # Create .tar.gz archives by tag 74 echo "Archiving tags.." 75 archivedir="${destdir}${d}/archive" 76 mkdir -p "${archivedir}" 77 cd "${reposdir}/${r}" 78 git tag -l | while read -r t; do 79 f="${archivedir}/${d}-$(echo "${t}" | tr '/' '_').tar.gz" 80 test -f "${f}" && continue 81 git archive \ 82 --format tar.gz \ 83 --prefix "${d}-${t}/" \ 84 -o "${f}" \ 85 -- \ 86 "${t}" 87 done 88 89 # Create .tar.gz archive for master 90 f="${archivedir}/${d}-master.tar.gz" 91 git archive \ 92 --format tar.gz \ 93 --prefix "${d}-master/" \ 94 -o "${f}" \ 95 -- \ 96 "master" 97 98 # trigger jenkins, create a token first 99 # https://plugins.jenkins.io/git/#plugin-content-push-notification-from-repository 100 TOKEN="" 101 curl --max-time 5 -s \ 102 "https://jenkins.in0rdr.ch/git/notifyCommit?token=$TOKEN&url=https://git.in0rdr.ch/$r" 103 104 echo "done"