Skip to content

Instantly share code, notes, and snippets.

@jdarling
Created March 12, 2020 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdarling/0b7588d3a279bb524793d6e51d1beeda to your computer and use it in GitHub Desktop.
Save jdarling/0b7588d3a279bb524793d6e51d1beeda to your computer and use it in GitHub Desktop.
Generic build.sh script for Dockerfiles that loads and saves versioning in version.txt file in same folder as docker file.
#!/usr/bin/env bash
# Works for any Dockerfile, hardcoded service name and docker org these could be pulled from config or environment variables
SERVICE_NAME=SET_SERVICE_NAME
DOCKER_ORG=SET_DOCKER_ORG
PUBLISH=false
VERSION=false
MAJOR=false
MINOR=false
PATCH=false
UPREV=false
Showhelp () {
scriptname=`basename "$0"`
echo "
${scriptname} <options>
Options:
-M, --major - Increment the major version number
-m, --minor - Increment the minor version number
-p, --patch - Increment the patch version number
-v <version>, --version <version> - Set the version number
-P, --publish - Publish the image to docker hub
-h or --help - Show this screen
"
exit 0
}
GetVersion () {
if [ -f "version.txt" ]; then
head -n 1 "version.txt"
else
echo "0.0.1"
fi
}
export SKIP_PREFLIGHT_CHECK=true
while [[ $# > 0 ]]
do
key="$1"
case $key in
-M|--major)
MAJOR=true
UPREV=true
;;
-m|--minor)
MINOR=true
UPREV=true
;;
-p|--patch)
PATCH=true
UPREV=true
;;
-v|--version)
VERSION="$2"
shift
;;
-P|--publish)
PUBLISH=true
;;
-h|--help)
Showhelp
;;
*)
# unknown option
;; esac
shift # past argument or value
done
# Get the current script directory
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
cd $DIR
if [[ $UPREV != true ]]; then
PATCH=true
fi
if [[ $UPREV == true ]]; then
if [[ $VERSION == false ]]; then
VERSION=$(GetVersion)
fi
a=( ${VERSION//./ } )
if [[ $MAJOR == true ]]; then
((a[0]++))
a[1]=0
a[2]=0
fi
if [[ $MINOR == true ]]; then
((a[1]++))
a[2]=0
fi
if [[ $PATCH == true ]]; then
((a[2]++))
fi
VERSION="${a[0]}.${a[1]}.${a[2]}"
echo "*****Calculated next build number: $VERSION*****"
fi
if [[ $VERSION == false ]]; then
VERSION=$(GetVersion)
else
echo "$VERSION">version.txt
fi
SERVICE_VERSION="v${VERSION}"
EXITED=`docker ps -aq --filter status=exited`
if [[ "$EXITED" != "" ]]; then
docker rm $(docker ps -aq --filter status=exited)
fi
docker image rm $SERVICE_NAME:latest
docker image rm $DOCKER_ORG/$SERVICE_NAME:latest
docker image rm $DOCKER_ORG/$SERVICE_NAME:${SERVICE_VERSION}
set -ex
docker build --rm --tag $SERVICE_NAME:latest .
docker tag $SERVICE_NAME:latest $DOCKER_ORG/$SERVICE_NAME:latest
docker tag $SERVICE_NAME:latest $DOCKER_ORG/$SERVICE_NAME:${SERVICE_VERSION}
if [[ $PUBLISH == true ]]; then
docker push $DOCKER_ORG/$SERVICE_NAME:latest
docker push $DOCKER_ORG/$SERVICE_NAME:${SERVICE_VERSION}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment