Skip to content

Commit 6876e63

Browse files
committed
[JENKINS-42487] Exclude git repository from being polled for changes.
1 parent 690f8a8 commit 6876e63

File tree

5 files changed

+122
-2
lines changed

5 files changed

+122
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</licenses>
1717

1818
<artifactId>git</artifactId>
19-
<version>3.9.1</version>
19+
<version>3.9.1-JENKINS-42487</version>
2020
<packaging>hpi</packaging>
2121
<name>Jenkins Git plugin</name>
2222
<description>Integrates Jenkins with GIT SCM</description>

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import hudson.plugins.git.extensions.impl.PathRestriction;
4242
import hudson.plugins.git.extensions.impl.LocalBranch;
4343
import hudson.plugins.git.extensions.impl.PreBuildMerge;
44+
import hudson.plugins.git.extensions.impl.PollExclusion;
4445
import hudson.plugins.git.opt.PreBuildMergeOptions;
4546
import hudson.plugins.git.util.Build;
4647
import hudson.plugins.git.util.*;
@@ -665,9 +666,17 @@ public PollingResult compareRemoteRevisionWith(Job<?, ?> project, Launcher launc
665666
private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher launcher, FilePath workspace, final @NonNull TaskListener listener) throws IOException, InterruptedException {
666667
// Poll for changes. Are there any unbuilt revisions that Hudson ought to build ?
667668

669+
if (getExtensions().get(PollExclusion.class) != null) {
670+
for (UserRemoteConfig userRemoteConfig: userRemoteConfigs) {
671+
listener.getLogger().println("Polling disabled for " + userRemoteConfig.toString());
672+
}
673+
return NO_CHANGES;
674+
}
675+
668676
listener.getLogger().println("Using strategy: " + getBuildChooser().getDisplayName());
669677

670678
final Run lastBuild = project.getLastBuild();
679+
671680
if (lastBuild == null) {
672681
// If we've never been built before, well, gotta build!
673682
listener.getLogger().println("[poll] No previous build, so forcing an initial build.");
@@ -684,7 +693,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
684693
final String singleBranch = getSingleBranch(pollEnv);
685694

686695
if (!requiresWorkspaceForPolling(pollEnv)) {
687-
696+
listener.getLogger().println("Workspace: not required");
688697
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : new EnvVars();
689698

690699
GitClient git = createClient(listener, environment, project, Jenkins.getInstance(), null);
@@ -752,13 +761,16 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
752761
return NO_CHANGES;
753762
}
754763

764+
listener.getLogger().println("Workspace: required");
765+
755766
final Node node = GitUtils.workspaceToNode(workspace);
756767
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener) : project.getEnvironment(node, listener);
757768

758769
FilePath workingDirectory = workingDirectory(project,workspace,environment,listener);
759770

760771
// (Re)build if the working directory doesn't exist
761772
if (workingDirectory == null || !workingDirectory.exists()) {
773+
listener.getLogger().println("Workspace missing: Building");
762774
return BUILD_NOW;
763775
}
764776

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)