Skip to content

Commit 5b556ee

Browse files
authored
Merge pull request #140 from jglick/cacheRootDir
Allow `$JENKINS_HOME/global-libraries-cache/` to be overridden
2 parents a270125 + 9af7214 commit 5b556ee

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
239239
boolean retrieve = false;
240240
switch (getCacheStatus(cachingConfiguration, versionCacheDir)) {
241241
case VALID:
242-
listener.getLogger().println("Library " + libraryLogString + " is cached. Copying from home.");
242+
listener.getLogger().println("Library " + libraryLogString + " is cached. Copying from cache.");
243243
break;
244244
case EMPTY:
245245
listener.getLogger().println("Library " + libraryLogString + " should have been cached but is empty, re-caching.");
@@ -285,7 +285,7 @@ static List<URL> retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriev
285285
retrieveLock.writeLock().unlock();
286286
}
287287
} else {
288-
listener.getLogger().println("Library " + libraryLogString + " is cached. Copying from home.");
288+
listener.getLogger().println("Library " + libraryLogString + " is cached. Copying from cache.");
289289
}
290290

291291
lastReadFile.touch(System.currentTimeMillis());

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryCachingConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import hudson.Extension;
44
import hudson.FilePath;
5-
import hudson.RestrictedSince;
65
import hudson.model.AbstractDescribableImpl;
76
import hudson.model.Descriptor;
87
import hudson.util.FormValidation;
8+
import java.io.File;
99
import jenkins.model.Jenkins;
10+
import jenkins.util.SystemProperties;
1011
import org.kohsuke.accmod.Restricted;
1112
import org.kohsuke.accmod.restrictions.NoExternalUse;
1213
import org.kohsuke.stapler.DataBoundConstructor;
@@ -36,7 +37,7 @@ public final class LibraryCachingConfiguration extends AbstractDescribableImpl<L
3637
private String includedVersionsStr;
3738

3839
private static final String VERSIONS_SEPARATOR = " ";
39-
public static final String GLOBAL_LIBRARIES_DIR = "global-libraries-cache";
40+
private static final String GLOBAL_LIBRARIES_DIR = "global-libraries-cache";
4041
public static final String LAST_READ_FILE = "last_read";
4142

4243
@DataBoundConstructor public LibraryCachingConfiguration(int refreshTimeMinutes, String excludedVersionsStr) {
@@ -134,8 +135,9 @@ public Boolean isIncluded(String version) {
134135
}
135136

136137
public static FilePath getGlobalLibrariesCacheDir() {
137-
Jenkins jenkins = Jenkins.get();
138-
return new FilePath(jenkins.getRootPath(), LibraryCachingConfiguration.GLOBAL_LIBRARIES_DIR);
138+
String cacheRootDirOverride = SystemProperties.getString(LibraryCachingConfiguration.class.getName() + ".cacheRootDir");
139+
File cacheRootDir = cacheRootDirOverride != null ? new File(cacheRootDirOverride) : new File(Jenkins.get().getRootDir(), GLOBAL_LIBRARIES_DIR);
140+
return new FilePath(cacheRootDir);
139141
}
140142

141143
@Extension public static class DescriptorImpl extends Descriptor<LibraryCachingConfiguration> {

src/test/java/org/jenkinsci/plugins/workflow/libs/LibraryAdderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public void parallelBuildsDontInterfereWithExpiredCache() throws Throwable {
505505
r.assertLogContains("global library root", b);
506506
// Second build should succeed and cache the global library at the root level
507507
b = r.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));
508-
r.assertLogContains("Library global@master is cached. Copying from home.", b);
508+
r.assertLogContains("Library global@master is cached. Copying from cache.", b);
509509
r.assertLogContains("global library root", b);
510510
// Simulate an error which leaves the cache directory empty
511511
FilePath globalCacheDir = LibraryCachingConfiguration.getGlobalLibrariesCacheDir();
@@ -523,7 +523,7 @@ public void parallelBuildsDontInterfereWithExpiredCache() throws Throwable {
523523
r.assertLogContains("global library root", b);
524524
// Third build should succeed and cache the global library at the root level again
525525
b = r.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));
526-
r.assertLogContains("Library global@master is cached. Copying from home.", b);
526+
r.assertLogContains("Library global@master is cached. Copying from cache.", b);
527527
r.assertLogContains("global library root", b);
528528
}
529529
// Change the library path to lib1 - build should succeed and cache the global library at the lib1 level

src/test/java/org/jenkinsci/plugins/workflow/libs/ResourceStepTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class ResourceStepTest {
8383

8484
WorkflowRun secondBuild = r.buildAndAssertSuccess(p);
8585
r.assertLogContains("got fixed contents", secondBuild);
86-
r.assertLogContains("Library stuff@master is cached. Copying from home.", secondBuild);
86+
r.assertLogContains("Library stuff@master is cached. Copying from cache.", secondBuild);
8787
r.assertLogNotContains("git", secondBuild); // git is not called
8888
}
8989

@@ -127,7 +127,7 @@ public class ResourceStepTest {
127127
modifyCacheTimestamp("stuff", "master", System.currentTimeMillis() - 60000 * 55); // 55 minutes have passed, still cached
128128
WorkflowRun secondBuild = r.buildAndAssertSuccess(p);
129129
r.assertLogContains("got fixed contents", secondBuild);
130-
r.assertLogContains("Library stuff@master is cached. Copying from home.", secondBuild);
130+
r.assertLogContains("Library stuff@master is cached. Copying from cache.", secondBuild);
131131
r.assertLogNotContains("git", secondBuild); // git is not called
132132

133133
modifyCacheTimestamp("stuff", "master", System.currentTimeMillis() - 60000 * 61); // 61 minutes have passed, due for a refresh

0 commit comments

Comments
 (0)