Skip to content

Commit

Permalink
Merge pull request #214 from andrewradamis-paay/feature/skip_save
Browse files Browse the repository at this point in the history
Add skipSave parameter
  • Loading branch information
repolevedavaj authored Jul 16, 2023
2 parents f3a56f8 + 325d5d6 commit 12d4fad
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following cache configuration options apply to all supported job types.
| Option | Mandatory | Description |
|-----------------|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `maxCacheSize` | no | The maximum size in megabytes of all configured caches that Jenkins will allow until it deletes all completely and starts the next build from an empty cache. This prevents caches from growing indefinitely with the downside of periodic fresh builds without a cache. Set to zero or empty to skip checking cache size. |
| `skipSave` | no | If set to `true`, skip saving the cache. Default `false` |
| `defaultBranch` | no | If the current branch has no cache, it will seed its cache from the specified branch. Leave empty to generate a fresh cache for each branch. |
| `caches` | yes | Defines the caches to use in the job (see below). |

Expand Down
21 changes: 18 additions & 3 deletions src/main/java/jenkins/plugins/jobcacher/CacheWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@
public class CacheWrapper extends SimpleBuildWrapper {

private Long maxCacheSize;
private boolean skipSave;
private List<Cache> caches;
private String defaultBranch;

public CacheWrapper() {
this.skipSave = false;
}

@DataBoundConstructor
Expand All @@ -82,6 +84,17 @@ public void setMaxCacheSize(Long maxCacheSize) {
this.maxCacheSize = maxCacheSize;
}

@SuppressWarnings("unused")
public boolean getSkipSave(){
return skipSave;
}

@DataBoundSetter
@SuppressWarnings("unused")
public void setSkipSave(boolean skipSave) {
this.skipSave = skipSave;
}

@SuppressWarnings("unused")
public String getDefaultBranch() {
return defaultBranch;
Expand All @@ -104,7 +117,7 @@ public void setCaches(List<Cache> caches) {
public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
List<Cache.Saver> cacheSavers = CacheManager.cache(getStorage(), build, workspace, launcher, listener, initialEnvironment, getCaches(), getDefaultBranch());

context.setDisposer(new CacheDisposer(getStorage(), getMaxCacheSize(), getCaches(), cacheSavers));
context.setDisposer(new CacheDisposer(getStorage(), getMaxCacheSize(), getSkipSave(), getCaches(), cacheSavers));
}

private static <T> List<T> wrapList(List<T> list, Function<List<T>, List<T>> listFactory) {
Expand Down Expand Up @@ -142,20 +155,22 @@ private static class CacheDisposer extends Disposer {

private final ItemStorage<?> storage;
private final Long maxCacheSize;
private final boolean skipSave;
private final List<Cache> caches;
private final List<Cache.Saver> cacheSavers;

@DataBoundConstructor
public CacheDisposer(ItemStorage<?> storage, Long maxCacheSize, List<Cache> caches, List<Cache.Saver> cacheSavers) {
public CacheDisposer(ItemStorage<?> storage, Long maxCacheSize, boolean skipSave, List<Cache> caches, List<Cache.Saver> cacheSavers) {
this.storage = storage;
this.maxCacheSize = maxCacheSize;
this.skipSave = skipSave;
this.caches = caches;
this.cacheSavers = cacheSavers;
}

@Override
public void tearDown(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
if (build.getResult() != Result.FAILURE && build.getResult() != Result.ABORTED) {
if (build.getResult() != Result.FAILURE && build.getResult() != Result.ABORTED && !skipSave) {
CacheManager.save(storage, build, workspace, launcher, listener, maxCacheSize, caches, cacheSavers);
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/jenkins/plugins/jobcacher/pipeline/CacheStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public class CacheStep extends Step {

private final List<Cache> caches;
private Long maxCacheSize;
private boolean skipSave;

public String defaultBranch;

@DataBoundConstructor
public CacheStep(Long maxCacheSize, List<Cache> caches) {
this.maxCacheSize = maxCacheSize;
this.skipSave = false;
this.caches = caches;
}

Expand All @@ -80,13 +82,24 @@ public Long getMaxCacheSize() {
return maxCacheSize;
}

@DataBoundSetter
@SuppressWarnings("unused")
public void setSkipSave(boolean skipSave) {
this.skipSave = skipSave;
}

@SuppressWarnings("unused")
public boolean getSkipSave() {
return this.skipSave;
}

public List<Cache> getCaches() {
return caches;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new CacheStepExecution(context, maxCacheSize, caches, defaultBranch);
return new CacheStepExecution(context, maxCacheSize, skipSave, caches, defaultBranch);
}

@Extension(optional = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public class CacheStepExecution extends GeneralNonBlockingStepExecution {
private static final long serialVersionUID = 1L;

private final Long maxCacheSize;
private final boolean skipSave;
private final List<Cache> caches;
private final String defaultBranch;

protected CacheStepExecution(StepContext context, Long maxCacheSize, List<Cache> caches, String defaultBranch) {
protected CacheStepExecution(StepContext context, Long maxCacheSize, boolean skipSave, List<Cache> caches, String defaultBranch) {
super(context);

this.maxCacheSize = maxCacheSize;
this.caches = caches;
this.defaultBranch = defaultBranch;
this.skipSave = skipSave;
}

@Override
Expand All @@ -50,7 +52,7 @@ private void execute() throws Exception {

context.newBodyInvoker()
.withContext(context)
.withCallback(new ExecutionCallback(maxCacheSize, caches, cacheSavers))
.withCallback(new ExecutionCallback(maxCacheSize, skipSave, caches, cacheSavers))
.start();
}

Expand All @@ -59,11 +61,13 @@ private static class ExecutionCallback extends BodyExecutionCallback {
private static final long serialVersionUID = 1L;

private final Long maxCacheSize;
private final boolean skipSave;
private final List<Cache> caches;
private final List<Cache.Saver> cacheSavers;

public ExecutionCallback(Long maxCacheSize, List<Cache> caches, List<Cache.Saver> cacheSavers) {
public ExecutionCallback(Long maxCacheSize, boolean skipSave, List<Cache> caches, List<Cache.Saver> cacheSavers) {
this.maxCacheSize = maxCacheSize;
this.skipSave = skipSave;
this.caches = caches;
this.cacheSavers = cacheSavers;
}
Expand All @@ -85,10 +89,14 @@ public void onFailure(StepContext context, Throwable t) {
}

private void complete(StepContext context) throws IOException, InterruptedException {
TaskListener listener = context.get(TaskListener.class);
if(skipSave) {
listener.getLogger().println("Skipping save due to skipSave being set to true.");
return;
}
Run<?, ?> run = context.get(Run.class);
FilePath workspace = context.get(FilePath.class);
Launcher launcher = context.get(Launcher.class);
TaskListener listener = context.get(TaskListener.class);

CacheManager.save(GlobalItemStorage.get().getStorage(), run, workspace, launcher, listener, maxCacheSize, caches, cacheSavers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
help="/plugin/jobcacher/help-maximumCacheSize.html">
<f:textbox/>
</f:entry>
<f:entry title="${%Skip Save}" field="skipSave"
help="/plugin/jobcacher/help-skipSave.html">
<f:checkbox default="false"/>
</f:entry>
<f:entry title="${%Branch to use for default cache}" field="defaultBranch"
help="/plugin/jobcacher/help-defaultBranch.html">
<f:textbox/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
help="/plugin/jobcacher/help-maximumCacheSize.html">
<f:textbox/>
</f:entry>
<f:entry title="${%Skip Save}" field="skipSave"
help="/plugin/jobcacher/help-skipSave.html">
<f:checkbox default="false"/>
</f:entry>
<f:entry title="${%Branch to use for default cache}" field="defaultBranch"
help="/plugin/jobcacher/help-defaultBranch.html">
<f:textbox/>
Expand Down
27 changes: 27 additions & 0 deletions src/main/webapp/help-skipSave.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
- The MIT License
-
- Copyright (c) 2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-->

<div>
If set to `true`, skip saving the cache. Default `false`
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public void testBackupAndRestore() throws Exception {
j.assertLogContains("Skip restoring cache as no up-to-date cache exists", result);
j.assertLogContains("Creating cache...", result);

// GIVEN
job.setDefinition(new CpsFlowDefinition("node {\n" +
" sh 'rm -rf *'\n" +
" cache(skipSave: true, maxCacheSize: 250, caches: [arbitraryFileCache(path: 'sub', compressionMethod: 'TARGZ')]){\n" +
" sh 'rm sub/file'\n" +
" }\n" +
"}", true));

// WHEN
result = job.scheduleBuild2(0).waitForStart();
j.waitForCompletion(result);

// THEN
j.assertBuildStatusSuccess(result);
j.assertLogContains("Skipping save due to skipSave being set to true.", result);

// GIVEN
job.setDefinition(new CpsFlowDefinition("node {\n" +
" sh 'rm -rf *'\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private FreeStyleProject createProjectWithFullyConfiguredArbitraryFileCache(Stri
CacheWrapper cacheWrapper = new CacheWrapper(Collections.singletonList(cache));
cacheWrapper.setMaxCacheSize(999L);
cacheWrapper.setDefaultBranch("develop");
cacheWrapper.setSkipSave(false);

FreeStyleProject project = jenkins.createProject(FreeStyleProject.class, name);
project.setDescription("description");
Expand Down

0 comments on commit 12d4fad

Please sign in to comment.