Search in sources :

Example 1 with Buildpack

use of dev.snowdrop.buildpack.Buildpack in project quarkus by quarkusio.

the class BuildpackProcessor method runBuildpackBuild.

private String runBuildpackBuild(BuildpackConfig buildpackConfig, ContainerImageInfoBuildItem containerImage, ContainerImageConfig containerImageConfig, Optional<ContainerImagePushRequestBuildItem> pushRequest, OutputTargetBuildItem outputTarget, boolean isNativeBuild) {
    Map<ProjectDirs, Path> dirs = getPaths(outputTarget);
    log.debug("Using target dir of " + dirs.get(ProjectDirs.TARGET));
    log.debug("Using source root of " + dirs.get(ProjectDirs.SOURCE));
    log.debug("Using project root of " + dirs.get(ProjectDirs.ROOT));
    String targetImageName = containerImage.getImage().toString();
    log.debug("Using Destination image of " + targetImageName);
    Map<String, String> envMap = new HashMap<>(buildpackConfig.builderEnv);
    if (!envMap.isEmpty()) {
        log.info("Using builder environment of " + envMap);
    }
    // Let's explicitly disable build and push during the build to avoid inception style builds
    envMap.put(QUARKUS_CONTAINER_IMAGE_BUILD, "false");
    envMap.put(QUARKUS_CONTAINER_IMAGE_PUSH, "false");
    log.info("Initiating Buildpack build");
    Buildpack buildpack = Buildpack.builder().addNewFileContent(dirs.get(ProjectDirs.ROOT).toFile()).withFinalImage(targetImageName).withEnvironment(envMap).withLogLevel(buildpackConfig.logLevel).withPullTimeoutSeconds(buildpackConfig.pullTimeoutSeconds).withLogger(new BuildpackLogger()).accept(BuildpackBuilder.class, b -> {
        if (isNativeBuild) {
            buildpackConfig.nativeBuilderImage.ifPresent(i -> b.withBuilderImage(i));
        } else {
            buildpackConfig.jvmBuilderImage.ifPresent(i -> b.withBuilderImage(i));
        }
        if (buildpackConfig.runImage.isPresent()) {
            log.info("Using Run image of " + buildpackConfig.runImage.get());
            b.withRunImage(buildpackConfig.runImage.get());
        }
        if (buildpackConfig.dockerHost.isPresent()) {
            log.info("Using DockerHost of " + buildpackConfig.dockerHost.get());
            b.withDockerHost(buildpackConfig.dockerHost.get());
        }
    }).build();
    if (buildpack.getExitCode() != 0) {
        throw new IllegalStateException("Buildpack build failed");
    }
    log.info("Buildpack build complete");
    if (containerImageConfig.isPushExplicitlyEnabled() || pushRequest.isPresent()) {
        if (!containerImageConfig.registry.isPresent()) {
            log.info("No container image registry was set, so 'docker.io' will be used");
        }
        AuthConfig authConfig = new AuthConfig();
        authConfig.withRegistryAddress(containerImageConfig.registry.orElse("docker.io"));
        containerImageConfig.username.ifPresent(u -> authConfig.withUsername(u));
        containerImageConfig.password.ifPresent(p -> authConfig.withPassword(p));
        log.info("Pushing image to " + authConfig.getRegistryAddress());
        Stream.concat(Stream.of(containerImage.getImage()), containerImage.getAdditionalImageTags().stream()).forEach(i -> {
            ResultCallback.Adapter<PushResponseItem> callback = buildpack.getDockerClient().pushImageCmd(i).start();
            try {
                callback.awaitCompletion();
                log.info("Push complete");
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        });
    }
    return targetImageName;
}
Also used : Path(java.nio.file.Path) Logger(org.jboss.logging.Logger) ContainerImageBuilderBuildItem(io.quarkus.container.spi.ContainerImageBuilderBuildItem) HashMap(java.util.HashMap) CurateOutcomeBuildItem(io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem) BuildpackBuilder(dev.snowdrop.buildpack.BuildpackBuilder) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) Buildpack(dev.snowdrop.buildpack.Buildpack) BuildStep(io.quarkus.deployment.annotations.BuildStep) Map(java.util.Map) Path(java.nio.file.Path) IsNormalNotRemoteDev(io.quarkus.deployment.IsNormalNotRemoteDev) ContainerImageLabelBuildItem(io.quarkus.container.spi.ContainerImageLabelBuildItem) AuthConfig(com.github.dockerjava.api.model.AuthConfig) ContainerImageBuildRequestBuildItem(io.quarkus.container.spi.ContainerImageBuildRequestBuildItem) NativeImageBuildItem(io.quarkus.deployment.pkg.builditem.NativeImageBuildItem) ContainerImagePushRequestBuildItem(io.quarkus.container.spi.ContainerImagePushRequestBuildItem) JarBuildItem(io.quarkus.deployment.pkg.builditem.JarBuildItem) PathsUtil.findMainSourcesRoot(io.quarkus.container.util.PathsUtil.findMainSourcesRoot) ContainerImageConfig(io.quarkus.container.image.deployment.ContainerImageConfig) ArtifactResultBuildItem(io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem) NativeBuild(io.quarkus.deployment.pkg.steps.NativeBuild) PackageConfig(io.quarkus.deployment.pkg.PackageConfig) ContainerImageInfoBuildItem(io.quarkus.container.spi.ContainerImageInfoBuildItem) List(java.util.List) Stream(java.util.stream.Stream) OutputTargetBuildItem(io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem) Optional(java.util.Optional) ResultCallback(com.github.dockerjava.api.async.ResultCallback) PushResponseItem(com.github.dockerjava.api.model.PushResponseItem) NativeBinaryUtil(io.quarkus.container.image.deployment.util.NativeBinaryUtil) AvailableContainerImageExtensionBuildItem(io.quarkus.container.spi.AvailableContainerImageExtensionBuildItem) AppCDSResultBuildItem(io.quarkus.deployment.pkg.builditem.AppCDSResultBuildItem) Collections(java.util.Collections) MainClassBuildItem(io.quarkus.deployment.builditem.MainClassBuildItem) ResultCallback(com.github.dockerjava.api.async.ResultCallback) HashMap(java.util.HashMap) PushResponseItem(com.github.dockerjava.api.model.PushResponseItem) AuthConfig(com.github.dockerjava.api.model.AuthConfig) BuildpackBuilder(dev.snowdrop.buildpack.BuildpackBuilder) Buildpack(dev.snowdrop.buildpack.Buildpack)

