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

Cache distribution package downloads with BuildKit cache mounts #224

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ unreleased
----------

- Add Alpine 3.21, deprecate Alpine 3.20. (@MisterDA, #225)
- Cache packages downloads for Apt (Debian, Ubuntu) and pacman (Arch
Linux) based distributions using BuildKit cache mounts.
(@MisterDA, #224)

v8.2.4 2024-11-18
-----------------
Expand Down
28 changes: 23 additions & 5 deletions src-opam/linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,30 @@ end

(** Debian rules *)
module Apt = struct
(* https://docs.docker.com/reference/dockerfile/#example-cache-apt-packages *)
let cache_mounts =
[
mount_cache ~target:"/var/cache/apt" ~sharing:`Locked ();
mount_cache ~target:"/var/lib/apt" ~sharing:`Locked ();
]

let update =
run "apt-get -y update"
@@ run "DEBIAN_FRONTEND=noninteractive apt-get -y upgrade"
run
{|mv /etc/apt/apt.conf.d/docker-clean /tmp/; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache|}
@@ run ~mounts:cache_mounts
"apt update && DEBIAN_FRONTEND=noninteractive apt-get -y upgrade"
@@ run
"mv /tmp/docker-clean /etc/apt/apt.conf.d/ && rm -f \
/etc/apt/apt.conf.d/keep-cache"

let install fmt =
ksprintf
(fun s ->
update @@ run "DEBIAN_FRONTEND=noninteractive apt-get -y install %s" s)
update
@@ run ~mounts:cache_mounts
"DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends \
install -y %s"
s)
fmt

let dev_packages ?extra () =
Expand Down Expand Up @@ -242,11 +258,13 @@ end

(** Pacman rules *)
module Pacman = struct
let update = run "pacman -Syu --noconfirm && yes | pacman -Scc"
let cache_mount = mount_cache ~target:"/var/cache/pacman" ~sharing:`Locked ()
let update = run ~mounts:[ cache_mount ] "pacman -Syu --noconfirm --needed"

let install fmt =
ksprintf
(fun s -> run "pacman -Syu --noconfirm %s && yes | pacman -Scc" s)
(fun s ->
run ~mounts:[ cache_mount ] "pacman -Syu --noconfirm --needed %s" s)
fmt

let dev_packages ?extra () =
Expand Down
10 changes: 6 additions & 4 deletions src-opam/linux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ end
module Apt : sig
val update : t
(** [update] will run [apt-get update && apt-get upgrade] non-interactively.
*)
Requires [syntax=docker/dockerfile:1]. *)

val install : ('a, unit, string, t) format4 -> 'a
(** [install fmt] will [apt-get update && apt-get install] the packages
specified by the [fmt] format string. *)
specified by the [fmt] format string. Requires
[syntax=docker/dockerfile:1]. *)

val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t
(** [add_user username] will install a new user with name [username] and a
Expand Down Expand Up @@ -150,11 +151,12 @@ end
(** Rules for Pacman-based distributions such as Archlinux *)
module Pacman : sig
val update : t
(** [update] will run [pacman -Syu] non-interactively. *)
(** [update] will run [pacman -Syu] non-interactively. Requires
[syntax=docker/dockerfile:1]. *)

val install : ('a, unit, string, t) format4 -> 'a
(** [install fmt] will [pacman -Syu] the packages specified by the [fmt]
format string. *)
format string. Requires [syntax=docker/dockerfile:1]. *)

val add_user : ?uid:int -> ?gid:int -> ?sudo:bool -> string -> t
(** [add_user username] will install a new user with name [username] and a
Expand Down