Skip to content

Commit 31ebb1f

Browse files
committed
[JENKINS-42487] Exclude git repository from being polled for changes.
1 parent f2b45cb commit 31ebb1f

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import hudson.plugins.git.extensions.impl.PathRestriction;
3030
import hudson.plugins.git.extensions.impl.LocalBranch;
3131
import hudson.plugins.git.extensions.impl.PreBuildMerge;
32+
import hudson.plugins.git.extensions.impl.PollExclusion;
3233
import hudson.plugins.git.opt.PreBuildMergeOptions;
3334
import hudson.plugins.git.util.Build;
3435
import hudson.plugins.git.util.*;
@@ -597,9 +598,17 @@ public PollingResult compareRemoteRevisionWith(Job<?, ?> project, Launcher launc
597598
private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher launcher, FilePath workspace, final TaskListener listener) throws IOException, InterruptedException {
598599
// Poll for changes. Are there any unbuilt revisions that Hudson ought to build ?
599600

601+
if (getExtensions().get(PollExclusion.class) != null) {
602+
for (UserRemoteConfig userRemoteConfig: userRemoteConfigs) {
603+
listener.getLogger().println("Polling disabled for " + userRemoteConfig.toString());
604+
}
605+
return NO_CHANGES;
606+
}
607+
600608
listener.getLogger().println("Using strategy: " + getBuildChooser().getDisplayName());
601609

602610
final Run lastBuild = project.getLastBuild();
611+
603612
if (lastBuild == null) {
604613
// If we've never been built before, well, gotta build!
605614
listener.getLogger().println("[poll] No previous build, so forcing an initial build.");
@@ -616,7 +625,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
616625
final String singleBranch = getSingleBranch(pollEnv);
617626

618627
if (!requiresWorkspaceForPolling(pollEnv)) {
619-
628+
listener.getLogger().println("Workspace: not required");
620629
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : new EnvVars();
621630

622631
GitClient git = createClient(listener, environment, project, Jenkins.getInstance(), null);
@@ -684,13 +693,16 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
684693
return NO_CHANGES;
685694
}
686695

696+
listener.getLogger().println("Workspace: required");
697+
687698
final Node node = GitUtils.workspaceToNode(workspace);
688699
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener) : project.getEnvironment(node, listener);
689700

690701
FilePath workingDirectory = workingDirectory(project,workspace,environment,listener);
691702

692703
// (Re)build if the working directory doesn't exist
693704
if (workingDirectory == null || !workingDirectory.exists()) {
705+
listener.getLogger().println("Workspace missing: Building");
694706
return BUILD_NOW;
695707
}
696708

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package hudson.plugins.git.extensions.impl;
2+
3+
import hudson.Extension;
4+
import hudson.model.TaskListener;
5+
import hudson.plugins.git.GitChangeSet;
6+
import hudson.plugins.git.GitSCM;
7+
import hudson.plugins.git.extensions.GitSCMExtension;
8+
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
9+
import hudson.plugins.git.util.BuildData;
10+
import org.jenkinsci.plugins.gitclient.GitClient;
11+
import org.kohsuke.stapler.DataBoundConstructor;
12+
13+
import static hudson.Util.*;
14+
15+
/**
16+
* {@link GitSCMExtension} that ignores all commits for the configured repository.
17+
*
18+
* @author Per Böhlin
19+
*/
20+
public class PollExclusion extends GitSCMExtension {
21+
22+
@DataBoundConstructor
23+
public PollExclusion() {
24+
}
25+
26+
@Override
27+
public boolean requiresWorkspaceForPolling() {
28+
return false;
29+
}
30+
31+
@Override
32+
public Boolean isRevExcluded(GitSCM scm, GitClient git, GitChangeSet commit, TaskListener listener, BuildData buildData) {
33+
listener.getLogger().println("Ignored commit " + commit.getCommitId() + ": Repository excluded");
34+
return true;
35+
}
36+
37+
@Extension
38+
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
39+
@Override
40+
public String getDisplayName() {
41+
return "Exclude the repository from polling all together";
42+
}
43+
}
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div>
2+
If set, and Jenkins is set to poll for changes, this repository will still
3+
not be checked for changes. It can be useful in jobs where you want to
4+
track changes and get latest commit but still don't want
5+
to trigger new builds based on those changes.
6+
<p/>
7+
This does not require a workspace.
8+
<p/>
9+
If no builds exists at all, this setting will not prevent a first build from
10+
being built.
11+
</div>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package hudson.plugins.git.extensions.impl;
2+
3+
import hudson.model.FreeStyleProject;
4+
import hudson.model.Result;
5+
import hudson.plugins.git.TestGitRepo;
6+
import hudson.plugins.git.extensions.GitSCMExtension;
7+
import hudson.plugins.git.extensions.GitSCMExtensionTest;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertTrue;
13+
14+
/**
15+
* @author Per Böhlin
16+
*/
17+
public class PollExclusionTest extends GitSCMExtensionTest{
18+
19+
FreeStyleProject project;
20+
TestGitRepo repo;
21+
22+
@Override
23+
public void before() throws Exception {
24+
repo = new TestGitRepo("repo", tmp.newFolder(), listener);
25+
project = setupBasicProject(repo);
26+
}
27+
28+
@Override
29+
protected GitSCMExtension getExtension() {
30+
return new PollExclusion();
31+
}
32+
33+
@Test
34+
public void test() throws Exception {
35+
36+
repo.commit("repo-init", repo.johnDoe, "repo0 initial commit");
37+
38+
assertTrue("Should always build when there are no builds", project.poll(listener).hasChanges());
39+
40+
build(project, Result.SUCCESS);
41+
42+
assertFalse("scm polling should never detect changes for the repository", project.poll(listener).hasChanges());
43+
44+
repo.commit("repo-init", repo.janeDoe, "second commit");
45+
46+
assertFalse("scm polling should never detect changes for the repository", project.poll(listener).hasChanges());
47+
48+
build(project, Result.SUCCESS);
49+
50+
assertFalse("scm polling should never detect changes for the repository", project.poll(listener).hasChanges());
51+
52+
}
53+
}

0 commit comments

Comments
 (0)