Skip to content

Commit a6e8d17

Browse files
committed
templates: improve docker build layers caching
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
1 parent 553f7f8 commit a6e8d17

5 files changed

Lines changed: 59 additions & 31 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ jobs:
6161
- quay.io/centos/centos:stream
6262

6363
steps:
64+
- name: Free Disk Space (Ubuntu)
65+
uses: linka-cloud/free-disk-space@main
66+
if: matrix.image == 'quay.io/centos/centos:stream'
67+
6468
- name: Checkout
6569
uses: actions/checkout@v3
6670
with:
@@ -109,6 +113,9 @@ jobs:
109113
- centos:8
110114
- quay.io/centos/centos:stream10
111115
steps:
116+
- name: Free Disk Space (Ubuntu)
117+
uses: linka-cloud/free-disk-space@main
118+
112119
- name: Checkout
113120
uses: actions/checkout@v3
114121
with:

config_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,30 @@ import (
2424
"github.com/google/uuid"
2525
"github.com/sirupsen/logrus"
2626
"github.com/stretchr/testify/require"
27+
"go.uber.org/multierr"
2728

2829
"go.linka.cloud/d2vm/pkg/docker"
2930
"go.linka.cloud/d2vm/pkg/exec"
3031
)
3132

32-
func testConfig(t *testing.T, ctx context.Context, name, img string, config Config, luks, grubBIOS, grubEFI bool) {
33+
func testConfig(t *testing.T, ctx context.Context, name, img string, config Config, luks, grubBIOS, grubEFI bool) func() error {
3334
require.NoError(t, docker.Pull(ctx, Arch, img))
3435
tmpPath := filepath.Join(os.TempDir(), "d2vm-tests", strings.NewReplacer(":", "-", ".", "-").Replace(name))
3536
require.NoError(t, os.MkdirAll(tmpPath, 0755))
3637
defer os.RemoveAll(tmpPath)
3738
logrus.Infof("inspecting image %s", img)
3839
r, err := FetchDockerImageOSRelease(ctx, img)
3940
require.NoError(t, err)
40-
defer docker.Remove(ctx, img)
41+
var fns []func() error
42+
clean := func() (err error) {
43+
for _, v := range fns {
44+
err = multierr.Append(err, v())
45+
}
46+
return err
47+
}
48+
fns = append(fns, func() error {
49+
return docker.Remove(ctx, img)
50+
})
4151
if !r.SupportsLUKS() && luks {
4252
t.Skipf("LUKS not supported for %s", r.Version)
4353
}
@@ -53,13 +63,16 @@ func testConfig(t *testing.T, ctx context.Context, name, img string, config Conf
5363
imgUUID := uuid.New().String()
5464
logrus.Infof("building kernel enabled image")
5565
require.NoError(t, docker.Build(ctx, false, imgUUID, p, dir, Arch))
56-
defer docker.Remove(ctx, imgUUID)
66+
fns = append(fns, func() error {
67+
return docker.Remove(ctx, imgUUID)
68+
})
5769
// we don't need to test the kernel location if grub is enabled
5870
if grubBIOS || grubEFI {
59-
return
71+
return clean
6072
}
6173
require.NoError(t, docker.RunAndRemove(ctx, imgUUID, "test", "-f", config.Kernel))
6274
require.NoError(t, docker.RunAndRemove(ctx, imgUUID, "test", "-f", config.Initrd))
75+
return clean
6376
}
6477

6578
func TestConfig(t *testing.T) {
@@ -147,12 +160,17 @@ func TestConfig(t *testing.T) {
147160
}
148161
}
149162
name := strings.Join(n, "-")
163+
var clean func() error
150164
t.Run(name, func(t *testing.T) {
151-
t.Parallel()
152165
ctx, cancel := context.WithCancel(context.Background())
153166
defer cancel()
154-
testConfig(t, ctx, name, test.image, test.config, luks, grubBIOS, grubEFI)
167+
clean = testConfig(t, ctx, name, test.image, test.config, luks, grubBIOS, grubEFI)
155168
})
169+
defer func() {
170+
if clean != nil {
171+
_ = clean()
172+
}
173+
}()
156174
}
157175
}
158176
}

