Skip to content

Commit

Permalink
[FIXED JENKINS-12076] Add the ability to specify the number of releas…
Browse files Browse the repository at this point in the history
…e builds to keep.

If the number is -1 then all builds are kept.
a positive number will keep that many sucessfull release builds.
Any other number (< -1 or 0) will cause no release builds to be kept.
  • Loading branch information
jtnord committed Mar 1, 2012
1 parent aeabbd9 commit 82e9e2c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 51 deletions.
Expand Up @@ -100,8 +100,10 @@ public class M2ReleaseBuildWrapper extends BuildWrapper {
public boolean selectAppendHudsonUsername = DescriptorImpl.DEFAULT_SELECT_APPEND_HUDSON_USERNAME;
public boolean selectScmCredentials = DescriptorImpl.DEFAULT_SELECT_SCM_CREDENTIALS;

public int numberOfReleaseBuildsToKeep = DescriptorImpl.DEFAULT_NUMBER_OF_RELEASE_BUILDS_TO_KEEP;

@DataBoundConstructor
public M2ReleaseBuildWrapper(String releaseGoals, String dryRunGoals, boolean selectCustomScmCommentPrefix, boolean selectAppendHudsonUsername, boolean selectScmCredentials, String releaseEnvVar, String scmUserEnvVar, String scmPasswordEnvVar) {
public M2ReleaseBuildWrapper(String releaseGoals, String dryRunGoals, boolean selectCustomScmCommentPrefix, boolean selectAppendHudsonUsername, boolean selectScmCredentials, String releaseEnvVar, String scmUserEnvVar, String scmPasswordEnvVar, int numBuildsToKeep) {
super();
this.releaseGoals = releaseGoals;
this.dryRunGoals = dryRunGoals;
Expand All @@ -111,6 +113,7 @@ public M2ReleaseBuildWrapper(String releaseGoals, String dryRunGoals, boolean se
this.releaseEnvVar = releaseEnvVar;
this.scmUserEnvVar = scmUserEnvVar;
this.scmPasswordEnvVar = scmPasswordEnvVar;
this.numberOfReleaseBuildsToKeep = numBuildsToKeep;
}


Expand Down Expand Up @@ -192,19 +195,33 @@ public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild bld, BuildLi
lstnr.getLogger().println("[M2Release] its only a dryRun, no need to mark it for keep");
}

int buildsKept = 0;
if (bld.getResult() != null && bld.getResult().isBetterOrEqualTo(Result.SUCCESS) && !args.isDryRun()) {
// keep this build.
lstnr.getLogger().println("[M2Release] marking build to keep until the next release build");
bld.keepLog();

if (numberOfReleaseBuildsToKeep > 0 || numberOfReleaseBuildsToKeep == -1) {
// keep this build.
lstnr.getLogger().println("[M2Release] assigning keep build to current build.");
bld.keepLog();
buildsKept++;
}
// the value may have changed since a previous release so go searching...

for (Run run : (RunList<? extends Run>) (bld.getProject().getBuilds())) {
M2ReleaseBadgeAction a = run.getAction(M2ReleaseBadgeAction.class);
if (a != null && run.getResult() == Result.SUCCESS && !a.isDryRun()) {
if (bld.getNumber() != run.getNumber()) {
lstnr.getLogger().println(
"[M2Release] removing keep build from build " + run.getNumber());
run.keepLog(false);
break;
if (isSuccessfulReleaseBuild(run)) {
if (bld.getNumber() != run.getNumber()) { // not sure we still need this check..
if (shouldKeepBuildNumber(numberOfReleaseBuildsToKeep, buildsKept)) {
if (!run.isKeepLog()) {
lstnr.getLogger().println(
"[M2Release] assigning keep build to build " + run.getNumber());
run.keepLog(true);
}
}
else {
if (run.isKeepLog()) {
lstnr.getLogger().println(
"[M2Release] removing keep build from build " + run.getNumber());
run.keepLog(false);
}
}
}
}
}
Expand Down Expand Up @@ -243,6 +260,26 @@ public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild bld, BuildLi
}
return retVal;
}

/**
* evaluate if the specified build is a sucessful release build (not including dry runs)
* @param run the run to check
* @return <code>true</code> if this is a successful release build that is not a dry run.
*/
private boolean isSuccessfulReleaseBuild(Run run) {
M2ReleaseBadgeAction a = run.getAction(M2ReleaseBadgeAction.class);
if (a != null && !run.isBuilding() && run.getResult().isBetterOrEqualTo(Result.SUCCESS) && !a.isDryRun()) {
return true;
}
return false;
}

private boolean shouldKeepBuildNumber(int numToKeep, int numKept) {
if (numToKeep == -1) {
return true;
}
return numKept < numToKeep;
}
};
}

Expand All @@ -262,6 +299,14 @@ public boolean isSelectAppendHudsonUsername() {
public void setSelectAppendHudsonUsername(boolean selectAppendHudsonUsername) {
this.selectAppendHudsonUsername = selectAppendHudsonUsername;
}

public int getNumberOfReleaseBuildsToKeep() {
return numberOfReleaseBuildsToKeep;
}

public void setNumberOfreleaseBuildsToKeep(int numberOfReleaseBuildsToKeep) {
this.numberOfReleaseBuildsToKeep = numberOfReleaseBuildsToKeep;
}

private MavenModuleSet getModuleSet(AbstractBuild<?,?> build) {
if (build instanceof MavenBuild) {
Expand All @@ -281,11 +326,6 @@ else if (build instanceof MavenModuleSetBuild) {
}


@Override
public Action getProjectAction(@SuppressWarnings("rawtypes") AbstractProject job) {
return new M2ReleaseAction((MavenModuleSet) job, selectCustomScmCommentPrefix, selectAppendHudsonUsername, selectScmCredentials);
}

public static boolean hasReleasePermission(@SuppressWarnings("rawtypes") AbstractProject job) {
return job.hasPermission(DescriptorImpl.CREATE_RELEASE);
}
Expand All @@ -312,9 +352,30 @@ public String getReleaseGoals() {
}

public String getDryRunGoals() {
return StringUtils.isBlank(dryRunGoals) ? DescriptorImpl.DEFAULT_DRYRUN_GOALS : dryRunGoals;
}
return StringUtils.isBlank(dryRunGoals) ? DescriptorImpl.DEFAULT_DRYRUN_GOALS : dryRunGoals;
}


/**
* Evaluate if the current build should be a release build.
* @return <code>true</code> if this build is a release build.
*/
private boolean isReleaseBuild(@SuppressWarnings("rawtypes") AbstractBuild build) {
return (build.getCause(ReleaseCause.class) != null);
}


/** Recreate the logger on de-serialisation. */
private Object readResolve() {
log = LoggerFactory.getLogger(M2ReleaseBuildWrapper.class);
return this;
}

@Override
public Action getProjectAction(@SuppressWarnings("rawtypes") AbstractProject job) {
return new M2ReleaseAction((MavenModuleSet) job, selectCustomScmCommentPrefix, selectAppendHudsonUsername, selectScmCredentials);
}

/**
* Hudson defines a method {@link Builder#getDescriptor()}, which returns the corresponding
* {@link Descriptor} object. Since we know that it's actually {@link DescriptorImpl}, override the method
Expand All @@ -327,11 +388,9 @@ public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}


@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor {

//public static final PermissionGroup PERMISSIONS = new PermissionGroup(M2ReleaseBuildWrapper.class, Messages._PermissionGroup_Title());
public static final Permission CREATE_RELEASE;

static {
Expand Down Expand Up @@ -406,7 +465,9 @@ public static class DescriptorImpl extends BuildWrapperDescriptor {
public static final boolean DEFAULT_SELECT_CUSTOM_SCM_COMMENT_PREFIX = false;
public static final boolean DEFAULT_SELECT_APPEND_HUDSON_USERNAME = false;
public static final boolean DEFAULT_SELECT_SCM_CREDENTIALS = false;


public static final int DEFAULT_NUMBER_OF_RELEASE_BUILDS_TO_KEEP = 1;

private boolean nexusSupport = false;
private String nexusURL = null;
private String nexusUser = "deployment"; //$NON-NLS-1$
Expand Down Expand Up @@ -510,18 +571,4 @@ public FormValidation doUrlCheck(@QueryParameter String urlValue,
return FormValidation.ok();
}
}

/**
* Evaluate if the current build should be a release build.
* @return <code>true</code> if this build is a release build.
*/
private boolean isReleaseBuild(@SuppressWarnings("rawtypes") AbstractBuild build) {
return (build.getCause(ReleaseCause.class) != null);
}

/** Recreate the logger on de-serialisation. */
private Object readResolve() {
log = LoggerFactory.getLogger(M2ReleaseBuildWrapper.class);
return this;
}
}
Expand Up @@ -10,10 +10,14 @@
-->
<f:textbox field="releaseGoals" value="${h.defaulted(instance.releaseGoals,descriptor.DEFAULT_RELEASE_GOALS)}"/>
</f:entry>
<f:entry title="DryRun goals and options" help="/plugin/m2release/help-projectConfig-dryRunGoals.html">
<f:textbox field="dryRunGoals" value="${h.defaulted(instance.dryRunGoals,descriptor.DEFAULT_DRYRUN_GOALS)}"/>
</f:entry>

<f:entry title="DryRun goals and options" help="/plugin/m2release/help-projectConfig-dryRunGoals.html">
<f:textbox field="dryRunGoals" value="${h.defaulted(instance.dryRunGoals,descriptor.DEFAULT_DRYRUN_GOALS)}"/>
</f:entry>

<f:entry title="Number of successful release builds to keep" help="/plugin/m2release/help-projectConfig-numberOfReleaseBuildsToKeep.html">
<f:textbox field="numberOfReleaseBuildsToKeep" value="${instance.numberOfReleaseBuildsToKeep}" />
</f:entry>

This comment has been minimized.

Copy link
@imod

imod Jan 3, 2013

Member

As the number of kept releases is a rather minor informative info, I think 'numberOfReleaseBuildsToKeep' should rather go into the advanced section.


<f:entry title="Default versioning mode" help="/plugin/m2release/help-projectConfig-versioningMode.html">
<select name="defaultVersioningMode">
<f:option value="${descriptor.VERSIONING_AUTO}" selected="${descriptor.VERSIONING_AUTO == h.defaulted(instance.defaultVersioningMode,descriptor.DEFAULT_VERSIONING)}">None</f:option>
Expand All @@ -26,7 +30,7 @@
<f:checkbox name="selectCustomScmCommentPrefix" checked="${h.defaulted(instance.selectCustomScmCommentPrefix,descriptor.DEFAULT_SELECT_CUSTOM_SCM_COMMENT_PREFIX)}"/>
</f:entry>

<f:entry title="Preselect append Hudson username" help="/plugin/m2release/help-projectConfig-selectAppendHudsonUsername.html">
<f:entry title="Preselect append Jenkins username" help="/plugin/m2release/help-projectConfig-selectAppendHudsonUsername.html">
<f:checkbox name="selectAppendHudsonUsername" checked="${h.defaulted(instance.selectAppendHudsonUsername,descriptor.DEFAULT_SELECT_APPEND_HUDSON_USERNAME)}"/>
</f:entry>

Expand All @@ -35,14 +39,14 @@
</f:entry>

<f:advanced>
<f:entry title="Release envrionment variable" help="/plugin/m2release/help-projectConfig-releaseEnvVar.html">
<f:textbox field="releaseEnvVar" value="${h.defaulted(instance.releaseEnvVar,descriptor.DEFAULT_RELEASE_ENVVAR)}"/>
</f:entry>
<f:entry title="SCM username environment variable" help="/plugin/m2release/help-projectConfig-scmUserEnvVar.html">
<f:textbox field="scmUserEnvVar" value="${instance.scmUserEnvVar}"/>
</f:entry>
<f:entry title="SCM password environment variable" help="/plugin/m2release/help-projectConfig-scmPasswordEnvVar.html">
<f:textbox field="scmPasswordEnvVar" value="${instance.scmPasswordEnvVar}"/>
</f:entry>
<f:entry title="Release environment variable" help="/plugin/m2release/help-projectConfig-releaseEnvVar.html">
<f:textbox field="releaseEnvVar" value="${h.defaulted(instance.releaseEnvVar,descriptor.DEFAULT_RELEASE_ENVVAR)}"/>
</f:entry>
<f:entry title="SCM username environment variable" help="/plugin/m2release/help-projectConfig-scmUserEnvVar.html">
<f:textbox field="scmUserEnvVar" value="${instance.scmUserEnvVar}"/>
</f:entry>
<f:entry title="SCM password environment variable" help="/plugin/m2release/help-projectConfig-scmPasswordEnvVar.html">
<f:textbox field="scmPasswordEnvVar" value="${instance.scmPasswordEnvVar}"/>
</f:entry>
</f:advanced>
</j:jelly>
@@ -0,0 +1,4 @@
<div>
Specify the number of successful release builds to keep forever.
A value of <tt>-1</tt> will lock all successful release builds, <tt>0</tt> will not lock any builds.
</div>
Expand Up @@ -60,7 +60,7 @@ public MavenModuleSetBuild runPepareRelease_dryRun(String projectZip, String unp
m.setGoals("dummygoal"); // build would fail with this goal

final M2ReleaseBuildWrapper wrapper = new M2ReleaseBuildWrapper(DescriptorImpl.DEFAULT_RELEASE_GOALS, DescriptorImpl.DEFAULT_DRYRUN_GOALS, false,
false, false, "ENV", "USERENV", "PWDENV");
false, false, "ENV", "USERENV", "PWDENV", DescriptorImpl.DEFAULT_NUMBER_OF_RELEASE_BUILDS_TO_KEEP);
M2ReleaseArgumentsAction args = new M2ReleaseArgumentsAction();
args.setDevelopmentVersion("1.0-SNAPSHOT");
args.setReleaseVersion("0.9");
Expand Down

0 comments on commit 82e9e2c

Please sign in to comment.