Skip to content

Commit

Permalink
Merge pull request #3 from imod/build-params
Browse files Browse the repository at this point in the history
add support for build parameters in maven release

In addition to the normal release parameters, the 'normal' build parameters are shown and can be modified too before triggering a release build.

This fixes or supports the following currently open issues: 
* JENKINS-10127: M2 Release plugin ignores parameters from a parameterized build
* JENKINS-4690: Be able to arbitrary paramterize m2 releases 
* JENKINS-4500: Make it possible to select a node to do the release on (together with the nodelabel-plugin)
* JENKINS-4958: add switch for -DdryRun=true
  • Loading branch information
jtnord committed Nov 30, 2011
2 parents 418a96c + fc205ef commit a1ddf1d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ bin

# Build output
target
/work
Expand Up @@ -26,9 +26,14 @@
import hudson.maven.MavenModule;
import hudson.maven.MavenModuleSet;
import hudson.model.Hudson;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.PermalinkProjectAction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -39,6 +44,9 @@

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.maven.shared.release.versions.DefaultVersionInfo;
import org.apache.maven.shared.release.versions.VersionParseException;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -62,6 +70,15 @@ public M2ReleaseAction(MavenModuleSet project, boolean selectCustomScmCommentPre
this.selectCustomScmCommentPrefix = selectCustomScmCommentPrefix;
this.selectAppendHudsonUsername = selectAppendHudsonUsername;
}

public List<ParameterDefinition> getParameterDefinitions() {
ParametersDefinitionProperty pdp = project.getProperty(ParametersDefinitionProperty.class);
List<ParameterDefinition> pds = Collections.emptyList();
if(pdp != null){
pds = pdp.getParameterDefinitions();
}
return pds;
}

public List<Permalink> getPermalinks() {
return PERMALINKS;
Expand Down Expand Up @@ -182,9 +199,28 @@ public void doSubmit(StaplerRequest req, StaplerResponse resp) throws IOExceptio
// this will throw an exception so control will terminate if the dev version is not a "SNAPSHOT".
enforceDeveloperVersion(developmentVersion);

// get the normal job parameters (adapted from hudson.model.ParametersDefinitionProperty._doBuild(StaplerRequest, StaplerResponse))
List<ParameterValue> values = new ArrayList<ParameterValue>();
JSONObject formData = req.getSubmittedForm();
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
for (Object o : a) {
JSONObject jo = (JSONObject) o;
if(jo != null && !jo.isNullObject()){
String name = jo.optString("name");
if(name != null){
ParameterDefinition d = getParameterDefinition(name);
if(d==null) {
throw new IllegalArgumentException("No such parameter definition: " + name);
}
ParameterValue parameterValue = d.createValue(req, jo);
values.add(parameterValue);
}
}
}

// schedule release build
synchronized (project) {
if (project.scheduleBuild(0, new ReleaseCause())) {
if (project.scheduleBuild(0, new ReleaseCause(), new ParametersAction(values))) {
m2Wrapper.enableRelease();
m2Wrapper.setReleaseVersion(releaseVersion);
m2Wrapper.setDevelopmentVersion(developmentVersion);
Expand All @@ -208,6 +244,18 @@ public void doSubmit(StaplerRequest req, StaplerResponse resp) throws IOExceptio
}
}

/**
* Gets the {@link ParameterDefinition} of the given name, if any.
*/
public ParameterDefinition getParameterDefinition(String name) {
for (ParameterDefinition pd : getParameterDefinitions()) {
if (pd.getName().equals(name)) {
return pd;
}
}
return null;
}

/**
* returns the value of the key as a String. if multiple values
* have been submitted, the first one will be returned.
Expand Down
Expand Up @@ -17,7 +17,7 @@
<f:entry title="Append Hudson Build Number">
<f:checkbox name="appendHudsonBuildNumber" checked="false"/>
</f:entry>

<f:optionalBlock name="specifyScmCredentials" title="Specify SCM login/password" checked="false">
<f:entry title="Username">
<f:textbox name="scmUsername" value="" />
Expand Down Expand Up @@ -51,10 +51,21 @@
</f:optionalBlock>
</f:section>
</j:if>
<tr><td colspan="4" align="right">
<f:submit value="${%Schedule Maven Release Build}"/>
</td></tr>
</f:section>

<j:if test="${!empty(it.parameterDefinitions)}">
<f:section title="${%Job properties}">
<j:forEach var="parameterDefinition" items="${it.parameterDefinitions}">
<tbody>
<st:include it="${parameterDefinition}" page="${parameterDefinition.descriptor.valuePage}" />
</tbody>
</j:forEach>
</f:section>
</j:if>

<tr><td colspan="4" align="right">
<f:submit value="${%Schedule Maven Release Build}"/>
</td></tr>

</f:form>

Expand Down

0 comments on commit a1ddf1d

Please sign in to comment.