Dist-Zilla-Plugin-Docker-API

 view release on metacpan or  search on metacpan

lib/Dist/Zilla/Plugin/Docker/API.pm  view on Meta::CPAN


has _client => (
    is      => 'ro',
    isa     => 'Object',
    lazy    => 1,
    builder => '_build_client',
);

sub _build_tag_template {
    my ($self) = @_;
    return Dist::Zilla::Plugin::Docker::API::TagTemplate->new(
        zilla     => $self->zilla,
        plugin_name => $self->plugin_name,
    );
}

sub _build_client {
    my ($self) = @_;
    my $client_class = $self->client_class;
    unless (eval "require $client_class; 1") {
        $self->log_fatal("Cannot load client_class $client_class: $@");
    }
    return $client_class->new(
        logger => sub { $self->log(@_) },
        logger_fatal => sub { $self->log_fatal(@_) },
    );
}

sub tag_template { shift->_tag_template }
sub client { shift->_client }

sub file { shift->dockerfile }

# API::Docker croaks through Carp, so a reason arrives carrying one or more
# " at FILE line N." tails. Strip all of them (not just the last) and flatten
# to a single line -- log_fatal repeats whatever it is given, and a
# multi-line message gets repeated in full.
sub _flatten_error {
    my ($self, $error) = @_;

    $error = '' unless defined $error;
    $error =~ s/\s+at\s+\S+\s+line\s+\d+\.?//g;
    $error =~ s/\s+/ /g;
    $error =~ s/^\s+|\s+$//g;
    return $error;
}

