Skip to content
Permalink
Browse files

Merge pull request #3 from mhuin/master

JENKINS-41839 Substitue build parameters, number in topic and payload
  • Loading branch information
gdubya committed Feb 12, 2017
2 parents b165068 + 1510113 commit 22c430f5cd111140c1cd7e5a41eddcdc48958915
@@ -247,7 +247,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
}

/**
* Replace both static and environment variables defined in the given rawString
* Replace both static and environment variables, build parameters defined in the given rawString
* @param rawString The string containing variables to be replaced
* @param build The current build
* @param listener The current buildListener
@@ -256,6 +256,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
private String replaceVariables(final String rawString, final AbstractBuild build, BuildListener listener) {
String result = replaceStaticVariables(rawString, build);
result = replaceEnvironmentVariables(result, build, listener);
result = replaceBuildVariables(result, build, listener);
return result;
}

@@ -265,6 +266,7 @@ private String replaceVariables(final String rawString, final AbstractBuild buil
* <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
* <li>BUILD_NUMBER</li> The build number
* </ul>
* @param rawString The string containing variables to be replaced
* @param build The current build
@@ -273,6 +275,8 @@ private String replaceVariables(final String rawString, final AbstractBuild buil
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());
String number = "" + build.number;
result = result.replaceAll("\\$BUILD_NUMBER", number);
if (rawString.contains("$CULPRITS")) {
StringBuilder culprits = new StringBuilder();
String delim = "";
@@ -312,4 +316,23 @@ private String replaceEnvironmentVariables(final String rawString, final Abstrac
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 replaceBuildVariables(final String rawString, final AbstractBuild build, BuildListener listener) {
final PrintStream logger = listener.getLogger();
String result = rawString;
Map<String, String> buildVarMap = build.getBuildVariables();
for (Map.Entry<String, String> buildVarEntry : buildVarMap.entrySet()) {
String key = "\\$" + buildVarEntry.getKey();
String value = buildVarEntry.getValue();
result = result.replaceAll(key, value);
}
return result;
}

}
@@ -1,7 +1,8 @@
<p>The "payload" for the MQTT message.</p>
<p>In addition to environment variables, the following variables can also be used (all variables must be prefix with "$"):
<p>In addition to environment variables and build parameters, 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>
<li>CULPRITS - Comma-separated list of the culprits responsible for the current build result</li>
</ul></p>
<li>BUILD_NUMBER - the build number</li>
</ul></p>
@@ -1,8 +1,9 @@
<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>In addition to environment variables, the following variables can also be used (all variables must be prefix with "$"):
<p>In addition to environment variables and build parameters, 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>
<li>CULPRITS - Comma-separated list of the culprits responsible for the current build result</li>
</ul></p>
<li>BUILD_NUMBER - the build number</li>
</ul></p>

0 comments on commit 22c430f

Please sign in to comment.
You can’t perform that action at this time.