From 570150b4960bc81d3612e040bd9aec8e549e9057 Mon Sep 17 00:00:00 2001 From: Paul van der Bles Date: Fri, 4 Oct 2024 16:18:44 +0200 Subject: [PATCH 1/6] Use Gradle Property and Provider to enable lazy evaluation for jib.container.environment (#4067) --- CONTRIBUTING.md | 1 + .../tools/jib/gradle/ContainerParameters.java | 19 ++++++++++--------- .../jib/gradle/GradleRawConfiguration.java | 2 +- .../gradle/GradleRawConfigurationTest.java | 5 +++-- .../tools/jib/gradle/JibExtensionTest.java | 8 ++------ .../cloud/tools/jib/gradle/JibPluginTest.java | 8 ++++++++ .../projects/lazy-evaluation/build.gradle | 14 ++++++++++++++ 7 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index aad54536bc..f41973a9f5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,6 +15,7 @@ Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution; this simply gives us permission to use and redistribute your contributions as part of the project. Head over to to see +part of the project. Head over to to see your current agreements on file or to sign a new one. You generally only need to submit a CLA once, so if you've already submitted one diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java index 095da5008a..7561550a55 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java @@ -40,7 +40,7 @@ public class ContainerParameters { private final ListProperty jvmFlags; - private Map environment = Collections.emptyMap(); + private MapProperty environment; private ListProperty entrypoint; private List extraClasspath = Collections.emptyList(); private boolean expandClasspathDependencies; @@ -64,6 +64,7 @@ public ContainerParameters(ObjectFactory objectFactory) { mainClass = objectFactory.property(String.class); jvmFlags = objectFactory.listProperty(String.class); entrypoint = objectFactory.listProperty(String.class); + environment = objectFactory.mapProperty(String.class, String.class).empty(); } @Input @@ -109,18 +110,18 @@ public void setJvmFlags(Provider> jvmFlags) { @Input @Optional - public Map getEnvironment() { - if (System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT) != null) { - return ConfigurationPropertyValidator.parseMapProperty( - System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT)); + public MapProperty getEnvironment() { + String environmentProperty = System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT); + if (environmentProperty != null) { + Map parsedLabels = + ConfigurationPropertyValidator.parseMapProperty(environmentProperty); + if (!parsedLabels.equals(environment.get())) { + environment.set(parsedLabels); + } } return environment; } - public void setEnvironment(Map environment) { - this.environment = environment; - } - @Input @Optional public List getExtraClasspath() { diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java index 10195cc756..b93a5e4097 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleRawConfiguration.java @@ -108,7 +108,7 @@ public String getAppRoot() { @Override public Map getEnvironment() { - return jibExtension.getContainer().getEnvironment(); + return jibExtension.getContainer().getEnvironment().get(); } @Override diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java index 8655241553..a3feb88ecb 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java @@ -40,6 +40,8 @@ public class GradleRawConfigurationTest { @Mock private MapProperty labels; + @Mock private MapProperty environment; + @Test public void testGetters() { JibExtension jibExtension = Mockito.mock(JibExtension.class); @@ -85,8 +87,7 @@ public void testGetters() { Mockito.when(containerParameters.getAppRoot()).thenReturn("/app/root"); Mockito.when(containerParameters.getArgs()).thenReturn(Arrays.asList("--log", "info")); Mockito.when(containerParameters.getEntrypoint()).thenReturn(Arrays.asList("java", "Main")); - Mockito.when(containerParameters.getEnvironment()) - .thenReturn(new HashMap<>(ImmutableMap.of("currency", "dollar"))); + Mockito.when(environment.get()).thenReturn(Collections.singletonMap("currency", "dollar")); Mockito.when(containerParameters.getJvmFlags()).thenReturn(Arrays.asList("-cp", ".")); Mockito.when(labels.get()).thenReturn(Collections.singletonMap("unit", "cm")); Mockito.when(containerParameters.getLabels()).thenReturn(labels); diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java index 440ae17779..2de786a858 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibExtensionTest.java @@ -176,7 +176,7 @@ public void testToTags_containsEmptyTag() { @Test public void testContainer() { assertThat(testJibExtension.getContainer().getJvmFlags()).isEmpty(); - assertThat(testJibExtension.getContainer().getEnvironment()).isEmpty(); + assertThat(testJibExtension.getContainer().getEnvironment().get()).isEmpty(); assertThat(testJibExtension.getContainer().getExtraClasspath()).isEmpty(); assertThat(testJibExtension.getContainer().getExpandClasspathDependencies()).isFalse(); assertThat(testJibExtension.getContainer().getMainClass()).isNull(); @@ -192,7 +192,6 @@ public void testContainer() { testJibExtension.container( container -> { container.setJvmFlags(Arrays.asList("jvmFlag1", "jvmFlag2")); - container.setEnvironment(ImmutableMap.of("var1", "value1", "var2", "value2")); container.setEntrypoint(Arrays.asList("foo", "bar", "baz")); container.setExtraClasspath(Arrays.asList("/d1", "/d2", "/d3")); container.setExpandClasspathDependencies(true); @@ -207,9 +206,6 @@ public void testContainer() { ContainerParameters container = testJibExtension.getContainer(); assertThat(container.getEntrypoint()).containsExactly("foo", "bar", "baz").inOrder(); assertThat(container.getJvmFlags()).containsExactly("jvmFlag1", "jvmFlag2").inOrder(); - assertThat(container.getEnvironment()) - .containsExactly("var1", "value1", "var2", "value2") - .inOrder(); assertThat(container.getExtraClasspath()).containsExactly("/d1", "/d2", "/d3").inOrder(); assertThat(testJibExtension.getContainer().getExpandClasspathDependencies()).isTrue(); assertThat(testJibExtension.getContainer().getMainClass()).isEqualTo("mainClass"); @@ -465,7 +461,7 @@ public void testProperties() { .containsExactly("entry1", "entry2", "entry3") .inOrder(); System.setProperty("jib.container.environment", "env1=val1,env2=val2"); - assertThat(testJibExtension.getContainer().getEnvironment()) + assertThat(testJibExtension.getContainer().getEnvironment().get()) .containsExactly("env1", "val1", "env2", "val2") .inOrder(); System.setProperty("jib.container.extraClasspath", "/d1,/d2,/d3"); diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java index fe2829d0dd..50a115b5c1 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java @@ -368,6 +368,14 @@ public void testLazyEvalForLabels() { "labels contain values [firstkey:updated-first-label, secondKey:updated-second-label]"); } + @Test + public void testLazyEvalForEnvironment() { + BuildResult showLabels = testProject.build("showenvironment", "-Djib.console=plain"); + assertThat(showLabels.getOutput()) + .contains( + "environment contains values [var1:val1updated, var2:val2updated]"); + } + @Test public void testLazyEvalForEntryPoint() { BuildResult showEntrypoint = testProject.build("showentrypoint", "-Djib.console=plain"); diff --git a/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle b/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle index 621baa34b5..37252b6e77 100644 --- a/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle +++ b/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle @@ -17,12 +17,20 @@ dependencies { project.ext.value = 'original' project.ext.jibCreationTime = '1970-01-23T00:23:42Z' project.ext.jibFilesModificationTime = '1970-01-23T01:23:42Z' +project.ext.jibEnvrionment = [ + "var1": "val1", + "var2": "val2" +] project.afterEvaluate { project.ext.value = 'updated' project.ext.getCustomPermissions = { -> return ['/updated': '755'] } project.ext.jibCreationTime = '2022-07-19T10:23:42Z' project.ext.jibFilesModificationTime = '2022-07-19T11:23:42Z' + project.ext.jibEnvrionment = [ + "var1": "val1updated", + "var2": "val2updated" + ] } jib { @@ -38,6 +46,7 @@ jib { ] } entrypoint = project.provider { [project.ext.value] } + environment = project.provider { project.ext.jibEnvrionment } creationTime = project.provider { project.ext.jibCreationTime } filesModificationTime = project.provider { project.ext.jibFilesModificationTime } mainClass = project.provider { project.ext.value } @@ -82,3 +91,8 @@ tasks.register('showJvmFlags') { List prop = project.extensions.jib.container.jvmFlags println('jvmFlags value ' + prop) } + +tasks.register('showenvironment') { + Map prop = project.extensions.getByName('jib')['container']['environment'].get() + println('environment contains values ' + prop) +} \ No newline at end of file From 346f45c758d99e43a2a5903ba23cd5a8f21d18ba Mon Sep 17 00:00:00 2001 From: Paul van der Bles Date: Fri, 4 Oct 2024 16:23:53 +0200 Subject: [PATCH 2/6] Use Gradle Property and Provider to enable lazy evaluation for jib.container.environment (#4067) --- .../cloud/tools/jib/gradle/GradleRawConfigurationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java index a3feb88ecb..49c9ea3e5f 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/GradleRawConfigurationTest.java @@ -88,6 +88,7 @@ public void testGetters() { Mockito.when(containerParameters.getArgs()).thenReturn(Arrays.asList("--log", "info")); Mockito.when(containerParameters.getEntrypoint()).thenReturn(Arrays.asList("java", "Main")); Mockito.when(environment.get()).thenReturn(Collections.singletonMap("currency", "dollar")); + Mockito.when(containerParameters.getEnvironment()).thenReturn(environment); Mockito.when(containerParameters.getJvmFlags()).thenReturn(Arrays.asList("-cp", ".")); Mockito.when(labels.get()).thenReturn(Collections.singletonMap("unit", "cm")); Mockito.when(containerParameters.getLabels()).thenReturn(labels); From 84ea09c259fe201c5164bd5b060b334288b2e7b1 Mon Sep 17 00:00:00 2001 From: Paul van der Bles Date: Fri, 4 Oct 2024 23:48:36 +0200 Subject: [PATCH 3/6] Undo duplicate line --- CONTRIBUTING.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f41973a9f5..aad54536bc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,6 @@ Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution; this simply gives us permission to use and redistribute your contributions as part of the project. Head over to to see -part of the project. Head over to to see your current agreements on file or to sign a new one. You generally only need to submit a CLA once, so if you've already submitted one From 1109172de194ef01d932fecdb73a6d2d7c5fb369 Mon Sep 17 00:00:00 2001 From: Paul van der Bles Date: Mon, 7 Oct 2024 10:58:48 +0200 Subject: [PATCH 4/6] Rename variables(#4067) --- .../google/cloud/tools/jib/gradle/ContainerParameters.java | 6 +++--- .../com/google/cloud/tools/jib/gradle/JibPluginTest.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java index 7561550a55..961aa90b34 100644 --- a/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java +++ b/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/ContainerParameters.java @@ -113,10 +113,10 @@ public void setJvmFlags(Provider> jvmFlags) { public MapProperty getEnvironment() { String environmentProperty = System.getProperty(PropertyNames.CONTAINER_ENVIRONMENT); if (environmentProperty != null) { - Map parsedLabels = + Map parsedEnvironment = ConfigurationPropertyValidator.parseMapProperty(environmentProperty); - if (!parsedLabels.equals(environment.get())) { - environment.set(parsedLabels); + if (!parsedEnvironment.equals(environment.get())) { + environment.set(parsedEnvironment); } } return environment; diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java index 50a115b5c1..ad2beadb75 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java @@ -370,8 +370,8 @@ public void testLazyEvalForLabels() { @Test public void testLazyEvalForEnvironment() { - BuildResult showLabels = testProject.build("showenvironment", "-Djib.console=plain"); - assertThat(showLabels.getOutput()) + BuildResult showEnvironment = testProject.build("showenvironment", "-Djib.console=plain"); + assertThat(showEnvironment.getOutput()) .contains( "environment contains values [var1:val1updated, var2:val2updated]"); } From 6385257d923a35576aa716097277fb64fd923c6f Mon Sep 17 00:00:00 2001 From: Paul van der Bles Date: Mon, 7 Oct 2024 11:10:34 +0200 Subject: [PATCH 5/6] Update test to match lazy eval labels test (#4067) --- .../google/cloud/tools/jib/gradle/JibPluginTest.java | 2 +- .../gradle/projects/lazy-evaluation/build.gradle | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java index ad2beadb75..82da3c2365 100644 --- a/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java +++ b/jib-gradle-plugin/src/test/java/com/google/cloud/tools/jib/gradle/JibPluginTest.java @@ -373,7 +373,7 @@ public void testLazyEvalForEnvironment() { BuildResult showEnvironment = testProject.build("showenvironment", "-Djib.console=plain"); assertThat(showEnvironment.getOutput()) .contains( - "environment contains values [var1:val1updated, var2:val2updated]"); + "environment contains values [var1:updated-first-environment-var, var2:updated-second-environment-var]"); } @Test diff --git a/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle b/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle index 37252b6e77..5390008aa6 100644 --- a/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle +++ b/jib-gradle-plugin/src/test/resources/gradle/projects/lazy-evaluation/build.gradle @@ -27,10 +27,6 @@ project.afterEvaluate { project.ext.getCustomPermissions = { -> return ['/updated': '755'] } project.ext.jibCreationTime = '2022-07-19T10:23:42Z' project.ext.jibFilesModificationTime = '2022-07-19T11:23:42Z' - project.ext.jibEnvrionment = [ - "var1": "val1updated", - "var2": "val2updated" - ] } jib { @@ -46,7 +42,12 @@ jib { ] } entrypoint = project.provider { [project.ext.value] } - environment = project.provider { project.ext.jibEnvrionment } + environment = project.provider { + [ + var1 : project.ext.value + '-first-environment-var', + var2: project.ext.value + '-second-environment-var' + ] + } creationTime = project.provider { project.ext.jibCreationTime } filesModificationTime = project.provider { project.ext.jibFilesModificationTime } mainClass = project.provider { project.ext.value } From 48c077272317c844dbe0e65699d3a2b6f044d68c Mon Sep 17 00:00:00 2001 From: Paul van der Bles Date: Mon, 21 Oct 2024 16:31:24 +0200 Subject: [PATCH 6/6] retrigger checks