Skip to content

Commit

Permalink
feat: add buildkit support (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRibeiro authored May 3, 2022
1 parent 838e3c6 commit 87c3dfc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ promotion_requires: &promotion_requires
publish-machine,
publish-docker-cache,
publish-docker-cache-not-found,
publish-docker-with-buildkit,
test-pull,
test-install-docker-tools-docker-latest,
test-install-docker-tools-docker-old,
Expand Down Expand Up @@ -303,6 +304,20 @@ workflows:
docker-password: DOCKER_PASS
use-docker-credentials-store: true
filters: *filters
- docker/publish:
name: publish-docker-with-buildkit
executor: docker-latest
context: CPE-orb-docker-testing
use-remote-docker: true
remote-docker-version: "20.10.12"
use-buildkit: true
dockerfile: test.Dockerfile
image: cpeorbtesting/docker-orb-test
tag: $CIRCLE_SHA1
docker-username: DOCKER_USER
docker-password: DOCKER_PASS
use-docker-credentials-store: true
filters: *filters
- docker/publish:
name: publish-docker-cache
requires:
Expand Down
14 changes: 14 additions & 0 deletions src/commands/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ parameters:
Pass through a default timeout if your Docker build does not output
anything for more than 10 minutes.
use-buildkit:
type: boolean
default: false
description: >
Use buildkit to build the image.
Available on Docker >= 18.09.0
https://docs.docker.com/develop/develop-images/build_enhancements/
steps:
- when:
condition: <<parameters.lint-dockerfile>>
Expand All @@ -99,6 +107,11 @@ steps:
dockerfile: <<parameters.path>>/<<parameters.dockerfile>>
debug: <<parameters.debug>>

- when:
condition: <<parameters.use-buildkit>>
steps:
- run: echo 'export DOCKER_BUILDKIT=1' >> $BASH_ENV

- when:
condition: <<parameters.attach-at>>
steps:
Expand All @@ -117,4 +130,5 @@ steps:
PARAM_IMAGE: <<parameters.image>>
PARAM_REGISTRY: <<parameters.registry>>
PARAM_TAG: <<parameters.tag>>
PARAM_USE_BUILDKIT: <<parameters.use-buildkit>>
command: <<include(scripts/build.sh)>>
8 changes: 8 additions & 0 deletions src/examples/build-and-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ usage:
- docker/publish:
image: $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
update-description: true
build-docker-image-only-with-buildkit:
jobs:
- docker/publish:
image: $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
update-description: true
use-remote-docker: true
use-buildkit: true
remote-docker-version: "20.10.12"
18 changes: 18 additions & 0 deletions src/jobs/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ parameters:
Setup a remote Docker engine for Docker commands? Only required
if using a Docker-based executor
remote-docker-version:
type: string
default: "17.09.0-ce"
description: >
Pick remote Docker engine version.
Available versions can be found at: https://circleci.com/docs/2.0/building-docker-images/#docker-version.
Must be >= 18.09 for BuildKit support.
remote-docker-dlc:
type: boolean
default: false
Expand Down Expand Up @@ -143,6 +151,14 @@ parameters:
Path to the directory containing your build context,
defaults to . (working directory)
use-buildkit:
type: boolean
default: false
description: >
Use buildkit to build the image.
Available on Docker >= 18.09.0
https://docs.docker.com/develop/develop-images/build_enhancements/
steps:
- checkout

Expand All @@ -154,6 +170,7 @@ steps:
condition: <<parameters.use-remote-docker>>
steps:
- setup_remote_docker:
version: <<parameters.remote-docker-version>>
docker_layer_caching: <<parameters.remote-docker-dlc>>

- when:
Expand Down Expand Up @@ -186,6 +203,7 @@ steps:
extra_build_args: <<parameters.extra_build_args>>
lint-dockerfile: <<parameters.lint-dockerfile>>
treat-warnings-as-errors: <<parameters.treat-warnings-as-errors>>
use-buildkit: <<parameters.use-buildkit>>

- when:
condition: <<parameters.after_build>>
Expand Down
30 changes: 23 additions & 7 deletions src/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,26 @@ if [ -n "$PARAM_CACHE_FROM" ]; then
fi
fi

# http://mywiki.wooledge.org/BashFAQ/050#I_only_want_to_pass_options_if_the_runtime_data_needs_them
docker build \
${PARAM_EXTRA_BUILD_ARGS:+"$PARAM_EXTRA_BUILD_ARGS"} \
${PARAM_CACHE_FROM:+--cache-from="$PARAM_CACHE_FROM"} \
"--file=$PARAM_DOCKERFILE_PATH/$PARAM_DOCKERFILE_NAME" \
"$DOCKER_TAGS_ARG" \
"$PARAM_DOCKER_CONTEXT"
build_args=(
"--file=$PARAM_DOCKERFILE_PATH/$PARAM_DOCKERFILE_NAME"
"$DOCKER_TAGS_ARG"
)

if [ -n "$PARAM_EXTRA_BUILD_ARGS" ]; then
build_args+=("$PARAM_EXTRA_BUILD_ARGS")
fi

if [ -n "$PARAM_CACHE_FROM" ]; then
build_args+=("--cache-from=$PARAM_CACHE_FROM")
fi

if [ "$PARAM_USE_BUILDKIT" -eq 1 ]; then
build_args+=("--progress=plain")
fi

# The context must be the last argument.
build_args+=("$PARAM_DOCKER_CONTEXT")

set -x
docker build "${build_args[@]}"
set +x

0 comments on commit 87c3dfc

Please sign in to comment.