-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add plugin configuration for environment variables #930
Changes from 2 commits
741a6e6
40e2935
e7b522e
20d3fbf
fef041c
415a53f
ee9a99e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,18 +104,7 @@ public void testSteps_forBuildToDockerRegistry() | |
|
||
String imageReference = "localhost:5000/testimage:testtag"; | ||
localRegistry.pull(imageReference); | ||
Assert.assertThat( | ||
new Command("docker", "inspect", imageReference).run(), | ||
CoreMatchers.containsString( | ||
" \"ExposedPorts\": {\n" | ||
+ " \"1000/tcp\": {},\n" | ||
+ " \"2000/tcp\": {},\n" | ||
+ " \"2001/tcp\": {},\n" | ||
+ " \"2002/tcp\": {},\n" | ||
+ " \"3000/udp\": {}")); | ||
String history = new Command("docker", "history", imageReference).run(); | ||
Assert.assertThat(history, CoreMatchers.containsString("jib-integration-test")); | ||
Assert.assertThat(history, CoreMatchers.containsString("bazel build ...")); | ||
assertDockerInspect(imageReference); | ||
Assert.assertEquals( | ||
"Hello, world. An argument.\n", new Command("docker", "run", imageReference).run()); | ||
} | ||
|
@@ -152,26 +141,7 @@ public void testSteps_forBuildToDockerDaemon() | |
new Caches.Initializer(cacheDirectory, false, cacheDirectory, false)) | ||
.run(); | ||
|
||
String dockerContainerConfig = new Command("docker", "inspect", imageReference).run(); | ||
Assert.assertThat( | ||
dockerContainerConfig, | ||
CoreMatchers.containsString( | ||
" \"ExposedPorts\": {\n" | ||
+ " \"1000/tcp\": {},\n" | ||
+ " \"2000/tcp\": {},\n" | ||
+ " \"2001/tcp\": {},\n" | ||
+ " \"2002/tcp\": {},\n" | ||
+ " \"3000/udp\": {}")); | ||
Assert.assertThat( | ||
dockerContainerConfig, | ||
CoreMatchers.containsString( | ||
" \"Labels\": {\n" | ||
+ " \"key1\": \"value1\",\n" | ||
+ " \"key2\": \"value2\"\n" | ||
+ " }")); | ||
String history = new Command("docker", "history", imageReference).run(); | ||
Assert.assertThat(history, CoreMatchers.containsString("jib-integration-test")); | ||
Assert.assertThat(history, CoreMatchers.containsString("bazel build ...")); | ||
assertDockerInspect(imageReference); | ||
Assert.assertEquals( | ||
"Hello, world. An argument.\n", new Command("docker", "run", imageReference).run()); | ||
} | ||
|
@@ -213,6 +183,7 @@ private BuildConfiguration getBuildConfiguration( | |
JavaEntrypointConstructor.makeDefaultEntrypoint( | ||
Collections.emptyList(), "HelloWorld")) | ||
.setProgramArguments(Collections.singletonList("An argument.")) | ||
.setEnvironment(ImmutableMap.of("var1", "value1", "var2", "value2")) | ||
.setExposedPorts( | ||
ExposedPortsParser.parse(Arrays.asList("1000", "2000-2002/tcp", "3000/udp"))) | ||
.setLabels(ImmutableMap.of("key1", "value1", "key2", "value2")) | ||
|
@@ -226,4 +197,31 @@ private BuildConfiguration getBuildConfiguration( | |
.setCreatedBy("jib-integration-test") | ||
.build(); | ||
} | ||
|
||
private void assertDockerInspect(String imageReference) throws IOException, InterruptedException { | ||
String dockerContainerConfig = new Command("docker", "inspect", imageReference).run(); | ||
Assert.assertThat( | ||
dockerContainerConfig, | ||
CoreMatchers.containsString( | ||
" \"ExposedPorts\": {\n" | ||
+ " \"1000/tcp\": {},\n" | ||
+ " \"2000/tcp\": {},\n" | ||
+ " \"2001/tcp\": {},\n" | ||
+ " \"2002/tcp\": {},\n" | ||
+ " \"3000/udp\": {}")); | ||
Assert.assertThat( | ||
dockerContainerConfig, | ||
CoreMatchers.containsString( | ||
" \"Labels\": {\n" | ||
+ " \"key1\": \"value1\",\n" | ||
+ " \"key2\": \"value2\"\n" | ||
+ " }")); | ||
Assert.assertThat( | ||
dockerContainerConfig, | ||
CoreMatchers.containsString( | ||
" \"var1=value1\",\n \"var2=value2\"\n")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to match the environment part ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue is that there are other environment variables already in there, like PATH and SSL_CERT_FILE. I guess I could use regex to skip those or just match those variables in the string as well (I'm not sure if they change though)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I can just use |
||
String history = new Command("docker", "history", imageReference).run(); | ||
Assert.assertThat(history, CoreMatchers.containsString("jib-integration-test")); | ||
Assert.assertThat(history, CoreMatchers.containsString("bazel build ...")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,7 @@ private static void addIfNotEmpty( | |
@Nullable private String baseImage; | ||
private List<String> entrypoint = Collections.emptyList(); | ||
private List<String> javaArguments = Collections.emptyList(); | ||
private Map<String, String> environment = Collections.emptyMap(); | ||
private List<String> exposedPorts = Collections.emptyList(); | ||
private Map<String, String> labels = Collections.emptyMap(); | ||
|
||
|
@@ -170,6 +171,17 @@ public JavaDockerContextGenerator setJavaArguments(List<String> javaArguments) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets the environment variables | ||
* | ||
* @param environment the environment variables | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: map from environment variable name to value? |
||
* @return this | ||
*/ | ||
public JavaDockerContextGenerator setEnvironment(Map<String, String> environment) { | ||
this.environment = environment; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the exposed ports. | ||
* | ||
|
@@ -240,6 +252,9 @@ public void generate(Path targetDirectory) throws IOException { | |
* | ||
* EXPOSE [port] | ||
* [More EXPOSE instructions, if necessary] | ||
* ENV [key1]="[value1]" \ | ||
* [key2]="[value2]" \ | ||
* [...] | ||
* LABEL [key1]="[value1]" \ | ||
* [key2]="[value2]" \ | ||
* [...] | ||
|
@@ -267,14 +282,24 @@ String makeDockerfile() throws JsonProcessingException { | |
dockerfile.append("\nEXPOSE ").append(port); | ||
} | ||
|
||
boolean firstLabel = true; | ||
boolean firstElement = true; | ||
for (Entry<String, String> env : environment.entrySet()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reusing booleans is a error prone. Maybe this code should use |
||
dockerfile | ||
.append(firstElement ? "\nENV " : " \\\n ") | ||
.append(env.getKey()) | ||
.append("=") | ||
.append(objectMapper.writeValueAsString(env.getValue())); | ||
firstElement = false; | ||
} | ||
|
||
firstElement = true; | ||
for (Entry<String, String> label : labels.entrySet()) { | ||
dockerfile | ||
.append(firstLabel ? "\nLABEL " : " \\\n ") | ||
.append(firstElement ? "\nLABEL " : " \\\n ") | ||
.append(label.getKey()) | ||
.append("=") | ||
.append(objectMapper.writeValueAsString(label.getValue())); | ||
firstLabel = false; | ||
firstElement = false; | ||
} | ||
|
||
dockerfile | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ jib { | |
args = ['An argument.'] | ||
mainClass = 'com.test.HelloWorld' | ||
jvmFlags = ['-Xms512m', '-Xdebug'] | ||
environment = [var1:'value1', var2:'value2'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: it might be clearer if the values were like |
||
ports = ['1000/tcp', '2000-2003/udp'] | ||
labels = [key1:'value1', key2:'value2'] | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. | |
|
||
### Added | ||
|
||
- `<skip>` configuration parameter to skip Jib execution in multi-module projects ([#865](https://github.com/GoogleContainerTools/jib/issues/865)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and add also settable via property |
||
- `<container><environment>` configuration parameter to configure environment variables ([#890](https://github.com/GoogleContainerTools/jib/issues/890)) | ||
|
||
### Changed | ||
|
||
### Fixed | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it can be static?