Skip to content

Instantly share code, notes, and snippets.

@hedgerh
Last active April 1, 2017 22:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedgerh/236567f0632b54e70947 to your computer and use it in GitHub Desktop.
Save hedgerh/236567f0632b54e70947 to your computer and use it in GitHub Desktop.
Build Gitbooks for each Git tag of a repository.

Setup:

Set permissions to allow script to run

chmod a+x ./gitbook-tag-build.sh

Usage:

Choose an output folder, and an input file containing a list of Git tags.

The default ouput folder is _allBooks and the default input is .gitbook-tags

# default usage
./gitbook-tag-build.sh <outFolder> <inputFile>

# or use `git tag` as input
git tag | ./gitbook-tag-build.sh

Example Input:

v1.0.0
v1.0.0-alpha

Example Output:

_allBooks/_book@v1.0.0
_allBooks/_book@v1.0.0-alpha
#!/bin/bash
# SCRIPT: gitbook-tag-build.sh
# PURPOSE: Parse list of Git tags and build gitbooks for each tag.
# AUTHOR: Harry Hedger, github.com/hedgerh, twitter.com/harry_hedger
# USAGE: ./gitbook-tag-build.sh <outputDir | default: _allBooks> <input | default: .gitbook-tags >
# Color Codes
RED='\033[0;31m'
NC='\033[0m' # No Color
ORIGIN_BRANCH=$(git rev-parse --abbrev-ref HEAD)
INPUT_FILE=${2-$(echo .gitbook-tags)}
OUTPUT_FOLDER=${1-$(echo _allBooks)}
rm -rf $OUTPUT_FOLDER
mkdir -p $OUTPUT_FOLDER
if [ -d $OUTPUT_FOLDER ] && [ -s $INPUT_FILE ]; then
cat $INPUT_FILE | while read TAG
do
printf "Building Gitbook: ${TAG}\n"
git checkout $TAG --quiet
ERROR_LOG=$(gitbook build)
if [ $? = 0 ] && [ -d _book ]; then
printf "... Done. Moving to ${OUTPUT_FOLDER}/_book@${TAG}.\n"
rm -rf $OUTPUT_FOLDER/_book@$TAG
mv _book _book@$TAG
mv _book@$TAG $OUTPUT_FOLDER
else
printf "... ${RED}ERROR! ${NC}Could not build Gitbook ${TAG}${NC}\n"
sleep 1
printf "${RED}LOG:${NC}\n${ERROR_LOG}\n\n"
sleep 0.5
fi
done
else
if ! [ -s $INPUT_FILE ]; then
printf "${RED}Error: ${NC}Missing input file or input file is empty.${NC}\n"
exit 1
fi
if ! [ -s $OUTPUT_FOLDER ]; then
printf "${RED}Error: ${NC}Output folder could not be created.${NC}\n"
exit 2
fi
fi
git checkout $ORIGIN_BRANCH --quiet
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment