Skip to content

Commit

Permalink
Add cache_to option in the build and publish commands (#195)
Browse files Browse the repository at this point in the history
* Add cache_to option to docker build and publish

* Fix trailing spaces

* Remove macos old tests

* Remove old resource class for macos

* Use docker buildx

* Add platfrom option to credentials helper

* Fix arch parameter name

* Use load when generating cache

* Use a bigger image to test the cache

* Add bigger image

* Use a different tag instead of repo

* use machine executor for cache test

* Use cache from on save cache step

* Add more debug to build script

* Use arm to cache test

* Use amd64

* update cache parameters description
  • Loading branch information
marboledacci authored Sep 10, 2024
1 parent 354aaf5 commit ac2f5c4
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 25 deletions.
66 changes: 52 additions & 14 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ promotion_requires: &promotion_requires
test-check-command-docker,
test-check-command-machine,
test-check-command-macos,
test-check-command-machine-arm,
test-credentials-store-docker,
test-credentials-store-machine,
test-credentials-store-macos,
test-credentials-store-machine-arm,
test-credentials-store-docker-custom-tag,
test-credentials-store-machine-custom-tag,
test-credentials-store-macos-custom-tag,
Expand All @@ -25,7 +27,6 @@ promotion_requires: &promotion_requires
test-install-docker-tools-docker-latest,
test-install-docker-tools-docker-old,
test-install-docker-tools-macos-latest,
test-install-docker-tools-macos-old,
test-install-docker-tools-machine-latest,
test-install-docker-tools-machine-old,
test-install-docker-tools-machine-arm,
Expand All @@ -35,7 +36,6 @@ promotion_requires: &promotion_requires
test-docker-latest,
test-docker-old,
test-macos-latest,
test-macos-old,
test-machine-latest,
test-machine-old,
test-machine-arm,
Expand Down Expand Up @@ -124,12 +124,17 @@ jobs:
type: env_var_name
use-docker-credentials-store:
type: boolean
arch:
default: amd64
type: enum
enum: [amd64, arm64]
executor: <<parameters.executor>>
steps:
- docker/check:
docker-username: <<parameters.docker-username>>
docker-password: <<parameters.docker-password>>
use-docker-credentials-store: <<parameters.use-docker-credentials-store>>
arch: <<parameters.arch>>
test-credentials-store:
parameters:
executor:
Expand All @@ -145,11 +150,16 @@ jobs:
release-tag:
type: string
default: ""
arch:
default: amd64
type: enum
enum: [amd64, arm64]
executor: <<parameters.executor>>
steps:
- docker/install-docker-credential-helper:
helper-name: <<parameters.helper-name>>
release-tag: <<parameters.release-tag>>
arch: <<parameters.arch>>
- docker/configure-docker-credentials-store:
helper-name: <<parameters.helper-name>>
- run:
Expand Down Expand Up @@ -360,6 +370,15 @@ workflows:
docker-password: DOCKER_TOKEN
use-docker-credentials-store: true
filters: *filters
- test-check-command:
name: test-check-command-machine-arm
executor: machine-arm
context: cimg-docker-image-building
docker-username: DOCKER_USER
docker-password: DOCKER_TOKEN
arch: arm64
use-docker-credentials-store: true
filters: *filters
- test-check-command:
name: test-check-command-macos
executor: macos-latest
Expand Down Expand Up @@ -388,6 +407,14 @@ workflows:
docker-username: DOCKER_USER
docker-password: DOCKER_TOKEN
filters: *filters
- test-credentials-store:
name: test-credentials-store-machine-arm
executor: machine-arm
context: cimg-docker-image-building
docker-username: DOCKER_USER
docker-password: DOCKER_TOKEN
arch: arm64
filters: *filters
- test-credentials-store:
name: test-credentials-store-macos
executor: macos-latest
Expand Down Expand Up @@ -467,17 +494,33 @@ workflows:
docker-password: DOCKER_TOKEN
use-docker-credentials-store: true
filters: *filters
- docker/publish:
name: publish-docker-save-cache
executor: machine-latest
context: cimg-docker-image-building
dockerfile: test4.Dockerfile
image: ccitest/docker-orb-test
tag: $CIRCLE_SHA1
cache_to: ccitest/docker-orb-test:cache-big,ccitest/docker-orb-test:cache2-big
cache_from: ccitest/docker-orb-test:cache2-big
docker-username: DOCKER_USER
docker-password: DOCKER_TOKEN
pre-steps:
- docker/install-docker-credential-helper:
# release-tag: v0.6.3
- docker/configure-docker-credentials-store:
helper-name: pass
filters: *filters
- docker/publish:
name: publish-docker-cache
requires:
- publish-docker
executor: docker-latest
- publish-docker-save-cache
executor: machine-latest
context: cimg-docker-image-building
use-remote-docker: true
dockerfile: test.Dockerfile
dockerfile: test4.Dockerfile
image: ccitest/docker-orb-test
tag: $CIRCLE_SHA1
cache_from: ccitest/docker-orb-test:$CIRCLE_SHA1
cache_from: ccitest/docker-orb-test:cache-big
docker-username: DOCKER_USER
docker-password: DOCKER_TOKEN
pre-steps:
Expand Down Expand Up @@ -580,7 +623,7 @@ workflows:
name: test-install-docker-tools-<< matrix.executor >>
matrix:
parameters:
executor: [macos-latest, macos-old]
executor: [macos-latest]
install-goss: false
filters: *filters
# end test-install-docker-tools
Expand All @@ -596,7 +639,7 @@ workflows:
name: test-<< matrix.executor >>
matrix:
parameters:
executor: [macos-latest, macos-old, machine-arm]
executor: [macos-latest, machine-arm]
install-goss: false
filters: *filters
# end test
Expand All @@ -617,14 +660,9 @@ workflows:
only: /^v[0-9]+\.[0-9]+\.[0-9]+$/

executors:
macos-old:
macos:
xcode: 14.3.1
resource_class: macos.x86.medium.gen2
macos-latest:
macos:
xcode: 15.2.0
resource_class: macos.x86.medium.gen2
docker-old:
docker:
- image: cimg/base:2020.08-20.04
Expand Down
14 changes: 12 additions & 2 deletions src/commands/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ parameters:
default: ""
description: >
Comma-separated list of images, images will first be pulled, then
passed as the --cache-from build argument
https://docs.docker.com/engine/reference/commandline/build/
passed as the --cache-from build argument.
This parameter only support registry type, the accepted syntax is:
cache_from: user/app:cache,user/app2:cache2
cache_to:
type: string
default: ""
description: >
Comman-separated list of images where cache will be pushed to.
This parameter only support registry type, the accepted syntax is:
cache_to: user/app:cache,user/app2:cache2
debug:
type: boolean
Expand Down Expand Up @@ -128,6 +137,7 @@ steps:
no_output_timeout: << parameters.no_output_timeout >>
environment:
PARAM_CACHE_FROM: <<parameters.cache_from>>
PARAM_CACHE_TO: <<parameters.cache_to>>
PARAM_DOCKER_CONTEXT: <<parameters.docker-context>>
PARAM_DOCKERFILE_NAME: <<parameters.dockerfile>>
PARAM_DOCKERFILE_PATH: <<parameters.path>>
Expand Down
12 changes: 11 additions & 1 deletion src/commands/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ parameters:
default: docker.io
description: Name of registry to use, defaults to docker.io

arch:
type: enum
default: amd64
enum: [amd64, arm64]
description: |
Which architecture is being used.
Values accepted are amd64 and arm64. Defaults to amd64.
When running on MacOS arm64 will be used.
docker-username:
type: env_var_name
default: DOCKER_LOGIN
Expand All @@ -32,7 +41,8 @@ steps:
- when:
condition: <<parameters.use-docker-credentials-store>>
steps:
- install-docker-credential-helper
- install-docker-credential-helper:
arch: <<parameters.arch>>
- configure-docker-credentials-store

- run:
Expand Down
10 changes: 9 additions & 1 deletion src/commands/install-docker-credential-helper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ parameters:
Note: Pre or alpha releases cannot be specified.
type: string
default: ""

arch:
type: enum
default: amd64
enum: [amd64, arm64]
description: |
Which architecture is being used.
Values accepted are amd64 and arm64. Defaults to amd64.
When running on MacOS arm64 will be used.
steps:
- run:
name: Install Docker credential helper
environment:
PARAM_HELPER_NAME: << parameters.helper-name >>
PARAM_RELEASE_TAG: << parameters.release-tag >>
PARAM_ARCH: << parameters.arch >>
SCRIPT_UTILS: <<include(scripts/utils.sh)>>
command: << include(scripts/install-docker-credential-helper.sh) >>
15 changes: 15 additions & 0 deletions src/examples/with-cache-to.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
description: >
Build/publish a Docker image using --cache-to
usage:
version: 2.1

orbs:
docker: circleci/[email protected]

workflows:
build-docker-image-only:
jobs:
- docker/publish:
image: $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME
tag: latest
cache_to: $CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME:cache
13 changes: 11 additions & 2 deletions src/jobs/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ parameters:
default: ""
description: >
Comma-separated list of images to pull before build for --cache-from
https://docs.docker.com/engine/reference/commandline/build/
This parameter only support registry type, the accepted syntax is:
cache_from: user/app:cache,user/app2:cache2
cache_to:
type: string
default: ""
description: >
Comman-separated list of images where cache will be pushed to.
This parameter only support registry type, the accepted syntax is:
cache_to: user/app:cache,user/app2:cache2
after_checkout:
description: Optional steps to run after checking out the code
Expand Down Expand Up @@ -135,7 +144,6 @@ parameters:
description: >
Name of environment variable storing your Docker password
attach-at:
type: string
default: ""
Expand Down Expand Up @@ -200,6 +208,7 @@ steps:
image: <<parameters.image>>
tag: <<parameters.tag>>
cache_from: <<parameters.cache_from>>
cache_to: <<parameters.cache_to>>
extra_build_args: <<parameters.extra_build_args>>
lint-dockerfile: <<parameters.lint-dockerfile>>
treat-warnings-as-errors: <<parameters.treat-warnings-as-errors>>
Expand Down
8 changes: 7 additions & 1 deletion src/scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ if [ -n "$PARAM_CACHE_FROM" ]; then
build_args+=("--cache-from=$PARAM_CACHE_FROM")
fi

if [ -n "$PARAM_CACHE_TO" ]; then
docker buildx create --name cache --use
docker buildx use cache
build_args+=("--cache-to=$PARAM_CACHE_TO" --load)
fi

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

Expand All @@ -74,7 +80,7 @@ IFS=' '

set -x
# shellcheck disable=SC2048 # We want word splitting here.
docker build ${build_args[*]}
docker buildx build ${build_args[*]}
set +x

IFS="$old_ifs"
12 changes: 8 additions & 4 deletions src/scripts/install-docker-credential-helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ expand_env_vars_with_prefix "PARAM_"

HELPER_NAME="$PARAM_HELPER_NAME"

if uname | grep -q "Darwin"; then platform="darwin"
else platform="linux"
if uname | grep -q "Darwin"; then
platform="darwin"
arch="arm64"
else
platform="linux"
arch="$PARAM_ARCH"
fi

# Infer helper name from the platform
Expand Down Expand Up @@ -75,11 +79,11 @@ minor_version="$(echo "$RELEASE_VERSION" | cut -d. -f2)"
download_base_url="$base_url/download/${RELEASE_VERSION}/${HELPER_FILENAME}-${RELEASE_VERSION}"

if [ "$minor_version" -gt 6 ]; then
DOWNLOAD_URL="$download_base_url.$platform-amd64"
DOWNLOAD_URL="$download_base_url.$platform-$arch"
echo "Downloading from url: $DOWNLOAD_URL"
curl -L -o "${HELPER_FILENAME}" "$DOWNLOAD_URL"
else
DOWNLOAD_URL="$download_base_url-amd64.tar.gz"
DOWNLOAD_URL="$download_base_url-$arch.tar.gz"
echo "Downloading from url: $DOWNLOAD_URL"
curl -L -o "${HELPER_FILENAME}_archive" "$DOWNLOAD_URL"
tar xvf "./${HELPER_FILENAME}_archive"
Expand Down
54 changes: 54 additions & 0 deletions test4.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
FROM ubuntu:22.04

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Update and install common packages (same as base)
RUN apt-get update && apt-get install -y \
python3.10 \
python3-pip \
curl \
wget \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install common Python packages (same as base)
RUN pip3 install --no-cache-dir --upgrade pip && \
pip3 install --no-cache-dir \
numpy \
pandas \
scikit-learn \
matplotlib

# Install additional packages
RUN apt-get update && apt-get install -y \
nodejs \
npm \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install TensorFlow and Keras
RUN pip3 install --no-cache-dir tensorflow keras

# Install PyTorch
RUN pip3 install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

# Install some Node.js packages globally
RUN npm install -g \
express \
lodash \
moment

# Create large files to simulate longer build time
RUN dd if=/dev/urandom of=/large_file_1 bs=1M count=500 && \
dd if=/dev/urandom of=/large_file_2 bs=1M count=500 && \
rm /large_file_1 /large_file_2

# Set working directory
WORKDIR /app

# Create a simple Python script that uses one of the new packages
RUN echo 'import tensorflow as tf; print(f"TensorFlow version: {tf.__version__}")' > tf_version.py

CMD ["python3", "tf_version.py"]

0 comments on commit ac2f5c4

Please sign in to comment.