private void applyJibInstruction()

in plugins/docker/component-image/src/main/java/co/elastic/gradle/dockercomponent/JibActions.java [312:374]


    private void applyJibInstruction(
            JibContainerBuilder jibBuilder,
            ContainerImageBuildInstruction instruction,
            Path contextRoot
    ) {
        if (instruction instanceof Copy) {
            Copy copyInstruction = (Copy) instruction;
            // We can't add directly to / causing a NPE in Jib
            // We need to walk through the contexts to add them separately => https://github.com/GoogleContainerTools/jib/issues/2195
            Path contextFolder = contextRoot.resolve(copyInstruction.getLayer());
            if (!Files.exists(contextFolder)) {
                throw new RuntimeException("Input layer " + contextFolder + " does not exit. " +
                                           "Did the copy spec actually copy any files?"
                );
            }
            if (!Files.isDirectory(contextFolder)) {
                throw new RuntimeException("Expected " + contextFolder + " to be a directory.");
            }
            try (Stream<Path> elements = Files.list(contextFolder)) {
                elements.forEach(file -> {
                            try {
                                jibBuilder.addFileEntriesLayer(
                                        FileEntriesLayer.builder()
                                                .addEntryRecursive(
                                                        file,
                                                        AbsoluteUnixPath.get("/" + file.getFileName()),
                                                        JibActions::getJibFilePermission,
                                                        FileEntriesLayer.DEFAULT_MODIFICATION_TIME_PROVIDER,
                                                        Optional.ofNullable(copyInstruction.getOwner()).isPresent() ?
                                                                (sourcePath, destinationPath) -> copyInstruction.getOwner() :
                                                                FileEntriesLayer.DEFAULT_OWNERSHIP_PROVIDER
                                                ).build());
                            } catch (IOException e) {
                                throw new UncheckedIOException(e);
                            }
                        }
                );
            } catch (IOException e) {
                throw new UncheckedIOException("Error configuring " + copyInstruction.getLayer() + " for Jib docker config", e);
            }
        } else if (instruction instanceof Entrypoint entrypoint) {
            jibBuilder.setEntrypoint(entrypoint.getValue());
        } else if (instruction instanceof Cmd cmd) {
            jibBuilder.setProgramArguments(cmd.getValue());
        } else if (instruction instanceof Env envInstruction) {
            jibBuilder.addEnvironmentVariable(envInstruction.getKey(), envInstruction.getValue());
        } else if (instruction instanceof Expose expose) {
            switch (expose.getType()) {
                case TCP -> jibBuilder.addExposedPort(Port.tcp(expose.getPort()));
                case UDP -> jibBuilder.addExposedPort(Port.udp(expose.getPort()));
            }
        } else if (instruction instanceof Label label) {
            jibBuilder.addLabel(label.getKey(), label.getValue());
        } else if (instruction instanceof ChangingLabel changingLabel) {
            jibBuilder.addLabel(changingLabel.getKey(), changingLabel.value());
        } else if (instruction instanceof Maintainer maintainer) {
            jibBuilder.addLabel("maintainer", maintainer.getName() + "<" + maintainer.getEmail() + ">");
        } else if (instruction instanceof Workdir) {
            jibBuilder.setWorkingDirectory(AbsoluteUnixPath.get(((Workdir) instruction).getFolder()));
        } else {
            throw new GradleException("Instruction " + instruction + "is not a valid Jib instruction");
        }
    }