API.pm

filter: Statementstmt 88.8% 248 / 279 Branchbran 71.1% 64 / 90 Conditioncond 60.3% 32 / 53 MC/DCmcdc 24.1% 7 / 29 Subroutinesub 94.4% 34 / 36 Podpod 28.5% 2 / 7 totaltotal 78.3% 387 / 494 CC 76 SCAR 22.4
CC
76
Cov
82%
release
21.3
_template_vars
13.7
before_build
11.1
CRAP 9.4
« API/Client.pm ↑ Summary API/TagTemplate.pm »
1 package Dist::Zilla::Plugin::Docker::API;
2 # ABSTRACT: Build and publish Docker images as Dist::Zilla release artifacts
3 11 our $VERSION = '0.107';
4 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 with 'Dist::Zilla::Role::Plugin';
6 11 with 'Dist::Zilla::Role::BeforeBuild';
7 11 with 'Dist::Zilla::Role::AfterBuild';
8 11 with 'Dist::Zilla::Role::Releaser';
9  
10 11 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use Dist::Zilla::Plugin::Docker::API::TagTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Dist::Zilla::Plugin::Docker::API::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Primary attributes
19 11 has image => (
20 is => 'ro',
21 isa => 'Str',
22 required => 1,
23 init_arg => 'image',
24 );
25  
26 # Deprecated reader; the constructor key is funneled into image by BUILDARGS.
27 has repository => (
28 is => 'ro',
29 isa => 'Str',
30 lazy => 1,
31 init_arg => undef,
32 3 ▶ default => sub { shift->image },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 11 );
34  
35 # The dist.ini key is 'dockerfile'. 'file' is the deprecated spelling and is
36 # funneled in by BUILDARGS -- it used to be the only one that worked, because
37 # this attribute carried init_arg => 'file' while the POD documented
38 # 'dockerfile'.
39 11 has dockerfile => (
40 is => 'ro',
41 isa => 'Str',
42 default => 'Dockerfile',
43 );
44  
45 # Canonical tag attribute: one list, applied both at build (locally) and
46 # at release (pushed). Deprecated build_tag / release_tag funnel into here
47 # via BUILDARGS.
48 11 has tag => (
49 is => 'ro',
50 isa => 'ArrayRef[Str]',
51 lazy => 1,
52 builder => '_build_tag_default',
53 );
54  
55 20 ▶sub _build_tag_default { ['latest', '%V', '%v'] }
Subroutine _build_tag_default: called CC=1 SCAR=0.0
56  
57 has build_arg => (
58 is => 'ro',
59 isa => 'ArrayRef[Str]',
60 39 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 11 );
62  
63 has label => (
64 is => 'ro',
65 isa => 'ArrayRef[Str]',
66 39 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 11 );
68  
69 has platform => (
70 is => 'ro',
71 isa => 'ArrayRef[Str]',
72 37 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 11 );
74  
75 # Release behavior
76 11 has release_push => (
77 is => 'ro',
78 isa => 'Bool',
79 default => 1,
80 );
81  
82 # Deprecated reader; the constructor key is funneled into release_push.
83 has push => (
84 is => 'ro',
85 isa => 'Bool',
86 lazy => 1,
87 init_arg => undef,
88 1 ▶ default => sub { shift->release_push },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 11 );
90  
91 # When false (default), the build log only echoes Dockerfile step headers
92 # (e.g. "Step 3/18 : RUN apt-get update", Podman's "STEP 3/18: ..." or
93 # BuildKit's "#5 [4/12] ..."),
94 # not the per-command output. Set to true for the full stream.
95 11 has build_verbose => (
96 is => 'ro',
97 isa => 'Bool',
98 default => 0,
99 );
100  
101 11 has release_enabled => (
102 is => 'ro',
103 isa => 'Bool',
104 default => 1,
105 );
106  
107 # Common options
108 11 has pull => (
109 is => 'ro',
110 isa => 'Bool',
111 default => 0,
112 );
113  
114 11 has no_cache => (
115 is => 'ro',
116 isa => 'Bool',
117 default => 0,
118 );
119  
120 11 has rm => (
121 is => 'ro',
122 isa => 'Bool',
123 default => 1,
124 );
125  
126 11 has force_rm => (
127 is => 'ro',
128 isa => 'Bool',
129 default => 1,
130 );
131  
132 11 has target => (
133 is => 'ro',
134 isa => 'Str',
135 default => '',
136 init_arg => '_target',
137 );
138  
139 11 has network_mode => (
140 is => 'ro',
141 isa => 'Str',
142 default => '',
143 init_arg => '_network_mode',
144 );
145  
146 11 has fail_if_tag_exists => (
147 is => 'ro',
148 isa => 'Bool',
149 default => 0,
150 );
151  
152 11 has skip_latest_on_trial => (
153 is => 'ro',
154 isa => 'Bool',
155 default => 1,
156 );
157  
158 11 has client_class => (
159 is => 'ro',
160 isa => 'Str',
161 default => 'Dist::Zilla::Plugin::Docker::API::Client',
162 );
163  
164 11 has _tag_template => (
165 is => 'ro',
166 isa => 'Dist::Zilla::Plugin::Docker::API::TagTemplate',
167 lazy => 1,
168 builder => '_build_tag_template',
169 );
170  
171 11 has _client => (
172 is => 'ro',
173 isa => 'Object',
174 lazy => 1,
175 builder => '_build_client',
176 );
177  
178 18 ▶sub _build_tag_template {
Subroutine _build_tag_template: called CC=1 SCAR=0.0
179 18 my ($self) = @_;
180 18 return Dist::Zilla::Plugin::Docker::API::TagTemplate->new(
181 zilla => $self->zilla,
182 plugin_name => $self->plugin_name,
183 );
184 }
185  
186 22 ▶sub _build_client {
Subroutine _build_client: called CC=2 SCAR=8.5
187 22 my ($self) = @_;
188 22 my $client_class = $self->client_class;
189 22 ▶ unless (eval "require $client_class; 1") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $client_class; 1") 0 22 22
190 0 $self->log_fatal("Cannot load client_class $client_class: $@");
191 }
192 return $client_class->new(
193 0 ▶ logger => sub { $self->log(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 ▶ logger_fatal => sub { $self->log_fatal(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 22 );
196 }
197  
198 77 ▶sub tag_template { shift->_tag_template }
Subroutine tag_template: called CC=1 SCAR=0.0
Pod undocumented
199 82 ▶sub client { shift->_client }
Subroutine client: called CC=1 SCAR=0.0
Pod undocumented
200  
201 1 ▶sub file { shift->dockerfile }
Subroutine file: called CC=1 SCAR=0.0
Pod documented
202  
203 # API::Docker croaks through Carp, so a reason arrives carrying one or more
204 # " at FILE line N." tails. Strip all of them (not just the last) and flatten
205 # to a single line -- log_fatal repeats whatever it is given, and a
206 # multi-line message gets repeated in full.
207 3 ▶sub _flatten_error {
Subroutine _flatten_error: called CC=2 SCAR=7.0
208 3 my ($self, $error) = @_;
209  
210 3 ▶ $error = '' unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 3 3
211 3 $error =~ s/\s+at\s+\S+\s+line\s+\d+\.?//g;
212 3 $error =~ s/\s+/ /g;
213 3 $error =~ s/^\s+|\s+$//g;
214 3 return $error;
215 }
216  
217 # after_build builds an image unconditionally, so every dzil command that
218 # builds needs a reachable engine. Ask for one up front instead of letting
219 # Dist::Zilla gather, munge and write out a whole distribution first and only
220 # then die on a socket that was never there.
221 23 ▶sub before_build {
Subroutine before_build: called CC=11 SCAR=24.1
Pod undocumented
222 23 my ($self) = @_;
223  
224 23 ▶ if ($ENV{DZIL_DOCKER_API_SKIP}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DZIL_DOCKER_API_SKIP"}) 2 21 23
225 2 $self->log('DZIL_DOCKER_API_SKIP is set: skipping the image build '
226 .'for this run - no engine contact, no image');
227 2 return;
228 }
229  
230 # The classic /build endpoint takes exactly one platform; a second value
231 # would otherwise be dropped without a word. This is configuration, not
232 # engine contact, so DZIL_DOCKER_API_SKIP_PRECHECK does not skip it.
233 21 my @platforms = @{ $self->platform };
234 21 ▶ if (@platforms > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@platforms > 1) 1 20 21
235 1 $self->log_fatal('only one platform is supported, got '
236 .scalar(@platforms).': '.join(', ', @platforms)
237 .' - the /build endpoint takes exactly one platform, and this '
238 .'plugin is not built for multi-arch images');
239 }
240  
241 20 ▶ return if $ENV{DZIL_DOCKER_API_SKIP_PRECHECK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DZIL_DOCKER_API_SKIP_PRECHECK"} 2 18 20
242  
243 18 my $info = eval { $self->client->engine_info };
244 18 my $error = $@;
245  
246 18 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 17 18
247 1 $error = $self->_flatten_error($error);
248  
249 1 $self->log('Docker::API speaks the Docker Engine HTTP API over a '
250 .'socket and never shells out to the docker binary, so any engine '
251 .'serving that API will do.');
252 1 $self->log('Point DOCKER_HOST at one. For rootless Podman: '
253 .'systemctl --user enable --now podman.socket, then '
254 .'DOCKER_HOST="unix://$XDG_RUNTIME_DIR/podman/podman.sock"');
255 1 $self->log_fatal('cannot reach a container engine: '.$error
256 .' (set DZIL_DOCKER_API_SKIP_PRECHECK=1 to skip this check)');
257 }
258  
259 $self->log('Docker::API engine ready: '
260 .($info->{engine} // 'unknown').' '
261 .($info->{version} // '?')
262 17 ▶ .' (API '.($info->{api_version} // '?').')');
Condition50%3/6
$info->{"engine"} // "unknown"
l!l
170
$info->{"version"} // "?"
l!l
170
$info->{"api_version"} // "?"
l!l
170
MC/DC0%0/1
$info->{"engine"} // "unknown"
$info->{"engine"}
MC/DC0%0/1
$info->{"version"} // "?"
$info->{"version"}
MC/DC0%0/1
$info->{"api_version"} // "?"
$info->{"api_version"}
Truth table50%1/2
A
A= $info->{"engine"}
Aresult
11
00
Truth table50%1/2
A
A= $info->{"version"}
Aresult
11
00
Truth table50%1/2
A
A= $info->{"api_version"}
Aresult
11
00
263  
264 $self->_precheck_registry_auth
265 17 ▶ if $ENV{DZIL_RELEASING} && $self->release_enabled && $self->release_push;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DZIL_RELEASING"} and $self->release_enabled and $self->release_push 3 14 17
Condition83%5/6
$ENV{"DZIL_RELEASING"} and $self->release_enabled and $self->release_push
!ll&&!rl&&r
1313
$ENV{"DZIL_RELEASING"} and $self->release_enabled
!ll&&!rl&&r
1304
MC/DC66%2/3
$ENV{"DZIL_RELEASING"} and $self->release_enabled and $self->release_push
$ENV{"DZIL_RELEASING"} $self->release_enabled $self->release_push
Truth table75%3/4
A and B and C
A= $ENV{"DZIL_RELEASING"}B= $self->release_enabledC= $self->release_push
ABCresult
0XX0
10X0
1100
1111
266 }
267  
268 # A rejected or expired registry credential otherwise surfaces only when the
269 # push fails -- after Dist::Zilla gathered and wrote the distribution and
270 # after_build built and tagged the image, i.e. after the damage. This runs
271 # before any of it.
272 #
273 # The phase hook that runs early enough is before_build, and it can tell a
274 # release from a plain build: Dist::Zilla::Dist::Builder::release sets
275 # DZIL_RELEASING before it calls build_archive, so the variable is already
276 # there when this hook runs (measured against Dist::Zilla 6.037). A plain
277 # dzil build never sets it and so never needs registry credentials.
278 #
279 # Only the registry host of `image` decides which credential applies, and
280 # that part carries no template variables, so no expansion is needed here.
281 3 ▶sub _precheck_registry_auth {
Subroutine _precheck_registry_auth: called CC=3 SCAR=11.1
282 3 my ($self) = @_;
283  
284 3 my $image_ref = $self->image;
285  
286 3 my $status = eval { $self->client->verify_auth_for_image_ref($image_ref) };
287 3 my $error = $@;
288  
289 # Deliberately not worded as "rejected": the engine answers a bad
290 # credential and an unreachable registry with the same failure -- Podman
291 # returns 500 for both, so the status cannot tell them apart. The engine's
292 # own text, which does, is carried through verbatim.
293 3 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 2 3
294 1 $self->log_fatal('the registry credential check for '.$image_ref
295 .' failed: '.$self->_flatten_error($error)
296 .' (set DZIL_DOCKER_API_SKIP_PRECHECK=1 to skip this check)');
297 }
298  
299 # No credential found for that registry. An anonymous push is a legal
300 # thing to attempt, so this is a note, not a failure.
301 2 ▶ unless (defined $status) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status) 2 0 2
302 2 $self->log('Docker::API: no registry credentials found for '
303 .$image_ref.' - the release push will be anonymous');
304 2 return;
305 }
306  
307 0 $self->log('Docker::API registry credentials accepted for '.$image_ref);
308 }
309  
310 20 ▶sub after_build {
Subroutine after_build: called CC=4 SCAR=14.0
Pod undocumented
311 20 my ($self, $arg) = @_;
312  
313 20 ▶ return if $ENV{DZIL_DOCKER_API_SKIP};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DZIL_DOCKER_API_SKIP"} 2 18 20
314  
315 18 $self->log("Docker::API building image");
316  
317 18 my $build_root = $arg->{build_root};
318 18 my $zilla = $self->zilla;
319  
320 18 my %tmpl_vars = $self->_template_vars($build_root, undef, $arg->{archive});
321  
322 18 my @image_refs = $self->_resolve_tags($self->tag, %tmpl_vars);
323 18 my %labels = $self->_resolve_labels(%tmpl_vars);
324 18 my %build_args = $self->_resolve_build_args(%tmpl_vars);
325  
326 18 ▶ my $context_path = Path::Tiny->new($build_root // $self->zilla->root);
Condition33%1/3
$build_root // $self->zilla->root
l!l&&r!l&&!r
1800
MC/DC0%0/2
$build_root // $self->zilla->root
$build_root $self->zilla->root
Truth table33%1/3
A // B
A= $build_rootB= $self->zilla->root
ABresult
1X1
011
000
327 18 ▶ unless ($context_path->child($self->dockerfile)->exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context_path->child($self->dockerfile)->exists) 0 18 18
328 0 $self->log_fatal("Dockerfile '" . $self->dockerfile
329 . "' not found in build context: $context_path");
330 }
331 18 my $context_tar = {
332 type => 'dir',
333 path => $context_path->stringify,
334 };
335  
336 18 my @platforms = @{ $self->platform };
337  
338 18 my $result = $self->client->build_image(
339 context_tar => $context_tar,
340 dockerfile => $self->dockerfile,
341 tags => \@image_refs,
342 labels => \%labels,
343 buildargs => \%build_args,
344 pull => $self->pull,
345 nocache => $self->no_cache,
346 rm => $self->rm,
347 forcerm => $self->force_rm,
348 target => $self->target,
349 network_mode => $self->network_mode,
350 platform => $platforms[0],
351 verbose => $self->build_verbose,
352 );
353  
354 18 $self->_log_build_result($result);
355 }
356  
357 8 ▶sub release {
Subroutine release: called CC=19 SCAR=30.6
Pod documented
358 8 my ($self, $archive) = @_;
359  
360 # A skipped build phase means there is no image to tag and push. Refusing
361 # here beats releasing a dist whose containers silently never shipped.
362 $self->log_fatal('DZIL_DOCKER_API_SKIP is set: refusing to release '
363 .'- the image build was skipped, there is nothing to push')
364 8 ▶ if $ENV{DZIL_DOCKER_API_SKIP};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DZIL_DOCKER_API_SKIP"} 1 7 8
365  
366 # Skip if release is disabled
367 7 ▶ return unless $self->release_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->release_enabled 1 6 7
368  
369 # If no tags configured, skip
370 6 ▶ return unless @{$self->tag};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->tag;} 0 6 6
371  
372 6 ▶ $self->log("Docker::API release: tagging and " . ($self->release_push ? "pushing" : "tagging only"));
Branch: 1 branch
BranchTrueFalseTotal
$self->release_push ? : 5 1 6
373  
374 6 my $zilla = $self->zilla;
375 6 my %tmpl_vars = $self->_template_vars($zilla->root, $zilla->version, $archive);
376  
377 6 my @tags = @{ $self->tag };
378  
379 6 ▶ if ($self->skip_latest_on_trial && $zilla->is_trial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skip_latest_on_trial and $zilla->is_trial) 0 6 6
Condition33%1/3
$self->skip_latest_on_trial and $zilla->is_trial
!ll&&!rl&&r
060
MC/DC0%0/2
$self->skip_latest_on_trial and $zilla->is_trial
$self->skip_latest_on_trial $zilla->is_trial
Truth table33%1/3
A and B
A= $self->skip_latest_on_trialB= $zilla->is_trial
ABresult
0X0
100
111
380 0 @tags = grep { $_ ne 'latest' } @tags;
381 0 $self->log("Skipping 'latest' tag for trial release");
382 }
383  
384 # Source image: first tag from the build phase (same list, resolved
385 # via the same template). Build must have happened before release.
386 6 my $source_image_ref = $self->image . ':' . $self->tag_template->expand($self->tag->[0], %tmpl_vars);
387  
388 # Check if the tag exists on the remote (if we're going to push), before
389 # anything is tagged or pushed.
390 #
391 # "This engine cannot answer" is fatal here, not a warning. Whoever sets
392 # fail_if_tag_exists asked for the tag to be protected; downgrading an
393 # unanswerable check to a silent "does not exist" is exactly the bug this
394 # replaces. An engine without a /distribution route -- rootless Podman --
395 # therefore stops the release and says so.
396 6 ▶ if ($self->release_push && $self->fail_if_tag_exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->release_push and $self->fail_if_tag_exists) 3 3 6
Condition100%3/3
$self->release_push and $self->fail_if_tag_exists
!ll&&!rl&&r
123
MC/DC100%2/2
$self->release_push and $self->fail_if_tag_exists
$self->release_push $self->fail_if_tag_exists
Truth table100%3/3
A and B
A= $self->release_pushB= $self->fail_if_tag_exists
ABresult
0X0
100
111
397 3 for my $tag (@tags) {
398 5 my $image_ref = $self->_image_ref($tag, %tmpl_vars);
399 5 my $exists = eval { $self->client->remote_tag_exists($image_ref) };
400 5 my $error = $@;
401  
402 5 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 4 5
403 1 $self->log_fatal('fail_if_tag_exists is set, but this engine '
404 ."cannot answer whether '$image_ref' already exists on the "
405 .'remote registry: '.$self->_flatten_error($error)
406 .' - set fail_if_tag_exists = 0 to release without the check');
407 }
408  
409 4 ▶ if ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) 1 3 4
410 1 $self->log_fatal("Tag '$tag' already exists on remote registry"
411 ." ($image_ref)");
412 }
413 }
414 }
415  
416 # Tag existing image with release tags. If the source image is missing
417 # (build never ran, or someone pruned the daemon), the underlying
418 # Docker API will return a real 404 — surface that as a fatal error
419 # instead of fabricating our own pre-check.
420 4 my @image_refs = $self->_resolve_tags(\@tags, %tmpl_vars);
421 4 for my $target_ref (@image_refs) {
422 12 ▶ next if $target_ref eq $source_image_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $target_ref eq $source_image_ref 4 8 12
423 8 eval {
424 8 $self->client->tag_image(source => $source_image_ref, target => $target_ref);
425 };
426 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
427 0 $self->log_fatal("Failed to tag '$source_image_ref' as '$target_ref': $@");
428 }
429 8 $self->log("Tagged: $target_ref");
430 }
431  
432 # Push if enabled
433 4 ▶ if ($self->release_push) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->release_push) 3 1 4
434 3 my @failed;
435 3 for my $image_ref (@image_refs) {
436 9 $self->log("Pushing $image_ref...");
437 9 eval {
438 9 $self->client->push_image(image_ref => $image_ref);
439 };
440 9 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
441 0 $self->log("Warning: failed to push $image_ref: $@");
442 0 push @failed, $image_ref;
443 }
444 }
445 3 ▶ if (@failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@failed) 0 3 3
446 0 $self->log_fatal("Push failed for: " . join(', ', @failed));
447 }
448 }
449 }
450  
451 24 ▶sub _template_vars {
Subroutine _template_vars: called CC=10 SCAR=26.2
452 24 my ($self, $build_root, $version, $archive) = @_;
453 24 my $zilla = $self->zilla;
454  
455 24 my $git = $self->_git_info;
456  
457 my %vars = (
458 name => $zilla->name,
459 version => $version // $zilla->version // '0',
460 trial => ($zilla->is_trial ? '-TRIAL' : ''),
461 git_short_sha => $git->{short_sha} // '',
462 git_full_sha => $git->{full_sha} // '',
463 24 ▶ branch => $git->{branch} // '',
Branch: 1 branch
BranchTrueFalseTotal
$zilla->is_trial ? : 0 24 24
Condition58%10/17
($version // $zilla->version) // 0
l!l
240
$version // $zilla->version
l!l&&r!l&&!r
6180
$git->{"short_sha"} // ""
l!l
240
$git->{"full_sha"} // ""
l!l
240
$git->{"branch"} // ""
l!l
240
$build_root // ""
l!l
240
$zilla->root // ""
l!l
240
$archive // ""
l!l
618
MC/DC0%0/2
($version // $zilla->version) // 0
$version $zilla->version
MC/DC0%0/1
$git->{"short_sha"} // ""
$git->{"short_sha"}
MC/DC0%0/1
$git->{"full_sha"} // ""
$git->{"full_sha"}
MC/DC0%0/1
$git->{"branch"} // ""
$git->{"branch"}
MC/DC0%0/1
$build_root // ""
$build_root
MC/DC0%0/1
$zilla->root // ""
$zilla->root
MC/DC100%1/1
$archive // ""
$archive
Truth table66%2/3
A // B
A= $versionB= $zilla->version
ABresult
1X1
011
000
Truth table50%1/2
A
A= $git->{"short_sha"}
Aresult
11
00
Truth table50%1/2
A
A= $git->{"full_sha"}
Aresult
11
00
Truth table50%1/2
A
A= $git->{"branch"}
Aresult
11
00
Truth table50%1/2
A
A= $build_root
Aresult
11
00
Truth table50%1/2
A
A= $zilla->root
Aresult
11
00
Truth table100%2/2
A
A= $archive
Aresult
11
00
464 build_root => $build_root // '',
465 source_root => $zilla->root // '',
466 archive => $archive // '',
467 plugin_name => $self->plugin_name,
468 );
469  
470 24 return %vars;
471 }
472  
473 24 ▶sub _git_info {
Subroutine _git_info: called CC=6 SCAR=19.4
474 24 my ($self) = @_;
475 24 ▶ return $self->{_git_info} //= do {
Condition66%2/3
$self->{"_git_info"} //= do { my $root = $self->zilla->root; my $sha = _git_capture($root, "rev-parse", "HEAD"); my $branch = _git_capture($root, "rev-parse", "--abbrev-ref", "HEAD"); my $full = $sha =~ /^([a-f0-9]{40})$/ ? $1 : ""; my $br = $branch ne "" && $branch ne "HEAD" ? $branch : ""; +{"full_sha", $full, "short_sha", $full ? substr($full, 0, 7) : "", "branch", $br} }
l!l&&r!l&&!r
6180
MC/DC0%0/2
$self->{"_git_info"} //= do { my $root = $self->zilla->root; my $sha = _git_capture($root, "rev-parse", "HEAD"); my $branch = _git_capture($root, "rev-parse", "--abbrev-ref", "HEAD"); my $full = $sha =~ /^([a-f0-9]{40})$/ ? $1 : ""; my $br = $branch ne "" && $branch ne "HEAD" ? $branch : ""; +{"full_sha", $full, "short_sha", $full ? substr($full, 0, 7) : "", "branch", $br} }
$self->{"_git_info"} do { my $root = $self->zilla->root; my $sha = _git_capture($root, "rev-parse", "HEAD"); my $branch = _git_capture($root, "rev-parse", "--abbrev-ref", "HEAD"); my $full = $sha =~ /^([a-f0-9]{40})$/ ? $1 : ""; my $br = $branch ne "" && $branch ne "HEAD" ? $branch : ""; +{"full_sha", $full, "short_sha", $full ? substr($full, 0, 7) : "", "branch", $br} }
Truth table66%2/3
A //= B
A= $self->{"_git_info"}B= do { my $root = $self->zilla->root; my $sha = _git_capture($root, "rev-parse", "HEAD"); my $branch = _git_capture($root, "rev-parse", "--abbrev-ref", "HEAD"); my $full = $sha =~ /^([a-f0-9]{40})$/ ? $1 : ""; my $br = $branch ne "" && $branch ne "HEAD" ? $branch : ""; +{"full_sha", $full, "short_sha", $full ? substr($full, 0, 7) : "", "branch", $br} }
ABresult
1X1
011
000
476 18 my $root = $self->zilla->root;
477 18 my $sha = _git_capture($root, 'rev-parse', 'HEAD');
478 18 my $branch = _git_capture($root, 'rev-parse', '--abbrev-ref', 'HEAD');
479  
480 18 ▶ my $full = ($sha =~ /^([a-f0-9]{40})$/) ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$sha =~ /^([a-f0-9]{40})$/ ? : 0 18 18
481 18 ▶ my $br = ($branch ne '' && $branch ne 'HEAD') ? $branch : '';
Branch: 1 branch
BranchTrueFalseTotal
$branch ne '' && $branch ne 'HEAD' ? : 0 18 18
Condition33%1/3
$branch ne "" and $branch ne "HEAD"
!ll&&!rl&&r
1800
MC/DC0%0/2
$branch ne "" and $branch ne "HEAD"
$branch ne "" $branch ne "HEAD"
Truth table33%1/3
A and B
A= $branch ne ""B= $branch ne "HEAD"
ABresult
0X0
100
111
482  
483 {
484 18 ▶ full_sha => $full,
Branch: 1 branch
BranchTrueFalseTotal
$full ? : 0 18 18
485 short_sha => $full ? substr($full, 0, 7) : '',
486 branch => $br,
487 };
488 };
489 }
490  
491 41 ▶sub _git_capture {
Subroutine _git_capture: called CC=8 SCAR=23.9
492 41 my ($dir, @cmd) = @_;
493 # No implicit fork, so it also runs on Windows. The list form of a piped
494 # open is native from perl 5.22 on Win32 and everywhere on Unix; a Win32
495 # perl before 5.22 gets one quoted command string for cmd.exe instead.
496 # git's stderr goes to the null device for the time of the call.
497 41 ▶ open(my $olderr, '>&', \*STDERR) or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $olderr, ">&", \*STDERR or return "" 41 0 41
498 41 ▶ unless (open(STDERR, '>', File::Spec->devnull)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open STDERR, ">", "File::Spec"->devnull) 0 41 41
499 0 open(STDERR, '>&', $olderr);
500 0 return '';
501 }
502 41 my ($out, $status) = ('', 1);
503 41 my @open = ('git', '-C', $dir, @cmd);
504 41 ▶ if ($^O eq 'MSWin32' && $] < 5.022) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and $] < "5.022") 0 41 41
Condition33%1/3
$^O eq "MSWin32" and $] < "5.022"
!ll&&!rl&&r
4100
MC/DC0%0/2
$^O eq "MSWin32" and $] < "5.022"
$^O eq "MSWin32" $] < "5.022"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $] < "5.022"
ABresult
0X0
100
111
505 @open = (join ' ', map {
506 0 my $word = $_;
507 0 $word =~ s/(\\*)"/$1$1\\"/g;
508 0 $word =~ s/(\\+)\z/$1$1/;
509 0 qq{"$word"};
510 } @open);
511 }
512 41 ▶ if (open(my $fh, '-|', @open)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "-|", @open) 41 0 41
513 41 binmode $fh;
514 41 ▶ $out = do { local $/; <$fh> } // '';
Condition50%1/2
do { local $/; readline $fh } // ""
l!l
410
MC/DC0%0/1
do { local $/; readline $fh } // ""
do { local $/; readline $fh }
Truth table50%1/2
A
A= do { local $/; readline $fh }
Aresult
11
00
515 41 close $fh;
516 41 $status = $?;
517 }
518 41 open(STDERR, '>&', $olderr);
519 41 ▶ return '' if $status != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status != 0 39 2 41
520 2 $out =~ s/\r?\n\z//;
521 2 return $out;
522 }
523  
524 22 ▶sub _resolve_tags {
Subroutine _resolve_tags: called CC=1 SCAR=0.0
525 22 my ($self, $tags, %vars) = @_;
526 22 return map { $self->_image_ref($_, %vars) } @{$tags};
527 }
528  
529 71 ▶sub _image_ref {
Subroutine _image_ref: called CC=1 SCAR=0.0
530 71 my ($self, $tag, %vars) = @_;
531 71 my $expanded = $self->tag_template->expand($tag, %vars);
532 71 return $self->image . ':' . $expanded;
533 }
534  
535 18 ▶sub _resolve_labels {
Subroutine _resolve_labels: called CC=3 SCAR=14.2
536 18 my ($self, %vars) = @_;
537 18 my %labels;
538 18 for my $label_def (@{ $self->label }) {
539 0 ▶ if ($label_def =~ /^([^=]+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_def =~ /^([^=]+)=(.*)$/) 0 0 0
540 0 my ($key, $value) = ($1, $2);
541 0 $labels{$key} = $self->tag_template->expand($value, %vars);
542 }
543 }
544 18 return %labels;
545 }
546  
547 18 ▶sub _resolve_build_args {
Subroutine _resolve_build_args: called CC=3 SCAR=14.2
548 18 my ($self, %vars) = @_;
549 18 my %args;
550 18 for my $arg_def (@{ $self->build_arg }) {
551 0 ▶ if ($arg_def =~ /^([^=]+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_def =~ /^([^=]+)=(.*)$/) 0 0 0
552 0 my ($key, $value) = ($1, $2);
553 0 $args{$key} = $self->tag_template->expand($value, %vars);
554 }
555 }
556 18 return %args;
557 }
558  
559 18 ▶sub _log_build_result {
Subroutine _log_build_result: called CC=7 SCAR=23.0
560 18 my ($self, $result) = @_;
561 18 ▶ if ($result->image_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->image_id) 18 0 18
562 18 $self->log("Built image: " . $result->image_id);
563 }
564 18 ▶ if (@{ $result->tags }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$result->tags;}) 18 0 18
565 18 $self->log("Tagged: " . join(', ', @{ $result->tags }));
566 }
567 18 ▶ if (@{ $result->pushed }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$result->pushed;}) 0 18 18
568 0 $self->log("Pushed: " . join(', ', @{ $result->pushed }));
569 }
570 18 ▶ if ($result->digest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->digest) 0 18 18
571 0 $self->log("Digest: " . $result->digest);
572 }
573 18 ▶ if (@{ $result->warnings }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$result->warnings;}) 0 18 18
574 0 for my $warning (@{ $result->warnings }) {
575 0 $self->log("Warning: $warning");
576 }
577 }
578 }
579  
580 # Deprecated one-to-one spellings, old key => canonical key. These used to be
581 # declared as lazy attributes reading *from* the canonical one, which meant
582 # setting them in dist.ini changed nothing at all: 'push = 0' left
583 # release_push at 1 and the code reads release_push. They are funneled here instead, like
584 # build_tag/release_tag, so they actually take effect.
585 11 my %DEPRECATED_KEY = (
586 file => 'dockerfile',
587 push => 'release_push',
588 repository => 'image',
589 );
590  
591 40 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
592 40 my ($orig, $class, @args) = @_;
593 40 my $args = $class->$orig(@args);
594  
595 40 for my $old (sort keys %DEPRECATED_KEY) {
596 120 ▶ next unless exists $args->{$old};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$old} 116 4 120
597 4 my $new = $DEPRECATED_KEY{$old};
598 4 my $value = delete $args->{$old};
599  
600 4 ▶ if (exists $args->{$new}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$new}) 1 3 4
601 1 warn "[Docker::API] '".$old."' is deprecated and '".$new
602 ."' is set explicitly; ignoring '".$old."'.\n";
603 1 next;
604 }
605  
606 3 warn "[Docker::API] '".$old."' is deprecated; use '".$new."' instead.\n";
607 3 $args->{$new} = $value;
608 }
609  
610 # Keys that no longer mean anything -- there is no switch behind them.
611 # Drop them with a warning rather than letting an unknown key be silently
612 # ignored.
613 40 for my $removed (qw(build_load load phase release_load)) {
614 160 ▶ next unless exists $args->{$removed};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$removed} 156 4 160
615 4 delete $args->{$removed};
616 4 warn "[Docker::API] '".$removed."' is no longer supported and has no effect.\n";
617 }
618  
619 40 my @legacy = grep { exists $args->{$_} } qw(build_tag release_tag);
620 40 ▶ if (@legacy) {
Branch: 1 branch
BranchTrueFalseTotal
if (@legacy) 4 36 40
621 4 warn "[Docker::API] '" . join("' and '", @legacy)
622 . "' are deprecated; use 'tag' instead.\n"
623 . " They are merged into 'tag' for now and will be removed in a future release.\n";
624  
625 4 my @merged;
626 4 ▶ push @merged, @{ delete $args->{build_tag} // [] };
Condition100%2/2
delete $args->{"build_tag"} // []
l!l
31
MC/DC100%1/1
delete $args->{"build_tag"} // []
delete $args->{"build_tag"}
Truth table100%2/2
A
A= delete $args->{"build_tag"}
Aresult
11
00
627 4 ▶ push @merged, @{ delete $args->{release_tag} // [] };
Condition100%2/2
delete $args->{"release_tag"} // []
l!l
22
MC/DC100%1/1
delete $args->{"release_tag"} // []
delete $args->{"release_tag"}
Truth table100%2/2
A
A= delete $args->{"release_tag"}
Aresult
11
00
628  
629 4 ▶ if (exists $args->{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'tag'}) { } 1 3 4
630 1 warn "[Docker::API] 'tag' is set explicitly; ignoring deprecated build_tag/release_tag values.\n";
631 }
632 else {
633 3 my %seen;
634 3 $args->{tag} = [ grep { !$seen{$_}++ } @merged ];
635 }
636 }
637  
638 40 return $args;
639 11 };
640  
641 40 ▶sub mvp_multivalue_args { qw(tag build_tag release_tag build_arg label platform) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
642  
643 11 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
644 11 __PACKAGE__->meta->make_immutable;
645  
646 11 1;
647  
648 __END__
« API/Client.pm ↑ Summary API/TagTemplate.pm »