-
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
Conversation
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Reusing booleans is a error prone. Maybe this code should use java.util.StringJoiner
instead — it supports a prefix.
jib-maven-plugin/CHANGELOG.md
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
and add also settable via property jib.skip
Assert.assertThat( | ||
dockerContainerConfig, | ||
CoreMatchers.containsString( | ||
" \"var1=value1\",\n \"var2=value2\"\n")); |
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.
It would be nice to match the environment part ("Env": [
) too, if possible.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I can just use docker inspect -f {{.Config.Env}} <image>
.
@@ -226,4 +197,31 @@ private BuildConfiguration getBuildConfiguration( | |||
.setCreatedBy("jib-integration-test") | |||
.build(); | |||
} | |||
|
|||
private void assertDockerInspect(String imageReference) throws IOException, InterruptedException { |
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?
/** | ||
* Sets the environment variables | ||
* | ||
* @param environment the environment variables |
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.
nit: map from environment variable name to value?
for (Entry<String, String> env : environment.entrySet()) { | ||
joiner.add(env.getKey() + "=" + objectMapper.writeValueAsString(env.getValue())); | ||
} | ||
if (environment.entrySet().size() > 0) { |
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.
nit: can just be environment.size()
dockerfile.append(joiner.toString()); | ||
} | ||
|
||
joiner = new StringJoiner(" \\\n ", "\nLABEL ", ""); |
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.
The logic for labels seems to be the same as the logic for environment - maybe condense to a static method?
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it might be clearer if the values were like env1
/env2
?
@@ -34,7 +34,7 @@ jib { | |||
args = ['An argument.'] | |||
mainClass = 'com.test.HelloWorld' | |||
jvmFlags = ['-Xms512m', '-Xdebug'] | |||
environment = [var1:'value1', var2:'value2'] | |||
environment = [env1:'value1', env2:'value2'] |
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.
Oh I was referring to value1
and value2
since it would make the assertEquals
be something like \nenv1value\nenv2value\n
rather than \nvalue1\nvalue2\n
where value1
and value2
are not immediately clear where they should have come from.
* @throws JsonProcessingException if getting the json string of a map value fails | ||
*/ | ||
private static String mapToDockerfileString( | ||
Map<String, String> map, String command, ObjectMapper objectMapper) |
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.
ObjectMapper
can prob just be eliminated and made into a constant.
* | ||
* <pre>{@code | ||
* command key1="value1" \ | ||
* key2="value2" \ |
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.
nit: should we line up key1
and key2
since we're adding spaces anyways?
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.
The command names are different lengths, and adding a variable number of spaces here would have harmed readability or required a weird extra parameter, so I don't think it's worth it, unless there's a function we can use to generate whitespace that I don't know about (that doesn't require another import
). I think it's fine as is.
It'd be really nice if these PRs had a bit more content in the description so it's really easy for reviewers to understand what the changes are without looking at code. For instance if this PR actually changes plugin config params it should include examples of what it enables. |
Fixes #890
This adds a new
environment
configuration parameter to the maven and gradle plugins to set environment variables on the container image.Maven:
Gradle:
This PR also adds the corresponding "ENV" command to the docker context generator.
Also slipped
skip
into the changelog to fix #929