Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
JENKINS-36981: keep release builds
Browse files Browse the repository at this point in the history
Added locking feature for successful builds.
  • Loading branch information
shillner committed Nov 2, 2016
1 parent 4b0c458 commit 9e24216
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
Expand Up @@ -57,11 +57,14 @@
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.queue.Tasks;
import hudson.security.ACL;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.ListBoxModel;
import hudson.util.RunList;
import hudson.util.Secret;
import net.sf.json.JSONObject;

Expand All @@ -88,12 +91,13 @@ public class UnleashMavenBuildWrapper extends BuildWrapper {
private boolean preselectCommitBeforeTagging = DescriptorImpl.DEFAULT_PRESELECT_COMMIT_BEFORE_TAGGING;
private String workflowPath = DescriptorImpl.DEFAULT_WORKFLOW_PATH;
private String credentialsId;
private int numberOfBuildsToLock = DescriptorImpl.DEFAULT_NUMBER_OF_LOCKED_BUILDS;

@DataBoundConstructor
public UnleashMavenBuildWrapper(String goals, String profiles, String releaseArgs, boolean useLogTimestamps,
String tagNamePattern, String scmMessagePrefix, boolean preselectUseGlobalVersion, List<HookDescriptor> hooks,
boolean preselectAllowLocalReleaseArtifacts, boolean preselectCommitBeforeTagging, String workflowPath,
String credentialsId) {
String credentialsId, int numberOfBuildsToLock) {
super();
this.goals = goals;
this.profiles = profiles;
Expand All @@ -107,6 +111,7 @@ public UnleashMavenBuildWrapper(String goals, String profiles, String releaseArg
this.preselectCommitBeforeTagging = preselectCommitBeforeTagging;
this.workflowPath = workflowPath;
this.credentialsId = credentialsId;
this.numberOfBuildsToLock = numberOfBuildsToLock;
}

@Override
Expand Down Expand Up @@ -187,13 +192,7 @@ public void buildEnvVars(Map<String, String> env) {
build.addAction(new UnleashArgumentInterceptorAction(command.toString()));
build.addAction(new UnleashBadgeAction());

return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
// TODO maybe add an environment variable indicating a release build
env.putAll(scmEnv);
}
};
return new UnleashEnvironment(scmEnv);
}

private Map<String, String> updateCommandWithScmCredentials(AbstractBuild build, StringBuilder command) {
Expand Down Expand Up @@ -336,6 +335,62 @@ public void setCredentialsId(String credentialsId) {
this.credentialsId = credentialsId;
}

public int getNumberOfBuildsToLock() {
return this.numberOfBuildsToLock;
}

public void setNumberOfBuildsToLock(int numberOfBuildsToLock) {
this.numberOfBuildsToLock = numberOfBuildsToLock;
}

private class UnleashEnvironment extends Environment {
private Map<String, String> scmEnv;

public UnleashEnvironment(Map<String, String> scmEnv) {
this.scmEnv = scmEnv;
}

@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(this.scmEnv);
}

@Override
public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
int lockedBuilds = 0;
Result result = build.getResult();
if (result != null && result.isBetterOrEqualTo(Result.SUCCESS)) {
if (UnleashMavenBuildWrapper.this.numberOfBuildsToLock != 0) {
build.keepLog();
lockedBuilds++;
}
for (Run run : (RunList<? extends Run>) build.getProject().getBuilds()) {
if (isSuccessfulReleaseBuild(run)) {
if (UnleashMavenBuildWrapper.this.numberOfBuildsToLock < 0
|| lockedBuilds < UnleashMavenBuildWrapper.this.numberOfBuildsToLock) {
run.keepLog();
lockedBuilds++;
} else {
run.keepLog(false);
}
}
}
}
return super.tearDown(build, listener);
}

private boolean isSuccessfulReleaseBuild(Run run) {
UnleashBadgeAction badgeAction = run.getAction(UnleashBadgeAction.class);
if (badgeAction != null && !run.isBuilding()) {
Result result = run.getResult();
if (result != null && result.isBetterOrEqualTo(Result.SUCCESS)) {
return true;
}
}
return false;
}
}

@Extension
public static class DescriptorImpl extends BuildWrapperDescriptor {
public static final String DEFAULT_GOALS = "unleash:perform";
Expand All @@ -348,6 +403,7 @@ public static class DescriptorImpl extends BuildWrapperDescriptor {
public static final boolean DEFAULT_PRESELECT_ALLOW_LOCAL_RELEASE_ARTIFACTS = true;
public static final boolean DEFAULT_PRESELECT_COMMIT_BEFORE_TAGGING = false;
public static final String DEFAULT_WORKFLOW_PATH = "";
public static final int DEFAULT_NUMBER_OF_LOCKED_BUILDS = 1;

private static final CredentialsMatcher CREDENTIALS_MATCHER = CredentialsMatchers.anyOf(
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class),
Expand Down
Expand Up @@ -38,6 +38,9 @@
</table>
</f:repeatable>
</f:entry>
<f:entry title="Lock the n latest successful release builds" help="/plugin/unleash/help-projectConfig-lockReleaseBuilds.html">
<f:textbox name="numberOfBuildsToLock" value="${instance.numberOfBuildsToLock}" default="${descriptor.DEFAULT_NUMBER_OF_LOCKED_BUILDS}" />
</f:entry>
<f:entry title="Use Log Timestamps" help="/plugin/unleash/help-projectConfig-logTimestamps.html">
<f:checkbox field="useLogTimestamps" value="${instance.useLogTimestamps}" default="${descriptor.useLogTimestamps}" />
</f:entry>
Expand Down
8 changes: 8 additions & 0 deletions src/main/webapp/help-projectConfig-lockReleaseBuilds.html
@@ -0,0 +1,8 @@
<div>
Set the number of successful release builds that shall be locked in the build history.<br>
<ul>
<li>Setting <tt>0</tt> won't lock any of the release builds so that the history may not contain the latest release.</li>
<li>Any value below <tt>0</tt>, f.i. <tt>-1</tt> will lock all release builds.</li>
<li>Any positive value determines a concrete number of release builds to lock.</li>
</ul>
</div>

0 comments on commit 9e24216

Please sign in to comment.