Skip to content

Commit

Permalink
Disable feature at the highest level
Browse files Browse the repository at this point in the history
  • Loading branch information
jprinet committed Jul 8, 2022
1 parent 97310f4 commit 886f195
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 57 deletions.
11 changes: 8 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuildScanInjection> 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
}
}

Expand All @@ -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());
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/hudson/plugins/gradle/injection/EnvUtil.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ class BuildScanInjectionGradleIntegrationTest extends AbstractIntegrationTest {
when:
disableBuildInjection(slave, gradleVersion)

then:
initScript.exists()

when:
turnOffBuildInjection(slave, gradleVersion)

then:
!initScript.exists()

Expand Down Expand Up @@ -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')
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 886f195

Please sign in to comment.