Skip to content

Commit 8ea9a27

Browse files
committed
Extract all base container dependencies for multi-stage builds (OBS-68)
1 parent e8b00dc commit 8ea9a27

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

Build/Docker.pm

+17-9
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,18 @@ sub cmd_apt_get {
169169
sub parse {
170170
my ($cf, $fn) = @_;
171171

172-
my $basecontainer;
173172
my $unorderedrepos;
174173
my $useobsrepositories;
175174
my $nosquash;
176-
my $dockerfile_data = slurp($fn);
177-
return { 'error' => 'could not open Dockerfile' } unless defined $dockerfile_data;
178-
175+
my $dockerfile_data;
176+
if (ref($fn) eq 'SCALAR') {
177+
$dockerfile_data = $$fn;
178+
} else {
179+
$dockerfile_data = slurp($fn);
180+
return { 'error' => 'could not open Dockerfile' } unless defined $dockerfile_data;
181+
}
179182
my @lines = split(/\r?\n/, $dockerfile_data);
183+
180184
my $ret = {
181185
'deps' => [],
182186
'path' => [],
@@ -261,9 +265,15 @@ sub parse {
261265
# process commands
262266
if ($cmd eq 'FROM') {
263267
shift @args if @args && $args[0] =~ /^--platform=/;
264-
if (@args && !$basecontainer && $args[0] ne 'scratch') {
265-
$basecontainer = $args[0];
266-
$basecontainer .= ':latest' unless $basecontainer =~ /:[^:\/]+$/;
268+
if (@args) {
269+
my $container = $args[0];
270+
if ($container ne 'scratch') {
271+
$container .= ':latest' unless $container =~ /:[^:\/]+$/;
272+
$container = "container:$container";
273+
if ($container && !grep {$_ eq $container} @{$ret->{'deps'}}) {
274+
push @{$ret->{'deps'}}, $container;
275+
}
276+
}
267277
}
268278
$vars_env = {}; # should take env from base container
269279
$vars = { %$vars_env };
@@ -301,7 +311,6 @@ sub parse {
301311
}
302312
}
303313
}
304-
push @{$ret->{'deps'}}, "container:$basecontainer" if $basecontainer;
305314
push @{$ret->{'deps'}}, '--unorderedimagerepos' if $unorderedrepos;
306315
my $version = $ret->{'version'};
307316
my $release = $cf->{'buildrelease'};
@@ -311,7 +320,6 @@ sub parse {
311320
}
312321
$ret->{'name'} = 'docker' if !defined($ret->{'name'}) && !$cf->{'__dockernoname'};
313322
$ret->{'path'} = [ { 'project' => '_obsrepositories', 'repository' => '' } ] if $useobsrepositories;
314-
$ret->{'basecontainer'} = $basecontainer if $basecontainer;
315323
$ret->{'nosquash'} = 1 if $nosquash;
316324
return $ret;
317325
}

help

-8
This file was deleted.

obs_example/Dockerfile

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
FROM opensuse:latest
1+
FROM opensuse/tumbleweed AS tw
22

3+
# debug
4+
RUN cat /etc/os-release
5+
6+
FROM opensuse/leap:15.2
37
# Make obs_pkg_mgr available
48
ARG OBS_REPOSITORY_URL
59
ADD obs_pkg_mgr /usr/bin/obs_pkg_mgr
610
RUN chmod +x /usr/bin/obs_pkg_mgr
711

8-
RUN obs_pkg_mgr add_repo http://download.opensuse.org/repositories/Virtualization:/containers/openSUSE_Leap_42.2/ "Virtualization:Containers (openSUSE_Leap_42.2)" && ls ; obs_pkg_mgr add_repo http://example.com/my_project:/mysubproject/my_package/ "My example project" && ls
12+
RUN obs_pkg_mgr add_repo http://download.opensuse.org/repositories/Virtualization:/containers/openSUSE_Leap_15.2/ "Virtualization:Containers (openSUSE_Leap_15.2)" && ls ; obs_pkg_mgr add_repo http://example.com/my_project:/mysubproject/my_package/ "My example project" && ls
913

1014
RUN obs_pkg_mgr install util-linux calcurse # amarok
1115
RUN obs_pkg_mgr install awk gcc \

t/parse_docker.t

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/perl -w
2+
3+
use strict;
4+
use Test::More tests => 1;
5+
6+
use Build;
7+
use Build::Docker;
8+
9+
my ($dockerfile);
10+
my $result;
11+
my $expected;
12+
13+
my $conf = Build::read_config('x86_64');
14+
15+
$dockerfile = q{
16+
FROM opensuse/tumbleweed AS tw
17+
18+
# debug
19+
RUN cat /etc/os-release
20+
21+
FROM opensuse/leap:15.2
22+
};
23+
24+
$expected = {
25+
'path' => [],
26+
'deps' => ['container:opensuse/tumbleweed:latest', 'container:opensuse/leap:15.2'],
27+
'imagerepos' => [],
28+
'name' => 'docker'
29+
};
30+
31+
$result = Build::Docker::parse($conf, \$dockerfile);
32+
is_deeply($result, $expected);

0 commit comments

Comments
 (0)