publish_www (2051B)
1 #!/bin/sh -e 2 # 3 # post-receive.d hook: publish the built site to the web root. 4 # 5 # Install: 6 # /home/git/git_infra/post-receive.d/publish_www (this file, chmod +x) 7 # /home/git/bracken_jp.git/hooks/post-receive.d/publish_www -> symlink to it 8 # 9 # Note this is symlinked ONLY into bracken_jp.git, unlike gout_html which is 10 # linked into every repo. 11 12 BRANCH="master" 13 WWW="/var/www/chris.bracken.jp" 14 15 # Files must be readable by the www user in the www jail, which reads this 16 # same dataset over a read-only nullfs mount. 17 umask 022 18 19 # Hooks are called from the repo directory. 20 repo_dir=$(pwd) 21 22 # Refuse to run if the web root is missing or not writable. Without this a 23 # missing nullfs mount would publish into the jail's own filesystem, which 24 # would look like it worked and serve nothing. 25 if [ ! -d "$WWW" ]; then 26 echo "[publish] ERROR: $WWW does not exist - is the nullfs mount attached?" >&2 27 exit 1 28 fi 29 if [ ! -w "$WWW" ]; then 30 echo "[publish] ERROR: $WWW is not writable" >&2 31 exit 1 32 fi 33 34 # stdin gives one line per updated ref: <old> <new> <refname> 35 pushed=0 36 while read -r _old new ref; do 37 [ "$ref" = "refs/heads/${BRANCH}" ] || continue 38 # Branch deletion - do not wipe the live site. 39 [ "$new" = "0000000000000000000000000000000000000000" ] && continue 40 pushed=1 41 done 42 43 if [ "$pushed" = "0" ]; then 44 echo "[publish] ${BRANCH} not updated; nothing to publish" 45 exit 0 46 fi 47 48 echo "[publish] Publishing ${BRANCH} to ${WWW}" 49 50 # checkout -f writes/overwrites every tracked file into the work tree. 51 git --git-dir="$repo_dir" --work-tree="$WWW" checkout -f "$BRANCH" 52 53 # checkout -f does not remove files deleted from the branch, so clean up 54 # anything untracked. This is what made `reset --hard` sufficient before. 55 # 56 # NOTE: this deletes ANY untracked file under $WWW. That is correct while the 57 # whole tree is repo-managed. If you ever need to keep something unmanaged 58 # there, add it to .gitignore AND drop -x, or this will remove it. 59 git --git-dir="$repo_dir" --work-tree="$WWW" clean -fd 60 61 echo "[publish] Done"