From 886f1957a50348bb27305fe12500f8742470e92c Mon Sep 17 00:00:00 2001 From: Jerome Prinet Date: Fri, 8 Jul 2022 14:30:49 +0200 Subject: [PATCH] Disable feature at the highest level --- README.adoc | 11 ++++-- .../gradle/injection/BuildScanInjection.java | 14 +------- .../injection/BuildScanInjectionListener.java | 32 ++++++++++++----- .../plugins/gradle/injection/EnvUtil.java | 15 ++++++++ .../injection/GradleBuildScanInjection.java | 26 +++++++------- .../injection/MavenBuildScanInjection.java | 34 +++++++++---------- ...dScanInjectionGradleIntegrationTest.groovy | 24 ++++++++++++- ...ldScanInjectionMavenIntegrationTest.groovy | 3 +- 8 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 src/main/java/hudson/plugins/gradle/injection/EnvUtil.java diff --git a/README.adoc b/README.adoc index ce50a5bb..36d790eb 100644 --- a/README.adoc +++ b/README.adoc @@ -96,12 +96,15 @@ The plugin can be configured to inject Build Scans into any Gradle or Maven buil This feature works by installing scripts and Jars on each agent depending on the environment variable settings. +The feature can be enabled by setting the `JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION` environment variable. + If the `JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_PLUGIN_VERSION` environment variable is present, injection is activated for Gradle builds. This causes an init script to be installed in the user home of each connected agent. -The init script is removed when the environment variable is set to `off`, thus deactivating injection for Gradle builds. +The init script is removed when `JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_PLUGIN_VERSION` is unset, thus deactivating injection for Gradle builds. + If the `JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_EXTENSION_VERSION` environment variable is present, injection is activated for Maven builds. This causes the Gradle Enterprise Maven Extension to be installed on each connected agent, and the `MAVEN_OPTS` environment variable will be modified so that each Maven process will load the extension. -The extension is deleted from all agents and the changes to `MAVEN_OPTS` are reverted when the environment variable is set to `off`. +The extension is deleted from all agents and the changes to `MAVEN_OPTS` are reverted when `JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_EXTENSION_VERSION` is unset. === Configuration options @@ -111,11 +114,13 @@ The following table lists all available configuration options. |=== |Environment variable|Build Tool|Description +|JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION|Both|Enable the feature if set. |JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_URL|Both|The URL of the Gradle Enterprise server to publish Build Scans to. |JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_ALLOW_UNTRUSTED_SERVER|Both|Whether to allow publishing to a server with a self-signed certificate. Defaults to `false`. -|JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_EXTENSION_VERSION|Maven|Enables injection for Maven builds. |JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_PLUGIN_VERSION|Gradle|Enables injection for Gradle builds and defines which version of the https://plugins.gradle.org/plugin/com.gradle.enterprise[Gradle Enterprise Gradle plugin] to use. |JENKINSGRADLEPLUGIN_CCUD_PLUGIN_VERSION|Gradle|Defines which version of the https://plugins.gradle.org/plugin/com.gradle.common-custom-user-data-gradle-plugin[Gradle Enterprise Gradle plugin] to use. +|JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_EXTENSION_VERSION|Maven|Enables injection for Maven builds. +|JENKINSGRADLEPLUGIN_CCUD_EXTENSION_VERSION|Maven|Defines which version of the https://docs.gradle.com/enterprise/maven-extension[Gradle Enterprise Maven extension] to use. |=== In addition to the variables above you might want to set the `GRADLE_ENTERPRISE_ACCESS_KEY` variable if you're Gradle Enterprise server requires authentication for publishing Build Scans. Refer to the https://docs.gradle.com/enterprise/gradle-plugin/#via_environment_variable[Gradle Enterprise Gradle plugin manual] and the https://docs.gradle.com/enterprise/maven-extension/#via_environment_variable[Gradle Enterprise Maven Extension manual] for more information. diff --git a/src/main/java/hudson/plugins/gradle/injection/BuildScanInjection.java b/src/main/java/hudson/plugins/gradle/injection/BuildScanInjection.java index e9924ca4..96feecc8 100644 --- a/src/main/java/hudson/plugins/gradle/injection/BuildScanInjection.java +++ b/src/main/java/hudson/plugins/gradle/injection/BuildScanInjection.java @@ -3,22 +3,10 @@ import hudson.EnvVars; import hudson.model.Node; -import java.util.Locale; - public interface BuildScanInjection { - String DISABLED = "off"; - - default String getEnv(EnvVars env, String key) { - return env != null ? env.get(key) : null; - } - - default boolean isEnabled(EnvVars env) { - return getEnv(env, getActivationEnvironmentVariableName()) != null; - } - default boolean isOn(EnvVars env) { - return isEnabled(env) && !DISABLED.equals(getEnv(env, getActivationEnvironmentVariableName().toLowerCase(Locale.ROOT))); + return EnvUtil.getEnv(env, getActivationEnvironmentVariableName()) != null; } String getActivationEnvironmentVariableName(); diff --git a/src/main/java/hudson/plugins/gradle/injection/BuildScanInjectionListener.java b/src/main/java/hudson/plugins/gradle/injection/BuildScanInjectionListener.java index bf4c8b87..ea656dd9 100644 --- a/src/main/java/hudson/plugins/gradle/injection/BuildScanInjectionListener.java +++ b/src/main/java/hudson/plugins/gradle/injection/BuildScanInjectionListener.java @@ -18,20 +18,32 @@ public class BuildScanInjectionListener extends ComputerListener { private static final Logger LOGGER = Logger.getLogger(BuildScanInjectionListener.class.getName()); + private static final String FEATURE_TOGGLE_INJECTION = "JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION"; + private final List injections = Arrays.asList( new GradleBuildScanInjection(), new MavenBuildScanInjection() ); + private boolean isInjectionEnabled(EnvVars env) { + return EnvUtil.getEnv(env, FEATURE_TOGGLE_INJECTION) != null; + } + @Override public void onOnline(Computer c, TaskListener listener) { try { EnvVars envGlobal = c.buildEnvironment(listener); - EnvVars envComputer = c.getEnvironment(); + if(isInjectionEnabled(envGlobal)) { + try { + EnvVars envComputer = c.getEnvironment(); - inject(c, envGlobal, envComputer); + inject(c, envGlobal, envComputer); + } catch (IOException | InterruptedException e) { + LOGGER.info("Error processing scan injection - " + e.getMessage()); + } + } } catch (IOException | InterruptedException e) { - LOGGER.info("Error processing scan injection - " + e.getMessage()); + // nothing can be done } } @@ -41,12 +53,14 @@ public void onConfigurationChange() { .get(EnvironmentVariablesNodeProperty.class); EnvVars envGlobal = envProperty != null ? envProperty.getEnvVars() : null; - for (Computer c : Jenkins.get().getComputers()) { - try { - final EnvVars envComputer = c.getEnvironment(); - inject(c, envGlobal, envComputer); - } catch (IOException | InterruptedException e) { - LOGGER.info("Error processing scan injection - " + e.getMessage()); + if(isInjectionEnabled(envGlobal)) { + for (Computer c : Jenkins.get().getComputers()) { + try { + final EnvVars envComputer = c.getEnvironment(); + inject(c, envGlobal, envComputer); + } catch (IOException | InterruptedException e) { + LOGGER.info("Error processing scan injection - " + e.getMessage()); + } } } } diff --git a/src/main/java/hudson/plugins/gradle/injection/EnvUtil.java b/src/main/java/hudson/plugins/gradle/injection/EnvUtil.java new file mode 100644 index 00000000..c26e9990 --- /dev/null +++ b/src/main/java/hudson/plugins/gradle/injection/EnvUtil.java @@ -0,0 +1,15 @@ +package hudson.plugins.gradle.injection; + +import hudson.EnvVars; +import hudson.FilePath; + +import java.io.IOException; +import java.io.InputStream; + +public class EnvUtil { + + public static String getEnv(EnvVars env, String key) { + return env != null ? env.get(key) : null; + } + +} diff --git a/src/main/java/hudson/plugins/gradle/injection/GradleBuildScanInjection.java b/src/main/java/hudson/plugins/gradle/injection/GradleBuildScanInjection.java index 35bc6963..08a76b8e 100644 --- a/src/main/java/hudson/plugins/gradle/injection/GradleBuildScanInjection.java +++ b/src/main/java/hudson/plugins/gradle/injection/GradleBuildScanInjection.java @@ -29,30 +29,30 @@ public String getActivationEnvironmentVariableName() { @Override public void inject(Node node, EnvVars envGlobal, EnvVars envComputer) { - if (isEnabled(envGlobal)) { - try { - String initScriptDirectory = getInitScriptDirectory(envGlobal, envComputer); - - if (isOn(envGlobal)) { - copyInitScript(node.getChannel(), initScriptDirectory); - } else { - removeInitScript(node.getChannel(), initScriptDirectory); - } - } catch (IllegalStateException e) { + try { + String initScriptDirectory = getInitScriptDirectory(envGlobal, envComputer); + + if (isOn(envGlobal)) { + copyInitScript(node.getChannel(), initScriptDirectory); + } else { + removeInitScript(node.getChannel(), initScriptDirectory); + } + } catch (IllegalStateException e) { + if (isOn(envGlobal)) { LOGGER.warning("Error: " + e.getMessage()); } } } private String getInitScriptDirectory(EnvVars envGlobal, EnvVars envComputer) { - String gradleHomeOverride = getEnv(envGlobal, JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_GRADLE_HOME); - String homeOverride = getEnv(envGlobal, JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_HOME); + String gradleHomeOverride = EnvUtil.getEnv(envGlobal, JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_GRADLE_HOME); + String homeOverride = EnvUtil.getEnv(envGlobal, JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_HOME); if (gradleHomeOverride != null) { return gradleHomeOverride + "/" + INIT_DIR; } else if (homeOverride != null) { return homeOverride + "/" + GRADLE_DIR + "/" + INIT_DIR; } else { - String home = getEnv(envComputer, "HOME"); + String home = EnvUtil.getEnv(envComputer, "HOME"); if(home == null){ throw new IllegalStateException("HOME is not set"); } diff --git a/src/main/java/hudson/plugins/gradle/injection/MavenBuildScanInjection.java b/src/main/java/hudson/plugins/gradle/injection/MavenBuildScanInjection.java index 83a2b9de..d6517384 100644 --- a/src/main/java/hudson/plugins/gradle/injection/MavenBuildScanInjection.java +++ b/src/main/java/hudson/plugins/gradle/injection/MavenBuildScanInjection.java @@ -47,23 +47,23 @@ public String getActivationEnvironmentVariableName() { @Override public void inject(Node node, EnvVars envGlobal, EnvVars envComputer) { - if (isEnabled(envGlobal)) { - try { - if (node == null) { - return; - } - - FilePath rootPath = node.getRootPath(); - if (rootPath == null) { - return; - } - - if (isOn(envGlobal)) { - injectMavenExtension(node, rootPath); - } else { - removeMavenExtension(node, rootPath); - } - } catch (IllegalStateException e) { + try { + if (node == null) { + return; + } + + FilePath rootPath = node.getRootPath(); + if (rootPath == null) { + return; + } + + if (isOn(envGlobal)) { + injectMavenExtension(node, rootPath); + } else { + removeMavenExtension(node, rootPath); + } + } catch (IllegalStateException e) { + if (isOn(envGlobal)) { LOGGER.warning("Error: " + e.getMessage()); } } diff --git a/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionGradleIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionGradleIntegrationTest.groovy index 4c087002..a25bc2fb 100644 --- a/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionGradleIntegrationTest.groovy +++ b/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionGradleIntegrationTest.groovy @@ -119,6 +119,12 @@ class BuildScanInjectionGradleIntegrationTest extends AbstractIntegrationTest { when: disableBuildInjection(slave, gradleVersion) + then: + initScript.exists() + + when: + turnOffBuildInjection(slave, gradleVersion) + then: !initScript.exists() @@ -157,6 +163,7 @@ task hello { EnvVars env = nodeProperty.getEnvVars() // we override the location of the init script to a workspace internal folder to allow parallel test runs + env.put('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION','on') env.put("JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_GRADLE_HOME", getGradleHome(slave, gradleVersion)) env.put('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_PLUGIN_VERSION', '3.10.1') env.put('GRADLE_OPTS','-Dscan.uploadInBackground=false') @@ -171,7 +178,22 @@ task hello { NodeProperty nodeProperty = new EnvironmentVariablesNodeProperty() EnvVars env = nodeProperty.getEnvVars() - env.put('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_PLUGIN_VERSION', 'off') + env.remove('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION') + env.put("JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_GRADLE_HOME", getGradleHome(slave, gradleVersion)) + + j.jenkins.globalNodeProperties.clear() + j.jenkins.globalNodeProperties.add(nodeProperty) + + // sync changes + restartSlave(slave) + } + + private void turnOffBuildInjection(DumbSlave slave, String gradleVersion) { + NodeProperty nodeProperty = new EnvironmentVariablesNodeProperty() + EnvVars env = nodeProperty.getEnvVars() + + env.put('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION', 'on') + env.remove('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_PLUGIN_VERSION') env.put("JENKINSGRADLEPLUGIN_BUILD_SCAN_OVERRIDE_GRADLE_HOME", getGradleHome(slave, gradleVersion)) j.jenkins.globalNodeProperties.clear() diff --git a/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy b/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy index db6eef99..ccb39557 100644 --- a/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy +++ b/src/test/groovy/hudson/plugins/gradle/injection/BuildScanInjectionMavenIntegrationTest.groovy @@ -138,7 +138,8 @@ node { } private DumbSlave setupBuildInjection() { - EnvVars env = addGlobalEnvVar('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_EXTENSION_VERSION', '1.14.2') + EnvVars env = addGlobalEnvVar('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_INJECTION', 'on') + env = addGlobalEnvVar('JENKINSGRADLEPLUGIN_GRADLE_ENTERPRISE_EXTENSION_VERSION', '1.14.2') DumbSlave slave = j.createOnlineSlave(Label.get("foo"), env) slave }