templates/centos.Dockerfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@ RUN yum install -y \
1616
kernel \
1717
systemd \
1818
NetworkManager \
19-
{{- if .GrubBIOS }}
20-
grub2 \
21-
{{- end }}
22-
{{- if .GrubEFI }}
23-
grub2 grub2-efi-x64 grub2-efi-x64-modules \
24-
{{- end }}
2519
e2fsprogs \
2620
sudo && \
2721
systemctl enable NetworkManager && \
2822
systemctl unmask systemd-remount-fs.service && \
2923
systemctl unmask getty.target && \
3024
find /boot -type l -exec rm {} \;
3125

26+
{{- if .GrubBIOS }}
27+
RUN yum install -y grub2
28+
{{- end }}
29+
{{- if .GrubEFI }}
30+
RUN yum install -y grub2 grub2-efi-x64 grub2-efi-x64-modules
31+
{{- end }}
32+
3233
{{ if .Luks }}
3334
RUN yum install -y cryptsetup && \
3435
dracut --no-hostonly --regenerate-all --force --install="/usr/sbin/cryptsetup"

templates/debian.Dockerfile

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \
1919
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
2020
systemd-sysv \
2121
systemd \
22-
{{- if .Grub }}
23-
grub-common \
24-
grub2-common \
25-
{{- end }}
26-
{{- if .GrubBIOS }}
27-
grub-pc-bin \
28-
{{- end }}
29-
{{- if .GrubEFI }}
30-
grub-efi-${ARCH}-bin \
31-
{{- end }}
3222
dbus \
3323
iproute2 \
3424
isc-dhcp-client \
3525
iputils-ping
3626

27+
{{- if .Grub }}
28+
RUN DEBIAN_FRONTEND=noninteractive apt install -y grub-common grub2-common
29+
{{- end }}
30+
{{- if .GrubBIOS }}
31+
RUN DEBIAN_FRONTEND=noninteractive apt install -y grub-pc-bin
32+
{{- end }}
33+
{{- if .GrubEFI }}
34+
RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \
35+
DEBIAN_FRONTEND=noninteractive apt install -y grub-efi-${ARCH}-bin
36+
{{- end }}
37+
3738
RUN systemctl preset-all
3839

3940
{{ if .Password }}RUN echo "root:{{ .Password }}" | chpasswd {{ end }}

templates/ubuntu.Dockerfile

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \
99
initramfs-tools \
1010
systemd-sysv \
1111
systemd \
12+
dbus \
13+
isc-dhcp-client \
14+
iproute2 \
15+
iputils-ping && \
16+
find /boot -type l -exec rm {} \;
17+
1218
{{- if .Grub }}
13-
grub-common \
14-
grub2-common \
19+
RUN DEBIAN_FRONTEND=noninteractive apt install -y grub-common grub2-common
1520
{{- end }}
1621
{{- if .GrubBIOS }}
17-
grub-pc-bin \
22+
RUN DEBIAN_FRONTEND=noninteractive apt install -y grub-pc-bin
1823
{{- end }}
1924
{{- if .GrubEFI }}
20-
grub-efi-${ARCH}-bin \
25+
RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \
26+
DEBIAN_FRONTEND=noninteractive apt install -y grub-efi-${ARCH}-bin
2127
{{- end }}
22-
dbus \
23-
isc-dhcp-client \
24-
iproute2 \
25-
iputils-ping && \
26-
find /boot -type l -exec rm {} \;
2728

2829
{{ if gt .Release.VersionID "16.04" }}
2930
RUN systemctl preset-all

0 commit comments

Comments
 (0)