Combining build.sh with push.sh

This commit is contained in:
Joe Ferguson 2014-12-05 16:25:56 -08:00
parent af7db8287f
commit 0c8a366888
1 changed files with 134 additions and 95 deletions

View File

@ -16,42 +16,63 @@ library="$(readlink -f "$library")"
src="$(readlink -f "$src")" src="$(readlink -f "$src")"
logs="$(readlink -f "$logs")" logs="$(readlink -f "$logs")"
# arg handling: all args are [repo|repo:tag]
usage() { usage() {
cat <<EOUSAGE cat <<EOUSAGE
usage: $0 [options] [repo[:tag] ...] usage: $0 [build|push] [options] [repo[:tag] ...]
ie: $0 --all ie: $0 build --all
$0 debian ubuntu:12.04 $0 push debian ubuntu:12.04
This script builds the Docker images specified using the Git repositories This script builds or pushes the Docker images specified using the Git
specified in the library files. repositories specified in the library files in the namespaces.
options: common options:
--help, -h, -? Print this help message --help, -h, -? Print this help message
--all Builds all Docker repos specified in library --all Builds all Docker repos specified in library
--no-clone Don't pull the Git repos
--no-build Don't build, just echo what would have built
--library="$library" --library="$library"
Where to find repository manifest files Where to find repository manifest files
--src="$src"
Where to store the cloned Git repositories
--logs="$logs"
Where to store the build logs
--namespaces="$namespaces" --namespaces="$namespaces"
Space separated list of namespaces to tag images in after Space separated list of namespaces to tag images in after
building building
--docker="$docker" --docker="$docker"
Use a custom Docker binary. Use a custom Docker binary.
build options:
--no-clone Don't pull the Git repos
--no-build Don't build, just echo what would have built
--src="$src"
Where to store the cloned Git repositories
--logs="$logs"
Where to store the build logs
push options:
--no-push Don't actually push the images to the Docker Hub
EOUSAGE EOUSAGE
} }
opts="$(getopt -o 'h?' --long 'help,all,no-clone,no-build,library:,src:,logs:,namespaces:,docker:' -- "$@" || { usage >&2 && false; })" # which subcommand
subcommand=$1
case "$subcommand" in
build|push)
shift
;;
*)
{
echo "error: unknown subcommand: $1"
usage
} >&2
exit 1
;;
esac
# arg handling
opts="$(getopt -o 'h?' --long 'help,all,no-clone,no-build,no-push,library:,src:,logs:,namespaces:,docker:' -- "$@" || { usage >&2 && false; })"
eval set -- "$opts" eval set -- "$opts"
doClone=1 doClone=1
doBuild=1 doBuild=1
doPush=1
buildAll= buildAll=
while true; do while true; do
flag=$1 flag=$1
@ -64,6 +85,7 @@ while true; do
--all) buildAll=1 ;; --all) buildAll=1 ;;
--no-clone) doClone= ;; --no-clone) doClone= ;;
--no-build) doBuild= ;; --no-build) doBuild= ;;
--no-push) doPush= ;;
--library) library="$1" && shift ;; --library) library="$1" && shift ;;
--src) src="$1" && shift ;; --src) src="$1" && shift ;;
--logs) logs="$1" && shift ;; --logs) logs="$1" && shift ;;
@ -102,7 +124,7 @@ declare -A repoGitRepo=()
declare -A repoGitRef=() declare -A repoGitRef=()
declare -A repoGitDir=() declare -A repoGitDir=()
logDir="$logs/build-$(date +'%Y-%m-%d--%H-%M-%S')" logDir="$logs/$subcommand-$(date +'%Y-%m-%d--%H-%M-%S')"
mkdir -p "$logDir" mkdir -p "$logDir"
latestLogDir="$logs/latest" # this gets shiny symlinks to the latest buildlog for each repo we've seen since the creation of the logs dir latestLogDir="$logs/latest" # this gets shiny symlinks to the latest buildlog for each repo we've seen since the creation of the logs dir
@ -174,6 +196,7 @@ for repoTag in "${repos[@]}"; do
gitRepo="${gitRepo%/}" gitRepo="${gitRepo%/}"
gitRepo="$src/$gitRepo" gitRepo="$src/$gitRepo"
if [ "$subcommand" == 'build' ]; then
if [ -z "$doClone" ]; then if [ -z "$doClone" ]; then
if [ "$doBuild" -a ! -d "$gitRepo" ]; then if [ "$doBuild" -a ! -d "$gitRepo" ]; then
echo >&2 "error: directory not found: $gitRepo" echo >&2 "error: directory not found: $gitRepo"
@ -202,6 +225,7 @@ for repoTag in "${repos[@]}"; do
# disable any automatic garbage collection too, just to help make sure we keep our dangling commit objects # disable any automatic garbage collection too, just to help make sure we keep our dangling commit objects
( cd "$gitRepo" && git config gc.auto 0 ) ( cd "$gitRepo" && git config gc.auto 0 )
fi fi
fi
repoGitRepo[$repo:$tag]="$gitRepo" repoGitRepo[$repo:$tag]="$gitRepo"
repoGitRef[$repo:$tag]="$gitRef" repoGitRef[$repo:$tag]="$gitRef"
@ -232,10 +256,12 @@ while [ "$#" -gt 0 ]; do
echo "Processing $repoTag ..." echo "Processing $repoTag ..."
thisLog="$logDir/build-$repoTag.log" thisLog="$logDir/$subcommand-$repoTag.log"
touch "$thisLog" touch "$thisLog"
ln -sf "$thisLog" "$latestLogDir/$(basename "$thisLog")" ln -sf "$thisLog" "$latestLogDir/$(basename "$thisLog")"
case "$subcommand" in
build)
if ! ( cd "$gitRepo" && git rev-parse --verify "${gitRef}^{commit}" &> /dev/null ); then if ! ( cd "$gitRepo" && git rev-parse --verify "${gitRef}^{commit}" &> /dev/null ); then
echo "- failed; invalid ref: $gitRef" echo "- failed; invalid ref: $gitRef"
didFail=1 didFail=1
@ -291,6 +317,19 @@ while [ "$#" -gt 0 ]; do
( set -x; "$docker" tag "$repoTag" "$namespace/$repoTag" ) &>> "$thisLog" ( set -x; "$docker" tag "$repoTag" "$namespace/$repoTag" ) &>> "$thisLog"
done done
fi fi
;;
push)
for namespace in $namespaces; do
if [ "$doPush" ]; then
if ! "$docker" push "$namespace/$repoTag"; then
echo >&2 "- $namespace/$repoTag failed to push!"
fi
else
echo "$docker push" "$namespace/$repoTag"
fi
done
;;
esac
done done
[ -z "$didFail" ] [ -z "$didFail" ]