-
Notifications
You must be signed in to change notification settings - Fork 161
/
ZipStepExecution.java
149 lines (137 loc) · 6.12 KB
/
ZipStepExecution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 CloudBees Inc.
*
* 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.
*/
package org.jenkinsci.plugins.pipeline.utility.steps.zip;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.io.Archiver;
import hudson.util.io.ArchiverFactory;
import jenkins.MasterToSlaveFileCallable;
import jenkins.util.BuildListenerAdapter;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Execution of {@link ZipStep}.
*
* @author Robert Sandell <rsandell@cloudbees.com>.
*/
public class ZipStepExecution extends SynchronousNonBlockingStepExecution<Void> {
private static final long serialVersionUID = 1L;
private transient ZipStep step;
protected ZipStepExecution(@Nonnull ZipStep step, @Nonnull StepContext context) {
super(context);
this.step = step;
}
@Override
protected Void run() throws Exception {
TaskListener listener = getContext().get(TaskListener.class);
assert listener != null;
FilePath ws = getContext().get(FilePath.class);
assert ws != null;
FilePath source = ws;
if (!StringUtils.isBlank(step.getDir())) {
source = ws.child(step.getDir());
if (!source.exists()) {
throw new IOException(source.getRemote() + " does not exist.");
} else if (!source.isDirectory()) {
throw new IOException(source.getRemote() + " is not a directory.");
}
}
FilePath destination = ws.child(step.getZipFile());
if (destination.exists()) {
throw new IOException(destination.getRemote() + " exists.");
}
if (StringUtils.isBlank(step.getGlob())) {
listener.getLogger().println("Writing zip file of " + source.getRemote() + " to " + destination.getRemote());
} else {
listener.getLogger().println("Writing zip file of " + source.getRemote()
+ " filtered by [" + step.getGlob() + "] to " + destination.getRemote());
}
int count = source.act(new ZipItFileCallable(destination, step.getGlob()));
listener.getLogger().println("Zipped " + count + " entries.");
if (step.isArchive()) {
Run build = getContext().get(Run.class);
if (build == null) {
throw new MissingContextVariableException(Run.class);
}
Launcher launcher = getContext().get(Launcher.class);
if (launcher == null) {
throw new MissingContextVariableException(Launcher.class);
}
listener.getLogger().println("Archiving " + destination.getRemote());
Map<String, String> files = new HashMap<String, String>();
String s = step.getZipFile().replace('\\', '/');
files.put(s, s);
build.pickArtifactManager().archive(ws, launcher, new BuildListenerAdapter(listener), files);
}
return null;
}
/**
* Performs the actual zip operation on the slave where the source dir is located.
*
* This is a more direct implementation because {@link FilePath#zip(FilePath)}
* will include the source dir as a base path in the zip file while this implementation doesn't.
*/
static class ZipItFileCallable extends MasterToSlaveFileCallable<Integer> {
final FilePath zipFile;
final String glob;
public ZipItFileCallable(FilePath zipFile, String glob) {
this.zipFile = zipFile;
this.glob = StringUtils.isBlank(glob) ? "**/*" : glob;
}
@Override
public Integer invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException {
String canonicalZip = new File(zipFile.getRemote()).getCanonicalPath();
Archiver archiver = ArchiverFactory.ZIP.create(zipFile.write());
FileSet fs = Util.createFileSet(dir, glob);
DirectoryScanner scanner = fs.getDirectoryScanner(new org.apache.tools.ant.Project());
try {
for (String path : scanner.getIncludedFiles()) {
File toArchive = new File(dir, path).getCanonicalFile();
if (!toArchive.getPath().equals(canonicalZip)) {
archiver.visit(toArchive, path);
}
}
} finally {
archiver.close();
}
return archiver.countEntries();
}
}
}