Skip to content

Commit 5d7588c

Browse files
authored
Add default target image name parameter for build to Docker daemon (GoogleContainerTools#375)
1 parent d45c9da commit 5d7588c

20 files changed

Lines changed: 267 additions & 14 deletions

File tree

‎jib-gradle-plugin/src/integration-test/java/com/google/cloud/tools/jib/gradle/JibPluginIntegrationTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class JibPluginIntegrationTest {
3535

3636
@ClassRule public static final TestProject simpleTestProject = new TestProject("simple");
3737

38+
@ClassRule
39+
public static final TestProject defaultTargetTestProject = new TestProject("default-target");
40+
3841
private static String buildAndRun(TestProject testProject, String imageReference)
3942
throws IOException, InterruptedException {
4043
BuildResult buildResult = testProject.build("clean", JibPlugin.BUILD_IMAGE_TASK_NAME);
@@ -98,6 +101,22 @@ public void testBuild_simple() throws IOException, InterruptedException {
98101
buildAndRun(simpleTestProject, "gcr.io/jib-integration-testing/simpleimage:gradle"));
99102
}
100103

104+
@Test
105+
public void testBuild_defaultTarget() {
106+
// Test error when 'to' is missing
107+
try {
108+
defaultTargetTestProject.build("clean", JibPlugin.BUILD_IMAGE_TASK_NAME, "-x=classes");
109+
Assert.fail();
110+
} catch (UnexpectedBuildFailure ex) {
111+
Assert.assertThat(
112+
ex.getMessage(),
113+
CoreMatchers.containsString(
114+
"Missing target image parameter. Add a 'jib.to.image' configuration parameter to "
115+
+ "your build.gradle or set the parameter via commandline (e.g. 'gradle jib "
116+
+ "--image <your image name>')."));
117+
}
118+
}
119+
101120
@Test
102121
public void testDockerDaemon_empty() throws IOException, InterruptedException {
103122
Assert.assertEquals(
@@ -114,6 +133,14 @@ public void testDockerDaemon_simple() throws IOException, InterruptedException {
114133
simpleTestProject, "gcr.io/jib-integration-testing/simpleimage:gradle"));
115134
}
116135

136+
@Test
137+
public void testDockerDaemon_defaultTarget() throws IOException, InterruptedException {
138+
Assert.assertEquals(
139+
"Hello, world. An argument.\n",
140+
buildToDockerDaemonAndRun(
141+
defaultTargetTestProject, "default-target-name:default-target-version"));
142+
}
143+
117144
@Test
118145
public void testDockerContext() throws IOException, InterruptedException {
119146
BuildResult buildResult =
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
plugins {
2+
id 'java'
3+
id 'com.google.cloud.tools.jib'
4+
}
5+
6+
sourceCompatibility = 1.8
7+
targetCompatibility = 1.8
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
compile files('libs/dependency-1.0.0.jar')
15+
}
16+
17+
jib {
18+
args = ['An argument.']
19+
20+
// Does not have tests use user-level cache for base image layers.
21+
useOnlyProjectCache = true
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = default-target-version
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'default-target-name'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.test;
18+
19+
import dependency.Greeting;
20+
import java.io.IOException;
21+
import java.net.URISyntaxException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
27+
/** Example class that uses a dependency and a resource file. */
28+
public class HelloWorld {
29+
30+
public static void main(String[] args) throws IOException, URISyntaxException {
31+
// 'Greeting' comes from the dependency artfiact.
32+
String greeting = Greeting.getGreeting();
33+
34+
// Gets the contents of the resource file 'world'.
35+
ClassLoader classLoader = HelloWorld.class.getClassLoader();
36+
Path worldFile = Paths.get(classLoader.getResource("world").toURI());
37+
String world = new String(Files.readAllBytes(worldFile), StandardCharsets.UTF_8);
38+
39+
System.out.println(greeting + ", " + world + ". " + (args.length > 0 ? args[0] : ""));
40+
}
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world

‎jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildDockerTask.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.cloud.tools.jib.registry.RegistryClient;
2929
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
3030
import com.google.common.base.Preconditions;
31+
import com.google.common.base.Strings;
3132
import javax.annotation.Nullable;
3233
import org.gradle.api.DefaultTask;
3334
import org.gradle.api.GradleException;
@@ -56,7 +57,7 @@ public JibExtension getJib() {
5657
return jibExtension;
5758
}
5859

59-
/** The target image can be overriden with the {@code --image} command line option. */
60+
/** The target image can be overridden with the {@code --image} command line option. */
6061
@Option(option = "image", description = "The image reference for the target image")
6162
public void setTargetImage(String targetImage) {
6263
Preconditions.checkNotNull(jibExtension).getTo().setImage(targetImage);
@@ -80,10 +81,17 @@ public void buildDocker() throws InvalidImageReferenceException {
8081
GradleProjectProperties gradleProjectProperties =
8182
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
8283
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
84+
85+
// TODO: Validate that project name and version are valid repository/tag
86+
ImageReference targetImage =
87+
Strings.isNullOrEmpty(jibExtension.getTargetImage())
88+
? ImageReference.of(null, getProject().getName(), getProject().getVersion().toString())
89+
: ImageReference.parse(jibExtension.getTargetImage());
90+
8391
BuildConfiguration buildConfiguration =
8492
BuildConfiguration.builder(gradleBuildLogger)
8593
.setBaseImage(ImageReference.parse(jibExtension.getBaseImage()))
86-
.setTargetImage(ImageReference.parse(jibExtension.getTargetImage()))
94+
.setTargetImage(targetImage)
8795
.setBaseImageCredentialHelperName(jibExtension.getFrom().getCredHelper())
8896
.setKnownBaseRegistryCredentials(knownBaseRegistryCredentials)
8997
.setMainClass(mainClass)

‎jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/BuildImageTask.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.cloud.tools.jib.registry.RegistryClient;
2828
import com.google.cloud.tools.jib.registry.credentials.RegistryCredentials;
2929
import com.google.common.base.Preconditions;
30+
import com.google.common.base.Strings;
3031
import javax.annotation.Nullable;
3132
import org.gradle.api.DefaultTask;
3233
import org.gradle.api.GradleException;
@@ -55,7 +56,7 @@ public JibExtension getJib() {
5556
return jibExtension;
5657
}
5758

58-
/** The target image can be overriden with the {@code --image} command line option. */
59+
/** The target image can be overridden with the {@code --image} command line option. */
5960
@Option(option = "image", description = "The image reference for the target image")
6061
public void setTargetImage(String targetImage) {
6162
Preconditions.checkNotNull(jibExtension).getTo().setImage(targetImage);
@@ -66,6 +67,13 @@ public void buildImage() throws InvalidImageReferenceException {
6667
// Asserts required @Input parameters are not null.
6768
Preconditions.checkNotNull(jibExtension);
6869

70+
if (Strings.isNullOrEmpty(jibExtension.getTargetImage())) {
71+
throw new GradleException(
72+
"Missing target image parameter. Add a 'jib.to.image' configuration parameter to your "
73+
+ "build.gradle or set the parameter via commandline (e.g. 'gradle jib --image "
74+
+ "<your image name>').");
75+
}
76+
6977
RegistryCredentials knownBaseRegistryCredentials = null;
7078
RegistryCredentials knownTargetRegistryCredentials = null;
7179
Authorization fromAuthorization = jibExtension.getFrom().getImageAuthorization();

‎jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ImageConfiguration.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public ImageConfiguration(ObjectFactory objectFactory) {
4747

4848
@Input
4949
@Nullable
50+
@Optional
5051
public String getImage() {
5152
return image;
5253
}

0 commit comments

Comments
 (0)