diff mbox series

[RFC,v2,2/3] gitlab: Cache container images

Message ID 20230224125207.19616-3-farosas@suse.de
State New
Headers show
Series ci: Speed up container stage | expand

Commit Message

Fabiano Rosas Feb. 24, 2023, 12:52 p.m. UTC
Make use of the --cache-from option from 'docker build' by including
build layers when building and then pulling a previously built image
before the next build.

This was previously done by the docker.py script, but got disabled due
to bad interactions with certain runners. See commit 6ddc3dc7a8
("tests/docker: don't use BUILDKIT in GitLab either").

We now believe those issues to be fixed, so restore the caching
functionality as it brings a significant reduction in container build
times.

Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
 .gitlab-ci.d/container-template.yml | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Daniel P. Berrangé Feb. 27, 2023, 11:09 a.m. UTC | #1
On Fri, Feb 24, 2023 at 09:52:06AM -0300, Fabiano Rosas wrote:
> Make use of the --cache-from option from 'docker build' by including
> build layers when building and then pulling a previously built image
> before the next build.
> 
> This was previously done by the docker.py script, but got disabled due
> to bad interactions with certain runners. See commit 6ddc3dc7a8
> ("tests/docker: don't use BUILDKIT in GitLab either").
> 
> We now believe those issues to be fixed, so restore the caching
> functionality as it brings a significant reduction in container build
> times.
> 
> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> ---
>  .gitlab-ci.d/container-template.yml | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/.gitlab-ci.d/container-template.yml b/.gitlab-ci.d/container-template.yml
> index f417452212..31e4e36a7d 100644
> --- a/.gitlab-ci.d/container-template.yml
> +++ b/.gitlab-ci.d/container-template.yml
> @@ -13,7 +13,10 @@
>    script:
>      - echo "TAG:$TAG"
>      - echo "COMMON_TAG:$COMMON_TAG"
> -    - docker build --tag $TAG -f "tests/docker/dockerfiles/$NAME.docker" "."
> +    - docker pull "$TAG" || true
> +    - docker build --tag "$TAG" --cache-from "$TAG"

We should have

   --cache-from "$TAG" --cache-from "$COMMON_TAG"

The reason is that users may not keep their branches up2date on a regular
basis. As such the registry associated with the fork may not be useful
as a caching source. Thus if we include cache from the upstream repo, this
will benefit forks.  This was the rational behind having TAG + COMMON_TAG
originally, but we lost it somewhere along the way.

> +      --build-arg BUILDKIT_INLINE_CACHE=1
> +      -f "tests/docker/dockerfiles/$NAME.docker" "."
>      - docker push "$TAG"
>    after_script:
>      - docker logout

With regards,
Daniel
diff mbox series

Patch

diff --git a/.gitlab-ci.d/container-template.yml b/.gitlab-ci.d/container-template.yml
index f417452212..31e4e36a7d 100644
--- a/.gitlab-ci.d/container-template.yml
+++ b/.gitlab-ci.d/container-template.yml
@@ -13,7 +13,10 @@ 
   script:
     - echo "TAG:$TAG"
     - echo "COMMON_TAG:$COMMON_TAG"
-    - docker build --tag $TAG -f "tests/docker/dockerfiles/$NAME.docker" "."
+    - docker pull "$TAG" || true
+    - docker build --tag "$TAG" --cache-from "$TAG"
+      --build-arg BUILDKIT_INLINE_CACHE=1
+      -f "tests/docker/dockerfiles/$NAME.docker" "."
     - docker push "$TAG"
   after_script:
     - docker logout