Aggregations

ResultCallback (com.github.dockerjava.api.async.ResultCallback)1 AuthConfig (com.github.dockerjava.api.model.AuthConfig)1 PushResponseItem (com.github.dockerjava.api.model.PushResponseItem)1 Buildpack (dev.snowdrop.buildpack.Buildpack)1 BuildpackBuilder (dev.snowdrop.buildpack.BuildpackBuilder)1 ContainerImageConfig (io.quarkus.container.image.deployment.ContainerImageConfig)1 NativeBinaryUtil (io.quarkus.container.image.deployment.util.NativeBinaryUtil)1 AvailableContainerImageExtensionBuildItem (io.quarkus.container.spi.AvailableContainerImageExtensionBuildItem)1 ContainerImageBuildRequestBuildItem (io.quarkus.container.spi.ContainerImageBuildRequestBuildItem)1 ContainerImageBuilderBuildItem (io.quarkus.container.spi.ContainerImageBuilderBuildItem)1 ContainerImageInfoBuildItem (io.quarkus.container.spi.ContainerImageInfoBuildItem)1 ContainerImageLabelBuildItem (io.quarkus.container.spi.ContainerImageLabelBuildItem)1 ContainerImagePushRequestBuildItem (io.quarkus.container.spi.ContainerImagePushRequestBuildItem)1 PathsUtil.findMainSourcesRoot (io.quarkus.container.util.PathsUtil.findMainSourcesRoot)1 IsNormalNotRemoteDev (io.quarkus.deployment.IsNormalNotRemoteDev)1 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 MainClassBuildItem (io.quarkus.deployment.builditem.MainClassBuildItem)1 PackageConfig (io.quarkus.deployment.pkg.PackageConfig)1 AppCDSResultBuildItem (io.quarkus.deployment.pkg.builditem.AppCDSResultBuildItem)1