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

Do not store reference to directory which should be cached #207

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
30 changes: 13 additions & 17 deletions src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,13 @@

@Override
public Saver cache(ObjectPath cachesRoot, ObjectPath fallbackCachesRoot, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
FilePath resolvedPath = resolvePath(workspace, initialEnvironment);
String expandedPath = initialEnvironment.expand(path);
FilePath resolvedPath = workspace.child(expandedPath);

ExistingCache existingCache = resolveExistingValidCache(cachesRoot, fallbackCachesRoot, workspace, listener);
if (existingCache == null) {
logMessage("Skip restoring cache as no up-to-date cache exists", listener);
return new SaverImpl(resolvedPath);
return new SaverImpl(expandedPath);
}

logMessage("Restoring cache...", listener);
Expand All @@ -191,13 +192,7 @@
resolvedPath.deleteRecursive();
}

return new SaverImpl(resolvedPath);
}

private FilePath resolvePath(FilePath workspace, EnvVars initialEnvironment) {
String expandedPath = initialEnvironment.expand(path);

return workspace.child(expandedPath);
return new SaverImpl(expandedPath);
}

private ExistingCache resolveExistingValidCache(ObjectPath cachesRoot, ObjectPath fallbackCachesRoot, FilePath workspace, TaskListener listener) throws IOException, InterruptedException {
Expand Down Expand Up @@ -326,8 +321,8 @@
}

private FilePath[] resolveCacheValidityDecidingFiles(FilePath workspace) throws IOException, InterruptedException {
List<String> includes = new ArrayList<String>();
List<String> excludes = new ArrayList<String>();
List<String> includes = new ArrayList<>();
List<String> excludes = new ArrayList<>();

for (String decidingFilePattern : cacheValidityDecidingFile.split(",")) {
if (decidingFilePattern.startsWith("!")) {
Expand All @@ -343,22 +338,23 @@

private static final long serialVersionUID = 1L;

private final FilePath resolvedPath;
private final String expandedPath;

public SaverImpl(FilePath resolvedPath) {
this.resolvedPath = resolvedPath;
public SaverImpl(String expandedPath) {
this.expandedPath = expandedPath;
}

@Override
public long calculateSize(ObjectPath objectPath, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
return resolvedPath.act(new DirectorySize(includes, excludes));
return workspace.child(expandedPath).act(new DirectorySize(includes, excludes));
}

@Override
public void save(ObjectPath cachesRoot, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
FilePath resolvedPath = workspace.child(expandedPath);
if (!resolvedPath.exists()) {
logMessage("Cannot create cache as the path does not exist", listener);
if (isPathOutsideWorkspace(workspace) && isMaybeInsideDockerContainer(workspace)) {
if (isPathOutsideWorkspace(resolvedPath, workspace) && isMaybeInsideDockerContainer(workspace)) {

Check warning on line 357 in src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 357 is only partially covered, 2 branches are missing
logMessage("Note that paths outside the workspace while using the Docker Pipeline plugin are not supported", listener);
}
return;
Expand Down Expand Up @@ -395,7 +391,7 @@
}
}

private boolean isPathOutsideWorkspace(FilePath workspace) {
private boolean isPathOutsideWorkspace(FilePath resolvedPath, FilePath workspace) {
return !StringUtils.startsWith(resolvedPath.getRemote(), workspace.getRemote());
}

Expand Down