Skip to content

Commit

Permalink
Added options to not tag docker with branch name or version.
Browse files Browse the repository at this point in the history
  • Loading branch information
flounderpinto committed Dec 14, 2023
1 parent 4da8c8e commit 7476c27
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 36 deletions.
52 changes: 38 additions & 14 deletions src/dockerBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CODE_DIR="${CODE_DIR:-/opt/code}"
function dockerBuildUsage
{
echo $'Usage:'
echo $'\tdockerBuild.sh dockerBuild -e dockerRegistryName -r dockerRepoName -c buildContextDir -f dockerFile [-g gitRepoDir] [-p platform]... [-t tag]... [-b buildArg]... [-a arg]... [-h]'
echo $'\tdockerBuild.sh dockerBuild -e dockerRegistryName -r dockerRepoName -c buildContextDir -f dockerFile [-g gitRepoDir] [-p platform]... [-t tag]... [-b buildArg]... [-a arg]... [-n] [-o] [-h]'
echo $'\t\t-e - Docker registry name.'
echo $'\t\t\te.g. "index.docker.io/my-registry"'
echo $'\t\t-r - Docker repo name.'
Expand Down Expand Up @@ -83,12 +83,14 @@ function dockerBuild
local BUILD_CONTEXT_DIR=""
local DOCKER_FILE=""
local GIT_DIR=""
local NO_GIT_VERSION=""
local NO_GIT_BRANCH=""
local PLATFORM=()
local TAGS=()
local BUILD_ARGS=()
local ADDITIONAL_BUILD_FLAGS=()

while getopts ":e:r:c:f:g:p:t:b:a:h" opt; do
while getopts ":e:r:c:f:g:p:t:b:a:noh" opt; do
case $opt in
e)
DOCKER_REGISTRY="$OPTARG"
Expand Down Expand Up @@ -117,6 +119,12 @@ function dockerBuild
a)
ADDITIONAL_BUILD_FLAGS+=("$OPTARG")
;;
n)
NO_GIT_VERSION="true"
;;
o)
NO_GIT_BRANCH="true"
;;
h)
dockerBuildUsage
exit 1
Expand Down Expand Up @@ -165,8 +173,9 @@ function dockerBuild
#The location of the .git directory is required.
GIT_DIR="$GIT_DIR/.git"

