Skip to content

Instantly share code, notes, and snippets.

@blast-hardcheese
Last active April 19, 2017 19:44
Show Gist options
  • Save blast-hardcheese/1e83c28a45155ce9f6369444f81d587d to your computer and use it in GitHub Desktop.
Save blast-hardcheese/1e83c28a45155ce9f6369444f81d587d to your computer and use it in GitHub Desktop.
publish:
./scripts/publish-to-gollum --prepare-docs-target --overwrite
sbt -no-colors tut
./scripts/publish-to-gollum --publish-docs --overwrite
#!/bin/bash
# Exit on first failure
set -ex
# Print help, then exit 1
help() {
cat >&2 <<!
$0: Manage publishing documentation
-t, --prepare-docs-target: Clone this repo's wiki to docs/
-p, --publish-docs: Publish whatever is in docs/
-o, --overwrite: Overwrite external changes
-h, --help: This message
!
exit 1
}
# Parse script options
# Usage: parseOpts "$@"
parseOpts() {
OVERWRITE=0
VERBOSE=0
PREPARE_DOCS_TARGET=0
PUBLISH_DOCS=0
while getopts ":htpov-:" opt; do
case $opt in
h)
help
;;
t)
PREPARE_DOCS_TARGET=1
;;
p)
PUBLISH_DOCS=1
;;
o)
OVERWRITE=1
;;
v)
VERBOSE=1
;;
-)
case "$OPTARG" in
help)
help
;;
overwrite)
OVERWRITE=1
;;
prepare-docs-target)
PREPARE_DOCS_TARGET=1
;;
publish-docs)
PUBLISH_DOCS=1
;;
*)
echo "Invalid long arg: --$OPTARG" >&2
help
esac
;;
*)
echo "Invalid option: -$OPTARG" >&2
help
;;
esac
done
}
hashDocs() {
find docs \( ! -name .hashes \) -a \( ! -path '*/.git/*' \) -a -type f -exec md5sum {} + | sort -n
}
slackNotify() {
msg="$1"
curl -X POST --data-urlencode "payload={\"channel\": \"#...\", \"username\": \"Jenkins\", \"text\": \"${msg}\"}" \
https://hooks.slack.com/...
}
# verifyHashes: Ensure we don't overwrite documentation changed by others
verifyHashes() {
if ! diff <(hashDocs) <(sort -n < docs/.hashes) >/dev/null; then
echo "Docs have been changed externally!" >&2
if [ "$OVERWRITE" -eq 1 ]; then
echo " ... overwriting!" >&2
contributors="$(cd docs; git log --format="%aN (%h)" $(git log --author='jenkins <>' --oneline | head -n1 | cut -f 1 -d ' ')..@ | awk -v ORS=", " '{ print $0 }' | sed 's/, $//')"
slackNotify "scala-service $RELEASE_VERSION is overwriting wiki changes from \"$contributors\". Please open a PR against https://your.ghe.instance/org/repo/tree/master/docs-src to update the wiki."
else
exit 1
fi
fi
}
cleanDocsTarget() {
rm -rf docs
}
prepareDocsTarget() {
cleanDocsTarget
remote=origin
tracking_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || true)
if [ ! -z "$tracking_branch" ]; then
remote="${tracking_branch%/*}"
fi
push_url=$(git remote get-url --push "$remote")
wiki_url="${push_url%.git}.wiki.git"
git clone -o origin "$wiki_url" docs
verifyHashes
# Clean out docs directory in preparation for accepting new docs
pushd docs
git rm -f -r .
popd
}
# publishDocs: Publish docs to wherever we should publish
publishDocs() {
hashDocs > docs/.hashes
pushd docs
git add .
git commit -m 'Updating documentation' || true
git push origin master:master
popd
}
# main: Actually do work
# Usage: main "$@", to grab args from the script
main() {
parseOpts "$@"
if [ -z "$RELEASE_VERSION" ]; then
echo "Not building a release." >&2
exit 0
elif [[ "$RELEASE_VERSION" = *SNAPSHOT ]]; then
echo "Snapshot build detected." >&2
exit 0
fi
if [ "$PREPARE_DOCS_TARGET" -eq 1 ] && [ "$PUBLISH_DOCS" -eq 1 ]; then
echo "It is an error to use --prepare-docs-target and --publish-docs at the same time" >&2
exit 1
elif [ "$PREPARE_DOCS_TARGET" -eq 1 ]; then
prepareDocsTarget
elif [ "$PUBLISH_DOCS" -eq 1 ]; then
publishDocs
fi
}
main "$@"
@blast-hardcheese
Copy link
Author

This assumes tut is configured to publish to docs/ and that Jenkins has access to a privkey with required auth. An implementation detail is that a hash of all files is kept, which is an indicator of external modifications. If you are in a more controlled environment you can remove a lot of this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment