-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
PostBuildScript.java
338 lines (285 loc) · 12.1 KB
/
PostBuildScript.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package org.jenkinsci.plugins.postbuildscript;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.matrix.*;
import hudson.maven.MavenModuleSet;
import hudson.model.*;
import hudson.tasks.*;
import org.jenkinsci.plugins.postbuildscript.service.ScriptExecutor;
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Gregory Boissinot
*/
public class PostBuildScript extends Notifier implements MatrixAggregatable {
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}
@Deprecated
private transient List<GenericScript> genericScriptList = new ArrayList<GenericScript>();
@SuppressWarnings("deprecation")
private transient List<GroovyScript> groovyScriptList = new ArrayList<GroovyScript>();
private List<GenericScript> genericScriptFileList = new ArrayList<GenericScript>();
private List<GroovyScriptFile> groovyScriptFileList = new ArrayList<GroovyScriptFile>();
private List<GroovyScriptContent> groovyScriptContentList = new ArrayList<GroovyScriptContent>();
private List<BuildStep> buildSteps;
private boolean scriptOnlyIfSuccess;
private boolean scriptOnlyIfFailure;
@DataBoundConstructor
public PostBuildScript(List<GenericScript> genericScriptFile,
List<GroovyScriptFile> groovyScriptFile,
List<GroovyScriptContent> groovyScriptContent,
boolean scriptOnlyIfSuccess,
boolean scriptOnlyIfFailure,
List<BuildStep> buildStep) {
this.genericScriptFileList = genericScriptFile;
this.groovyScriptFileList = groovyScriptFile;
this.groovyScriptContentList = groovyScriptContent;
this.buildSteps = buildStep;
this.scriptOnlyIfSuccess = scriptOnlyIfSuccess;
this.scriptOnlyIfFailure = scriptOnlyIfFailure;
}
public MatrixAggregator createAggregator(MatrixBuild build, Launcher launcher, BuildListener listener) {
return new MatrixAggregator(build, launcher, listener) {
@Override
public boolean endBuild() throws InterruptedException, IOException {
return _perform(build, launcher, listener);
}
};
}
@Override
public boolean perform(AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException {
if (!(build.getProject() instanceof MatrixConfiguration)) {
return _perform(build, launcher, listener);
}
return true;
}
private boolean _perform(AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException {
listener.getLogger().println("[PostBuildScript] - Execution post build scripts.");
ScriptExecutor executor = new ScriptExecutor(
new PostBuildScriptLog(listener),
listener
);
try {
if (scriptOnlyIfSuccess && build.getResult().isWorseThan(Result.SUCCESS)) {
listener.getLogger().println("[PostBuildScript] Build is not success : do not execute script");
return true;
} else if (scriptOnlyIfFailure && build.getResult().isBetterThan(Result.FAILURE)) {
listener.getLogger().println("[PostBuildScript] Build is not failure : do not execute script");
return true;
} else {
return processScripts(executor, build, launcher, listener);
}
} catch (PostBuildScriptException pse) {
listener.getLogger().println("[PostBuildScript] - [Error] - Problems occurs: " + pse.getMessage());
build.setResult(Result.FAILURE);
return false;
}
}
private boolean processScripts(ScriptExecutor executor, AbstractBuild build, Launcher launcher, BuildListener listener) throws PostBuildScriptException {
//Execute Generic scripts file
if (genericScriptFileList != null) {
boolean result = processGenericScriptList(genericScriptFileList, executor, build, launcher, listener);
if (!result) {
setFailedResult(build);
return false;
}
}
//Execute Groovy scripts file
if (groovyScriptFileList != null) {
boolean result = processGroovyScriptFileList(groovyScriptFileList, executor, build, listener);
if (!result) {
setFailedResult(build);
return false;
}
}
//Execute Groovy scripts content
if (groovyScriptContentList != null) {
boolean result = processGroovyScriptContentList(build.getWorkspace(), groovyScriptContentList, executor);
if (!result) {
setFailedResult(build);
return false;
}
}
//Execute Build steps
if (buildSteps != null) {
boolean result = processBuildSteps(buildSteps, build, launcher, listener);
if (!result) {
setFailedResult(build);
return false;
}
}
return true;
}
private boolean processGenericScriptList(List<GenericScript> genericScriptFileList, ScriptExecutor executor, AbstractBuild build, Launcher launcher, BuildListener listener) throws PostBuildScriptException {
assert genericScriptFileList != null;
FilePath workspace = build.getWorkspace();
for (GenericScript script : genericScriptFileList) {
String scriptPath = getResolvedPath(script.getFilePath(), build, listener);
if (scriptPath != null) {
int cmd = executor.executeScriptPathAndGetExitCode(workspace, scriptPath, launcher);
if (cmd != 0) {
return false;
}
}
}
return true;
}
private boolean processGroovyScriptFileList(List<GroovyScriptFile> groovyScriptFileList, ScriptExecutor executor, AbstractBuild build, BuildListener listener) throws PostBuildScriptException {
assert groovyScriptFileList != null;
FilePath workspace = build.getWorkspace();
for (GroovyScriptFile groovyScript : groovyScriptFileList) {
String groovyPath = getResolvedPath(groovyScript.getFilePath(), build, listener);
if (groovyPath != null) {
if (!executor.performGroovyScriptFile(workspace, groovyPath)) {
return false;
}
}
}
return true;
}
private boolean processGroovyScriptContentList(FilePath workspace, List<GroovyScriptContent> groovyScriptContentList, ScriptExecutor executor) throws PostBuildScriptException {
assert groovyScriptContentList != null;
for (GroovyScriptContent groovyScript : groovyScriptContentList) {
String content = groovyScript.getContent();
if (content != null) {
if (!executor.performGroovyScript(workspace, content)) {
return false;
}
}
}
return true;
}
private boolean processBuildSteps(List<BuildStep> buildSteps, AbstractBuild build, Launcher launcher, BuildListener listener) throws PostBuildScriptException {
try {
for (BuildStep bs : buildSteps) {
if (!bs.perform(build, launcher, listener)) {
setFailedResult(build);
return false;
}
}
return true;
} catch (IOException ioe) {
throw new PostBuildScriptException(ioe);
} catch (InterruptedException ie) {
throw new PostBuildScriptException(ie);
}
}
private String getResolvedPath(String path, AbstractBuild build, BuildListener listener) throws PostBuildScriptException {
if (path == null) {
return null;
}
String resolvedPath;
try {
resolvedPath = Util.replaceMacro(path, build.getEnvironment(listener));
resolvedPath = Util.replaceMacro(resolvedPath, build.getBuildVariables());
return resolvedPath;
} catch (IOException ioe) {
throw new PostBuildScriptException(ioe);
} catch (InterruptedException ie) {
throw new PostBuildScriptException(ie);
}
}
private void setFailedResult(AbstractBuild build) {
build.setResult(Result.FAILURE);
}
@Deprecated
@SuppressWarnings({"unused", "deprecation"})
public List<GenericScript> getGenericScriptList() {
return genericScriptList;
}
@Deprecated
@SuppressWarnings({"unused", "deprecation"})
public List<GroovyScript> getGroovyScriptList() {
return groovyScriptList;
}
@SuppressWarnings("unused")
public List<GenericScript> getGenericScriptFileList() {
return genericScriptFileList;
}
@SuppressWarnings("unused")
public List<GroovyScriptFile> getGroovyScriptFileList() {
return groovyScriptFileList;
}
@SuppressWarnings("unused")
public List<GroovyScriptContent> getGroovyScriptContentList() {
return groovyScriptContentList;
}
@SuppressWarnings("unused")
public List<BuildStep> getBuildSteps() {
return buildSteps;
}
@SuppressWarnings("unused")
public boolean isScriptOnlyIfSuccess() {
return scriptOnlyIfSuccess;
}
@SuppressWarnings("unused")
public boolean isScriptOnlyIfFailure() {
return scriptOnlyIfFailure;
}
@Extension(ordinal = 99)
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public String getDisplayName() {
return "[PostBuildScript] - Execute a set of scripts";
}
@Override
public String getHelpFile() {
return "/plugin/postbuildscript/help.html";
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return Project.class.isAssignableFrom(jobType)
|| MatrixProject.class.isAssignableFrom(jobType)
|| MavenModuleSet.class.isAssignableFrom(jobType)
|| (Hudson.getInstance().getPlugin("ivy") != null && hudson.ivy.IvyModuleSet.class.isAssignableFrom(jobType));
}
}
@SuppressWarnings({"unused", "deprecation"})
public Object readResolve() {
if (genericScriptList != null) {
if (genericScriptFileList == null) {
genericScriptFileList = new ArrayList<GenericScript>();
}
if (buildSteps == null) {
buildSteps = new ArrayList<BuildStep>();
}
for (GenericScript script : genericScriptList) {
if (script.getContent() != null) {
if (isUnix()) {
buildSteps.add(new Shell(script.getContent()));
} else {
buildSteps.add(new BatchFile(script.getContent()));
}
} else {
genericScriptFileList.add(script);
}
}
}
if (groovyScriptList != null) {
if (groovyScriptContentList == null) {
groovyScriptContentList = new ArrayList<GroovyScriptContent>();
}
if (groovyScriptFileList == null) {
groovyScriptFileList = new ArrayList<GroovyScriptFile>();
}
for (GroovyScript groovyScript : groovyScriptList) {
if (groovyScript.getContent() != null) {
groovyScriptContentList.add(new GroovyScriptContent(groovyScript.getContent()));
}
if (groovyScript.getFilePath() != null) {
groovyScriptFileList.add(new GroovyScriptFile(groovyScript.getFilePath()));
}
}
}
return this;
}
private boolean isUnix() {
return File.pathSeparatorChar == ':';
}
}