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

Job.BuildNumberAssigner #9846

Merged
merged 2 commits into from
Nov 2, 2024
Merged
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
41 changes: 36 additions & 5 deletions core/src/main/java/hudson/model/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import hudson.BulkChange;
import hudson.EnvVars;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.FeedAdapter;
import hudson.PermalinkList;
Expand Down Expand Up @@ -346,12 +347,42 @@ public boolean isKeepDependencies() {
}

/**
* Allocates a new buildCommand number.
* Allocates a new build number.
* @see BuildNumberAssigner
*/
public synchronized int assignBuildNumber() throws IOException {
int r = nextBuildNumber++;
saveNextBuildNumber();
return r;
public int assignBuildNumber() throws IOException {
return ExtensionList.lookupFirst(BuildNumberAssigner.class).assignBuildNumber(this, this::saveNextBuildNumber);
}

/**
* Alternate strategy for assigning build numbers.
*/
@Restricted(Beta.class)
public interface BuildNumberAssigner extends ExtensionPoint {
/**
* Implementation of {@link Job#assignBuildNumber}.
*/
int assignBuildNumber(Job<?, ?> job, SaveNextBuildNumber saveNextBuildNumber) throws IOException;
/**
* Provides an externally accessible alias for {@link Job#saveNextBuildNumber}, which is {@code protected}.
* ({@link #getNextBuildNumber} and {@link #fastUpdateNextBuildNumber} are already accessible.)
*/
interface SaveNextBuildNumber {
void call() throws IOException;
}
}

@Restricted(DoNotUse.class)
@Extension(ordinal = -1000)
public static final class DefaultBuildNumberAssigner implements BuildNumberAssigner {
@Override
public int assignBuildNumber(Job<?, ?> job, SaveNextBuildNumber saveNextBuildNumber) throws IOException {
synchronized (job) {
int r = job.nextBuildNumber++;
saveNextBuildNumber.call();
return r;
}
}
}

/**
Expand Down
Loading