# after_build builds an image unconditionally, so every dzil command that
# builds needs a reachable engine. Ask for one up front instead of letting
# Dist::Zilla gather, munge and write out a whole distribution first and only
# then die on a socket that was never there.
sub before_build {
    my ($self) = @_;

    if ($ENV{DZIL_DOCKER_API_SKIP}) {
        $self->log('DZIL_DOCKER_API_SKIP is set: skipping the image build '
            .'for this run - no engine contact, no image');
        return;
    }

    # The classic /build endpoint takes exactly one platform; a second value
    # would otherwise be dropped without a word. This is configuration, not
    # engine contact, so DZIL_DOCKER_API_SKIP_PRECHECK does not skip it.
    my @platforms = @{ $self->platform };
    if (@platforms > 1) {
        $self->log_fatal('only one platform is supported, got '
            .scalar(@platforms).': '.join(', ', @platforms)
            .' - the /build endpoint takes exactly one platform, and this '
            .'plugin is not built for multi-arch images');
    }

    return if $ENV{DZIL_DOCKER_API_SKIP_PRECHECK};

    my $info = eval { $self->client->engine_info };
    my $error = $@;

    if ($error) {
        $error = $self->_flatten_error($error);

        $self->log('Docker::API speaks the Docker Engine HTTP API over a '
            .'socket and never shells out to the docker binary, so any engine '
            .'serving that API will do.');
        $self->log('Point DOCKER_HOST at one. For rootless Podman: '
            .'systemctl --user enable --now podman.socket, then '
            .'DOCKER_HOST="unix://$XDG_RUNTIME_DIR/podman/podman.sock"');
        $self->log_fatal('cannot reach a container engine: '.$error
            .' (set DZIL_DOCKER_API_SKIP_PRECHECK=1 to skip this check)');
    }

    $self->log('Docker::API engine ready: '
        .($info->{engine} // 'unknown').' '
        .($info->{version} // '?')
        .' (API '.($info->{api_version} // '?').')');

    $self->_precheck_registry_auth
        if $ENV{DZIL_RELEASING} && $self->release_enabled && $self->release_push;
}

# A rejected or expired registry credential otherwise surfaces only when the
# push fails -- after Dist::Zilla gathered and wrote the distribution and
# after_build built and tagged the image, i.e. after the damage. This runs
# before any of it.
#
# The phase hook that runs early enough is before_build, and it can tell a
# release from a plain build: Dist::Zilla::Dist::Builder::release sets
# DZIL_RELEASING before it calls build_archive, so the variable is already
# there when this hook runs (measured against Dist::Zilla 6.037). A plain
# dzil build never sets it and so never needs registry credentials.
#
# Only the registry host of `image` decides which credential applies, and
# that part carries no template variables, so no expansion is needed here.
sub _precheck_registry_auth {
    my ($self) = @_;

    my $image_ref = $self->image;

    my $status = eval { $self->client->verify_auth_for_image_ref($image_ref) };
    my $error = $@;

    # Deliberately not worded as "rejected": the engine answers a bad
    # credential and an unreachable registry with the same failure -- Podman
    # returns 500 for both, so the status cannot tell them apart. The engine's
    # own text, which does, is carried through verbatim.
    if ($error) {
        $self->log_fatal('the registry credential check for '.$image_ref
            .' failed: '.$self->_flatten_error($error)
            .' (set DZIL_DOCKER_API_SKIP_PRECHECK=1 to skip this check)');
    }

lib/Dist/Zilla/Plugin/Docker/API.pm  view on Meta::CPAN

C<docker.io>. A C<credsStore> or C<credHelpers> entry that delegates the
secret to an external helper is not supported -- nothing in this plugin
reads either key, so such a registry resolves to no credential and any
request against it goes out anonymous rather than failing.

A plain C<docker login> or C<podman login> is now picked up with no extra
configuration: Podman's own default auth file is candidate three above. If
C<podman login --authfile PATH> was used to write somewhere else, set
C<REGISTRY_AUTH_FILE=PATH> in the environment C<dzil> runs in so candidate
one finds it.

Finding no credential for a registry is never an error by itself in this
plugin -- both C<fail_if_tag_exists> and the release push treat it as "go
anonymous," and only a credential that C<auth_for_image_ref> did find and
the engine then rejects (or an unreachable registry -- see above) is fatal.

=head1 CONFIGURATION

=over 4

=item C<image> - Full image repository (required). Example: C<ghcr.io/user/my-app>

=item C<tag> - Tags applied to the image (can be repeated, template-enabled).
Default: C<latest>, C<%V>, and C<%v> (e.g. C<latest>, C<0>, C<0.402>).
Applied identically in both build and release. Note: setting C<tag>
explicitly B<replaces> the default list, it does not append to it.

=item C<dockerfile> - Dockerfile name (default: C<Dockerfile>)

=item C<release_push> - Push to registry during release (default: true)

=item C<build_verbose> - When false (default), the build log only echoes
Dockerfile step headers — the legacy builder format C<Step N/M : ...>,
Podman's classic builder C<STEP N/M: ...> and BuildKit's C<#N [N/M] ...> —
instead of the full per-command output.
Set to true to see every line the daemon streams back. Errors are always
surfaced regardless of this flag.

=item C<fail_if_tag_exists> - Abort the release if any tag already exists on
the remote registry (default: false). The check runs before anything is
tagged or pushed, and only when C<release_push> is also true. It asks the
I<registry>, not the local daemon, through C<API::Docker>'s
C<< distribution->exists >> (C<GET /distribution/{name}/json>), using the
credential resolved for C<image> (see L</"Registry credentials">), or an
anonymous request when none applies.

An engine that has no C</distribution> route -- rootless Podman among them --
cannot answer the question at all, and that is treated as a release-stopping
failure rather than as "the tag is free": the release aborts with the
engine's own error and a reminder that C<fail_if_tag_exists = 0> releases
without the check.

=item C<skip_latest_on_trial> - Skip C<latest> tag for trial releases

=item C<build_arg> - Build arguments (can be repeated, template-enabled)

=item C<label> - OCI labels (can be repeated, template-enabled)

=item C<platform> - Target platform, exactly one (e.g. C<linux/arm64>). The key
is repeatable only so that a second value is refused with a clear error: the
/build endpoint takes one platform, and this plugin is not built for multi-arch
images.

=back

=head1 DEPRECATED

The following names are still accepted but emit a warning and will be removed
in a future release. Each is funneled into its canonical attribute by
C<BUILDARGS>; where both spellings are given, the canonical one wins and the
collision is reported.

=over 4

=item C<file> - Use C<dockerfile> instead.

Until 0.104 this was the only spelling that worked: the attribute carried
C<< init_arg => 'file' >> while the documentation described C<dockerfile>, so
C<dockerfile = ...> in a F<dist.ini> was silently ignored and the default
F<Dockerfile> used instead. C<dockerfile> is now the canonical key.

=item C<build_tag>, C<release_tag>

Replaced by the single C<tag> attribute. When either is given, the values are
merged (build_tag first, release_tag second) into C<tag> and a deprecation
warning is emitted. If C<tag> is also set explicitly, it wins and the legacy
values are ignored.

=item C<repository> - Use C<image> instead.

=item C<push> - Use C<release_push> instead.

=back

Note that C<repository> and C<push> did B<not> work as aliases before
0.104: they were declared as readers taking their value I<from> the canonical
attribute, so setting one in a F<dist.ini> had no effect whatsoever. They are
funneled properly now.

=head1 REMOVED

These keys are no longer supported. Each is warned about and discarded; none
has a replacement.

=over 4

=item C<phase> - The build and release phases are implicit.

=item C<build_load>, C<release_load>, C<load> - The image is built through the
engine API, where it lands in the engine's image store regardless, so there is
nothing left to switch.

=back

=head1 SEE ALSO

L<Dist::Zilla::Plugin::Docker::API::TagTemplate>,
L<Dist::Zilla::Plugin::Docker::API::Client>,
L<Dist::Zilla::Plugin::Docker::API::Result>

=head1 SUPPORT



( run in 1.368 second using v1.01-cache-2.11-cpan-eb134014566 )