Skip to content
Permalink
Browse files
JENKINS-31669 Substitute environment variables in topic / payload
  • Loading branch information
gdubya committed Jan 9, 2016
1 parent 7d945be commit a344095d61e5621e78bccf1f2f5b240358be616f
@@ -5,6 +5,7 @@
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
@@ -34,6 +35,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Map;

/**
* A simple build result notifier that publishes the result via MQTT.
@@ -154,8 +156,8 @@ public boolean perform(final AbstractBuild build, final Launcher launcher, final
}
mqtt.connect(mqttConnectOptions);
mqtt.publish(
replaceVariables(getTopic(), build),
replaceVariables(getMessage(), build).getBytes(),
replaceVariables(getTopic(), build, listener),
replaceVariables(getMessage(), build, listener).getBytes(),
getQos(),
isRetainMessage()
);
@@ -244,7 +246,31 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
}
}

private String replaceVariables(final String rawString, final AbstractBuild build) {
/**
* Replace both static and environment variables defined in the given rawString
* @param rawString The string containing variables to be replaced
* @param build The current build
* @param listener The current buildListener
* @return a new String with variables replaced
*/
private String replaceVariables(final String rawString, final AbstractBuild build, BuildListener listener) {
String result = replaceStaticVariables(rawString, build);
result = replaceEnvironmentVariables(result, build, listener);
return result;
}

/**
* Replace the static variables (defined by this plugin):
* <ul>
* <li>BUILD_RESULT</li> The build result (e.g. SUCCESS)
* <li>PROJECT_URL</li> The URL to the project
* <li>CULPRITS</li> The culprits responsible for the build
* </ul>
* @param rawString The string containing variables to be replaced
* @param build The current build
* @return a new String with variables replaced
*/
private String replaceStaticVariables(final String rawString, final AbstractBuild build) {
String result = rawString.replaceAll("\\$PROJECT_URL", build.getProject().getUrl());
result = result.replaceAll("\\$BUILD_RESULT", build.getResult().toString());
if (rawString.contains("$CULPRITS")) {
@@ -259,4 +285,31 @@ private String replaceVariables(final String rawString, final AbstractBuild buil
return result;
}

/**
*
* @param rawString The string containing variables to be replaced
* @param build The current build
* @param listener The current buildListener
* @return a new String with variables replaced
*/
private String replaceEnvironmentVariables(final String rawString, final AbstractBuild build, BuildListener listener) {
final PrintStream logger = listener.getLogger();
String result = rawString;
try {
EnvVars environment = build.getProject().getEnvironment(build.getBuiltOn(), listener);
for (Map.Entry<String, String> envVarEntry : environment.entrySet()) {
String key = "\\$" + envVarEntry.getKey();
String value = envVarEntry.getValue();
result = result.replaceAll(key, value);
}
} catch (IOException ioe) {
logger.println("ERROR: Caught IOException while trying to replace environment variables: " + ioe.getMessage());
ioe.printStackTrace(logger);
} catch (InterruptedException ie) {
logger.println("ERROR: Caught InterruptedException while trying to replace environment variables: " + ie.getMessage());
ie.printStackTrace(logger);
}
return result;
}

}
@@ -1,5 +1,5 @@
<p>The "payload" for the MQTT message.</p>
<p>The following variables can also be used (prefix with "$"):
<p>In addition to environment variables, the following variables can also be used (all variables must be prefix with "$"):
<ul>
<li>BUILD_RESULT - The result of the build (e.g. SUCCESS, FAILURE, ABORTED, etc.)</li>
<li>PROJECT_URL - The job project's URL (e.g. "job/my-build")</li>
@@ -1,6 +1,6 @@
<p>The topic to which to publish the notification.
If no value is specified then the default value ("jenkins/$JOB_URL" e.g. "jenkins/job/my-build") is used.</p>
<p>The following variables can also be used (prefix with "$"):
<p>In addition to environment variables, the following variables can also be used (all variables must be prefix with "$"):
<ul>
<li>BUILD_RESULT - The result of the build (e.g. SUCCESS, FAILURE, ABORTED, etc.)</li>
<li>PROJECT_URL - The job project's URL (e.g. "job/my-build")</li>

0 comments on commit a344095

Please sign in to comment.