Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builds extra layer with contents of src/main/jib. #562

Merged
merged 6 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testBuild_simple() throws IOException, InterruptedException {
}

Assert.assertEquals(
"Hello, world. An argument.\n",
"Hello, world. An argument.\nfoo\ncat\n",
buildAndRun(simpleTestProject, "gcr.io/jib-integration-testing/simpleimage:gradle"));
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public void testDockerDaemon_empty() throws IOException, InterruptedException {
@Test
public void testDockerDaemon_simple() throws IOException, InterruptedException {
Assert.assertEquals(
"Hello, world. An argument.\n",
"Hello, world. An argument.\nfoo\ncat\n",
buildToDockerDaemonAndRun(
simpleTestProject, "gcr.io/jib-integration-testing/simpleimage:gradle"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ public static void main(String[] args) throws IOException, URISyntaxException {
String world = new String(Files.readAllBytes(worldFile), StandardCharsets.UTF_8);

System.out.println(greeting + ", " + world + ". " + (args.length > 0 ? args[0] : ""));

// Prints the contents of the extra files.
if (Files.exists(Paths.get("/foo"))) {
System.out.println(new String(Files.readAllBytes(Paths.get("/foo")), StandardCharsets.UTF_8));
System.out.println(
new String(Files.readAllBytes(Paths.get("/bar/cat")), StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.cloud.tools.jib.builder.BuildConfiguration;
import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException;
import com.google.cloud.tools.jib.configuration.CacheConfiguration;
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
import com.google.cloud.tools.jib.docker.DockerClient;
import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException;
import com.google.cloud.tools.jib.frontend.BuildStepsRunner;
Expand All @@ -31,6 +32,11 @@
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -72,7 +78,7 @@ public void setTargetImage(String targetImage) {
}

@TaskAction
public void buildDocker() throws InvalidImageReferenceException {
public void buildDocker() throws InvalidImageReferenceException, IOException {
if (!new DockerClient().isDockerInstalled()) {
throw new GradleException(HELPFUL_SUGGESTIONS.forDockerNotInstalled());
}
Expand Down Expand Up @@ -107,6 +113,15 @@ public void buildDocker() throws InvalidImageReferenceException {
.setJvmFlags(jibExtension.getJvmFlags())
.setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts()))
.setAllowHttp(jibExtension.getAllowInsecureRegistries());
if (Files.exists(jibExtension.getExtraDirectory())) {
try (Stream<Path> extraFilesLayerDirectoryFiles =
Files.list(jibExtension.getExtraDirectory())) {
buildConfigurationBuilder.setExtraFilesLayerConfiguration(
LayerConfiguration.builder()
.addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/")
.build());
}
}
CacheConfiguration applicationLayersCacheConfiguration =
CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory());
buildConfigurationBuilder.setApplicationLayersCacheConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.cloud.tools.jib.builder.BuildConfiguration;
import com.google.cloud.tools.jib.cache.CacheDirectoryCreationException;
import com.google.cloud.tools.jib.configuration.CacheConfiguration;
import com.google.cloud.tools.jib.configuration.LayerConfiguration;
import com.google.cloud.tools.jib.frontend.BuildStepsExecutionException;
import com.google.cloud.tools.jib.frontend.BuildStepsRunner;
import com.google.cloud.tools.jib.frontend.ExposedPortsParser;
Expand All @@ -30,6 +31,11 @@
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand Down Expand Up @@ -71,7 +77,7 @@ public void setTargetImage(String targetImage) {
}

@TaskAction
public void buildImage() throws InvalidImageReferenceException {
public void buildImage() throws InvalidImageReferenceException, IOException {
// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
Expand Down Expand Up @@ -114,6 +120,15 @@ public void buildImage() throws InvalidImageReferenceException {
.setExposedPorts(ExposedPortsParser.parse(jibExtension.getExposedPorts()))
.setTargetFormat(jibExtension.getFormat())
.setAllowHttp(jibExtension.getAllowInsecureRegistries());
if (Files.exists(jibExtension.getExtraDirectory())) {
try (Stream<Path> extraFilesLayerDirectoryFiles =
Files.list(jibExtension.getExtraDirectory())) {
buildConfigurationBuilder.setExtraFilesLayerConfiguration(
LayerConfiguration.builder()
.addEntry(extraFilesLayerDirectoryFiles.collect(Collectors.toList()), "/")
.build());
}
}
CacheConfiguration applicationLayersCacheConfiguration =
CacheConfiguration.forPath(gradleProjectProperties.getCacheDirectory());
buildConfigurationBuilder.setApplicationLayersCacheConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void generateDockerContext() {
// here.
ExposedPortsParser.parse(jibExtension.getExposedPorts());

// TODO: Add support for extra files layer.
new DockerContextGenerator(gradleProjectProperties.getSourceFilesConfiguration())
.setBaseImage(jibExtension.getBaseImage())
.setJvmFlags(jibExtension.getJvmFlags())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -66,11 +67,16 @@ public class JibExtension {
private static final boolean DEFAULT_USE_ONLY_PROJECT_CACHE = false;
private static final boolean DEFAULT_ALLOW_INSECURE_REGISTIRIES = false;

private static Path resolveDefaultExtraDirectory(Path projectDirectory) {
return projectDirectory.resolve("src").resolve("main").resolve("jib");
}

private final ImageConfiguration from;
private final ImageConfiguration to;
private final ContainerParameters container;
private final Property<Boolean> useOnlyProjectCache;
private final Property<Boolean> allowInsecureRegistries;
private final Property<Path> extraDirectory;

// TODO: Deprecated parameters; remove these 4
private final ListProperty<String> jvmFlags;
Expand All @@ -92,13 +98,15 @@ public JibExtension(Project project) {

useOnlyProjectCache = objectFactory.property(Boolean.class);
allowInsecureRegistries = objectFactory.property(Boolean.class);
extraDirectory = objectFactory.property(Path.class);

// Sets defaults.
from.setImage(DEFAULT_FROM_IMAGE);
jvmFlags.set(Collections.emptyList());
args.set(Collections.emptyList());
useOnlyProjectCache.set(DEFAULT_USE_ONLY_PROJECT_CACHE);
allowInsecureRegistries.set(DEFAULT_ALLOW_INSECURE_REGISTIRIES);
extraDirectory.set(resolveDefaultExtraDirectory(project.getProjectDir().toPath()));
}

/**
Expand Down Expand Up @@ -176,6 +184,10 @@ public void setAllowInsecureRegistries(boolean allowInsecureRegistries) {
this.allowInsecureRegistries.set(allowInsecureRegistries);
}

public void setExtraDirectory(Path extraDirectory) {
this.extraDirectory.set(extraDirectory);
}

@Internal
String getBaseImage() {
return Preconditions.checkNotNull(from.getImage());
Expand Down Expand Up @@ -251,4 +263,10 @@ boolean getUseOnlyProjectCache() {
boolean getAllowInsecureRegistries() {
return allowInsecureRegistries.get();
}

@Input
@Optional
Path getExtraDirectory() {
return extraDirectory.get();
}
}