提交 6327e265 编写于 作者: K Kohsuke Kawaguchi

Added the set-build-parameter command.

上级 0adc32a7
......@@ -55,6 +55,8 @@ Upcoming changes</a>
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=rfe>
Added a new <tt>set-build-parameter</tt> command that can update a build variable from within a build.
<li class=rfe>
Can use <code>-Dhudson.udp=-1</code> to disable UDP broadcast without showing an ugly exception.
<li class=bug>
......
package hudson.cli;
import hudson.Extension;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.StringParameterValue;
import org.kohsuke.args4j.Argument;
import java.util.Collections;
/**
* Used from the build to update the build variable.
*
* This allows one build step to affect the environment variables seen by later build steps.
*
* @author Kohsuke Kawaguchi
* @since 1.514
*/
@Extension
public class SetBuildParameterCommand extends CommandDuringBuild {
@Argument(index=0, required=true, usage="Name of the build variable")
public String name;
@Argument(index=1,required=true, usage="Value of the build variable")
public String value;
@Override
public String getShortDescription() {
return Messages.SetBuildParameterCommand_ShortDescription();
}
@Override
protected int run() throws Exception {
Run r = getCurrentlyBuilding();
StringParameterValue p = new StringParameterValue(name, value);
ParametersAction a = r.getAction(ParametersAction.class);
if (a!=null) {
ParametersAction b = a.createUpdated(Collections.singleton(p));
r.addAction(b);
r.getActions().remove(a);
} else {
r.addAction(new ParametersAction(p));
}
return 0;
}
}
......@@ -36,6 +36,7 @@ import hudson.util.VariableResolver;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
......@@ -159,6 +160,29 @@ public class ParametersAction implements Action, Iterable<ParameterValue>, Queue
}
}
/**
* Creates a new {@link ParametersAction} that contains all the parameters in this action
* with the overrides / new values given as parameters.
*/
public ParametersAction createUpdated(Collection<? extends ParameterValue> newValues) {
List<ParameterValue> r = new ArrayList<ParameterValue>();
Set<String> names = new HashSet<String>();
for (ParameterValue v : newValues) {
names.add(v.name);
}
for (Iterator<ParameterValue> itr = parameters.iterator(); itr.hasNext(); ) {
ParameterValue v = itr.next();
if (!names.contains(v.getName()))
r.add(v);
}
r.addAll(newValues);
return new ParametersAction(r);
}
private Object readResolve() {
if (build != null)
OldDataMonitor.report(build, "1.283");
......
......@@ -37,6 +37,7 @@ MailCommand.ShortDescription=\
Reads stdin and sends that out as an e-mail.
SetBuildDescriptionCommand.ShortDescription=\
Sets the description of a build.
SetBuildParameterCommand.ShortDescription=Update/set the build parameter of the current build in progress
SetBuildResultCommand.ShortDescription=\
Sets the result of the current build. Works only if invoked from within a build.
VersionCommand.ShortDescription=\
......
package hudson.cli
import hudson.Launcher
import hudson.model.AbstractBuild
import hudson.model.BuildListener
import hudson.model.ParametersAction
import hudson.tasks.Shell
import jenkins.model.JenkinsLocationConfiguration
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.TestBuilder
/**
* @author Kohsuke Kawaguchi
*/
public class SetBuildParameterCommandTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Test
public void cli() {
JenkinsLocationConfiguration.get().url = j.URL;
def p = j.createFreeStyleProject();
p.buildersList.add(new TestBuilder() {
@Override
boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
def jar = j.jenkins.servletContext.getResource("/WEB-INF/jenkins-cli.jar")
build.workspace.child("cli.jar").copyFrom(jar);
return true;
}
});
p.buildersList.add(new Shell("java -jar cli.jar set-build-parameter a b"))
p.buildersList.add(new Shell("java -jar cli.jar set-build-parameter a x"))
p.buildersList.add(new Shell("java -jar cli.jar set-build-parameter b y"))
def r = [:];
def b = j.assertBuildStatusSuccess(p.scheduleBuild2(0))
b.getAction(ParametersAction.class).parameters.each { v -> r[v.name]=v.value }
assert r.equals(["a":"x", "b":"y"]);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册