Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-68822 added support for removing all builds with LogRotator #9405

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/src/main/java/hudson/tasks/ArtifactArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
if (bd instanceof LogRotator) {
LogRotator lr = (LogRotator) bd;
if (lr.getArtifactNumToKeep() == -1) {
p.setBuildDiscarder(new LogRotator(lr.getDaysToKeep(), lr.getNumToKeep(), lr.getArtifactDaysToKeep(), 1));
LogRotator newLr = new LogRotator(lr.getDaysToKeep(), lr.getNumToKeep(), lr.getArtifactDaysToKeep(), 1);
newLr.setRemoveLastBuild(lr.isRemoveLastBuild());
p.setBuildDiscarder(newLr);
} else {
LOG.log(Level.WARNING, "will not clobber artifactNumToKeep={0} in {1}", new Object[] {lr.getArtifactNumToKeep(), p});
}
Expand Down
22 changes: 19 additions & 3 deletions core/src/main/java/hudson/tasks/LogRotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import jenkins.util.io.CompositeIOException;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Default implementation of {@link BuildDiscarder}.
Expand Down Expand Up @@ -108,6 +109,12 @@ public CollatedLogRotatorException(String msg, Collection<Exception> values) {
*/
private final Integer artifactNumToKeep;

/**
* If enabled also remove last successful build.
* @since TODO
*/
private boolean removeLastBuild;

@DataBoundConstructor
public LogRotator(String daysToKeepStr, String numToKeepStr, String artifactDaysToKeepStr, String artifactNumToKeepStr) {
this (parse(daysToKeepStr), parse(numToKeepStr),
Expand Down Expand Up @@ -140,6 +147,11 @@ public LogRotator(int daysToKeep, int numToKeep, int artifactDaysToKeep, int art

}

@DataBoundSetter
public void setRemoveLastBuild(boolean removeLastBuild) {
this.removeLastBuild = removeLastBuild;
}

@Override
@SuppressWarnings("rawtypes")
public void perform(Job<?, ?> job) throws IOException, InterruptedException {
Expand All @@ -148,9 +160,9 @@ public void perform(Job<?, ?> job) throws IOException, InterruptedException {

LOGGER.log(FINE, "Running the log rotation for {0} with numToKeep={1} daysToKeep={2} artifactNumToKeep={3} artifactDaysToKeep={4}", new Object[] {job, numToKeep, daysToKeep, artifactNumToKeep, artifactDaysToKeep});

// always keep the last successful and the last stable builds
Run lsb = job.getLastSuccessfulBuild();
Run lstb = job.getLastStableBuild();
// if configured keep the last successful and the last stable builds
Run lsb = removeLastBuild ? null : job.getLastSuccessfulBuild();
Run lstb = removeLastBuild ? null : job.getLastStableBuild();

if (numToKeep != -1) {
// Note that RunList.size is deprecated, and indeed here we are loading all the builds of the job.
Expand Down Expand Up @@ -270,6 +282,10 @@ public int getArtifactNumToKeep() {
return unbox(artifactNumToKeep);
}

public boolean isRemoveLastBuild() {
return removeLastBuild;
}

public String getDaysToKeepStr() {
return toString(daysToKeep);
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/resources/hudson/tasks/LogRotator/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ THE SOFTWARE.
description="${%if not empty, only up to this number of builds have their artifacts retained}" field="artifactNumToKeepStr">
<f:number clazz="positive-number" min="1" step="1" />
</f:entry>
<f:entry title="${%Remove last build}"
field="removeLastBuild">
<f:booleanRadio />
</f:entry>
</f:advanced>
</j:jelly>
37 changes: 34 additions & 3 deletions test/src/test/java/hudson/tasks/LogRotatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class LogRotatorTest {
@Test
public void successVsFailure() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.setLogRotator(new LogRotator(-1, 2, -1, -1));
project.setBuildDiscarder(new LogRotator(-1, 2, -1, -1));
j.buildAndAssertSuccess(project); // #1
project.getBuildersList().replaceBy(Set.of(new FailureBuilder()));
j.buildAndAssertStatus(Result.FAILURE, project); // #2
Expand All @@ -82,11 +82,25 @@ public void successVsFailure() throws Exception {
assertEquals(3, numberOf(project.getLastFailedBuild()));
}

@Test
public void successVsFailureWithRemoveLastBuild() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
LogRotator logRotator = new LogRotator(-1, 1, -1, -1);
logRotator.setRemoveLastBuild(true);
project.setBuildDiscarder(logRotator);
project.getPublishersList().replaceBy(Set.of(new TestsFail()));
j.buildAndAssertStatus(Result.UNSTABLE, project); // #1
project.getBuildersList().replaceBy(Set.of(new FailureBuilder()));
j.buildAndAssertStatus(Result.FAILURE, project); // #2
assertNull(project.getBuildByNumber(1));
assertEquals(2, numberOf(project.getLastFailedBuild()));
}

@Test
@Issue("JENKINS-2417")
public void stableVsUnstable() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.setLogRotator(new LogRotator(-1, 2, -1, -1));
project.setBuildDiscarder(new LogRotator(-1, 2, -1, -1));
j.buildAndAssertSuccess(project); // #1
project.getPublishersList().replaceBy(Set.of(new TestsFail()));
j.buildAndAssertStatus(Result.UNSTABLE, project); // #2
Expand All @@ -98,11 +112,28 @@ public void stableVsUnstable() throws Exception {
assertNull(project.getBuildByNumber(2));
}

@Test
public void stableVsUnstableWithRemoveLastBuild() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
LogRotator logRotator = new LogRotator(-1, 1, -1, -1);
logRotator.setRemoveLastBuild(true);
project.setBuildDiscarder(logRotator);
j.buildAndAssertSuccess(project); // #1
project.getPublishersList().replaceBy(Set.of(new TestsFail()));
j.buildAndAssertStatus(Result.UNSTABLE, project); // #2
project.getBuildersList().replaceBy(Set.of(new FailureBuilder()));
j.buildAndAssertStatus(Result.FAILURE, project); // #3
assertNull(project.getBuildByNumber(1));
assertNull(project.getBuildByNumber(2));
assertEquals(-1, numberOf(project.getLastSuccessfulBuild()));
assertEquals(3, numberOf(project.getLastBuild()));
}

@Test
@Issue("JENKINS-834")
public void artifactDelete() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
project.setLogRotator(new LogRotator(-1, 6, -1, 2));
project.setBuildDiscarder(new LogRotator(-1, 6, -1, 2));
project.getPublishersList().replaceBy(Set.of(new ArtifactArchiver("f", "", true, false)));
j.buildAndAssertStatus(Result.FAILURE, project); // #1
assertFalse(project.getBuildByNumber(1).getHasArtifacts());
Expand Down
Loading