#Default is to just tag with the git version.
if [ ${#TAGS[@]} -eq 0 ]; then
#Unless the NO_GIT_VERSION flag is set, get the git repo version
# and add to the tags list.
if [ -z "$NO_GIT_VERSION" ]; then
local gitVersion=""
gitVersion=$(getGitVersion)
if [ -z "$gitVersion" ]; then
Expand All @@ -177,15 +186,18 @@ function dockerBuild
fi

#Find the current git branch.
local gitBranch=""
gitBranch=$(getGitBranch)
if [ -z "$gitBranch" ]; then
echo "Could not determine git branch name, caching to main."
gitBranch="$MAIN_BRANCH"
else
#Tag with the branch name, so we have a tag that tracks the latest version of a branch,
# and also so that we have a location to push the build cache.
TAGS+=("$gitBranch")
# Default to the main branch for caching.
local gitBranch="$MAIN_BRANCH"
if [ -z "$NO_GIT_BRANCH" ]; then
gitBranch=$(getGitBranch)
if [ -z "$gitBranch" ]; then
echo "Could not determine git branch name."
exit 1
else
#Tag with the branch name, so we have a tag that tracks the latest version of a branch,
# and also so that we have a location to push the build cache.
TAGS+=("$gitBranch")
fi
fi

echo "REGISTRY:$DOCKER_REGISTRY"
Expand Down Expand Up @@ -242,10 +254,22 @@ function dockerBuild

#When calling this script through docker, many arguments are
# always the same. This is a shortcut for calling dockerBuild()
function dockerBuildStandard
function dockerBuildStandardBranch
{
dockerBuild -c "$CODE_DIR" -f "$CODE_DIR/docker/Dockerfile" -g "$CODE_DIR" "$@"
}
function dockerBuildStandardMain
{
dockerBuild -c "$CODE_DIR" -f "$CODE_DIR/docker/Dockerfile" -g "$CODE_DIR" -t "latest" "$@"
}
function dockerBuildStandardTag
{
local TAG="$1"
shift
echo "${@}"
#TODO - -n
dockerBuild -c "$CODE_DIR" -f "$CODE_DIR/docker/Dockerfile" -g "$CODE_DIR" -n -o -t "$TAG" "${@}"
}

#Allows function calls based on arguments passed to the script
"$@"
3 changes: 3 additions & 0 deletions test/unit/expected/buildStandardBranch
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
createBuilder
docker buildx build -o type=registry -t docker-registry/docker-repo:mocked-git-version -t docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch -f /opt/code/docker/Dockerfile /opt/code 2>&1
removeBuilder
3 changes: 3 additions & 0 deletions test/unit/expected/buildStandardMain
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
createBuilder
docker buildx build -o type=registry -t docker-registry/docker-repo:latest -t docker-registry/docker-repo:mocked-git-version -t docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch -f /opt/code/docker/Dockerfile /opt/code 2>&1
removeBuilder
3 changes: 3 additions & 0 deletions test/unit/expected/buildStandardTag
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
createBuilder
docker buildx build -o type=registry -t docker-registry/docker-repo:tag1 --cache-from=type=registry,ref=docker-registry/docker-repo:main --cache-from=type=registry,ref=docker-registry/docker-repo:main -f /opt/code/docker/Dockerfile /opt/code 2>&1
removeBuilder
2 changes: 1 addition & 1 deletion test/unit/expected/multipleArgs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
createBuilder
docker buildx build -o type=registry --build-arg build-arg1 --build-arg build-arg2 --platform=platform1,platform2 --arg1 on --arg2 off -t docker-registry/docker-repo:tag1 -t docker-registry/docker-repo:tag2 -t docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch -f dockerfile build-context-dir 2>&1
docker buildx build -o type=registry --build-arg build-arg1 --build-arg build-arg2 --platform=platform1,platform2 --arg1 on --arg2 off -t docker-registry/docker-repo:tag1 -t docker-registry/docker-repo:tag2 -t docker-registry/docker-repo:mocked-git-version -t docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch -f dockerfile build-context-dir 2>&1
removeBuilder
2 changes: 1 addition & 1 deletion test/unit/expected/singleArgs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
createBuilder
docker buildx build -o type=registry --build-arg build-arg --platform=target --arg on -t docker-registry/docker-repo:tag -t docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch -f dockerfile build-context-dir 2>&1
docker buildx build -o type=registry --build-arg build-arg --platform=target --arg on -t docker-registry/docker-repo:tag -t docker-registry/docker-repo:mocked-git-version -t docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch --cache-from=type=registry,ref=docker-registry/docker-repo:mocked-git-branch -f dockerfile build-context-dir 2>&1
removeBuilder
59 changes: 39 additions & 20 deletions test/unit/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,54 @@ function testMultipleArgsCommaSeparated()
echo ""
}

function testdockerBuildStandard()
function testdockerBuildStandardBranch()
{
local E="docker-registry"
local R="docker-repo"
local P1="platform1"
local P2="platform2"
local T1="tag1"
local T2="tag2"
local B1="build-arg1"
local B2="build-arg2"
local A1="--arg1 on"
local A2="--arg2 off"

rm -f "$OUTPUT_FILE"

stdout=$(dockerBuildStandard \
stdout=$(dockerBuildStandardBranch \
-e "$E" \
-r "$R" \
-p "$P1" \
-p "$P2" \
-t "$T1" \
-t "$T2" \
-b "$B1" \
-b "$B2" \
-a "$A1" \
-a "$A2")
-r "$R" )
echo "$stdout"
buildCmd=$(<"$OUTPUT_FILE")
expected=$(<"$SCRIPT_DIR/expected/buildStandardBranch")
assertEquals "$expected" "$buildCmd"
echo ""
}

function testdockerBuildStandardMain()
{
local E="docker-registry"
local R="docker-repo"

rm -f "$OUTPUT_FILE"

stdout=$(dockerBuildStandardMain \
-e "$E" \
-r "$R" )
echo "$stdout"
buildCmd=$(<"$OUTPUT_FILE")
expected=$(<"$SCRIPT_DIR/expected/buildStandardMain")
assertEquals "$expected" "$buildCmd"
echo ""
}

function testdockerBuildStandardTag()
{
local E="docker-registry"
local R="docker-repo"

rm -f "$OUTPUT_FILE"

stdout=$(dockerBuildStandardTag \
"tag1" \
-e "$E" \
-r "$R" )
echo "$stdout"
buildCmd=$(<"$OUTPUT_FILE")
expected=$(<"$SCRIPT_DIR/expected/buildStandard")
expected=$(<"$SCRIPT_DIR/expected/buildStandardTag")
assertEquals "$expected" "$buildCmd"
echo ""
}
Expand Down

0 comments on commit 7476c27

Please sign in to comment.