Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the option of passing custom --build-arg(s)… #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ export HAS_DB_IMAGE="${BUILDKITE_PLUGIN_SBC_SHARED_DB_IMAGE:-false}"
ACTION="$BUILDKITE_PLUGIN_SBC_SHARED_ACTION"
if [[ $ACTION == "publish_gem" ]];then
cp "$(dirname $BASH_SOURCE)/../lib/release_jfrog.sh" .buildkite/release.sh
fi
fi
64 changes: 48 additions & 16 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ validate_switches() {
echo "Validating switches"

arr=("$@")

set +u
for item in "${arr[@]}"
do
Expand All @@ -64,13 +64,27 @@ validate_switches() {
set -u
}

# builds a string version ($build_args) of a list of passed --build_args
compile_build_args() {
build_args=''

passed_params=("$@")
for i in "${!passed_params[@]}"; do
if [[ ${passed_params[i]} == '--build_arg' ]]; then
build_args="$build_args --build_arg ${passed_params[i+1]}"
fi
done
}

# target => (Optional) set the target build stage to build
# tag => tag for the docker image
# file => source docker file to build from
# cache_id => cache identifier from where it was built from. Typically GH branch name
buildx() {
compile_build_args "$@"
target=
switches "$@"

validate_switches tag file cache_id
varx REPO

Expand All @@ -81,18 +95,23 @@ buildx() {
OPTIONAL_TARGET="--target $target"
fi

docker buildx build \
-f $file \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg CI_BRANCH \
--build-arg CI_STRING_TIME \
--cache-from $BK_ECR:$APP-$tag-cache-$cache_id \
--cache-from $BK_ECR:$APP-$tag-cache-master \
--secret id=railslts,env=BUNDLE_GEMS__RAILSLTS__COM \
--secret id=jfrog,env=BUNDLE_SAGEONEGEMS__JFROG__IO \
--ssh default $OPTIONAL_TARGET \
-t $REPO:$tag \
.
args=(
"-f $file"
"--build-arg BUILDKIT_INLINE_CACHE=1"
"--build-arg CI_BRANCH"
"--build-arg CI_STRING_TIME"
"--cache-from $BK_ECR:$APP-$tag-cache-$cache_id"
"--cache-from $BK_ECR:$APP-$tag-cache-master"
"--secret id=railslts,env=BUNDLE_GEMS__RAILSLTS__COM"
"--secret id=jfrog,env=BUNDLE_SAGEONEGEMS__JFROG__IO"
"--ssh default $OPTIONAL_TARGET"
"-t $REPO:$tag"
"${build_args/"--build_arg"/"--build-arg"}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this replacement still work if there are multiple build arg? This test suggests not..

$ var='foo bar wibble bar'
$ echo ${var/bar/meep}
foo meep wibble bar

)

concatted_args=$(printf " %s" "${args[@]}")
command="docker buildx build ${concatted_args:1} ."
eval $command
}

# app => name of the application
Expand All @@ -101,6 +120,7 @@ buildx() {
# file => source docker file to build from
# cache_id => cache identifier from where it was built from. Typically GH branch name
buildx_and_cachex () {
compile_build_args "$@"
target=
switches "$@"
validate_switches app tag cache_id file
Expand All @@ -111,8 +131,20 @@ buildx_and_cachex () {
OPTIONAL_TARGET="--target $target"
fi

buildx --app $app $OPTIONAL_TARGET --tag $tag --file $file --cache_id $cache_id

args=(
"--app $app"
"$OPTIONAL_TARGET"
"--tag $tag"
"--file $file"
"--cache_id $cache_id"
$build_args
)

concatted_args=$(printf " %s" "${args[@]}")
command="buildx ${concatted_args:1} ."

eval $command

cachex --app $app --tag $tag --cache_id $cache_id
}

Expand Down Expand Up @@ -160,4 +192,4 @@ push_image () {
docker pull $SOURCE_IMAGE
docker tag $SOURCE_IMAGE $TARGET_IMAGE
docker push $TARGET_IMAGE
}
}