This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
QiniuPublisher.java
266 lines (234 loc) · 7.76 KB
/
QiniuPublisher.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
package net.zouxin.lab.qiniuplugin;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.CopyOnWriteList;
import hudson.util.FormValidation;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.PutPolicy;
/**
* Sample {@link Builder}.
*
* <p>
* When the user configures the project and enables this builder,
* {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked and a new
* {@link QiniuPublisher} is created. The created instance is persisted to the
* project configuration XML by using XStream, so this allows you to use
* instance fields (like {@link #name}) to remember the configuration.
*
* <p>
* When a build is performed, the
* {@link #perform(AbstractBuild, Launcher, BuildListener)} method will be
* invoked.
*
* @author Kohsuke Kawaguchi
*/
public class QiniuPublisher extends Recorder {
private final List<QiniuEntry> entries = new ArrayList<QiniuEntry>();
public QiniuPublisher() {
super();
}
@Override
public boolean perform(AbstractBuild build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
// This is where you 'build' the project.
// Since this is a dummy, we just say 'hello world' and call that a
// build.
// This also shows how you can consult the global configuration of the
// builder
FilePath ws = build.getWorkspace();
String wsPath = ws.getRemote() + File.separator;
PrintStream logger = listener.getLogger();
Map<String, String> envVars = build.getEnvironment(listener);
final boolean buildFailed = build.getResult() == Result.FAILURE;
logger.println("开始上传到七牛...");
for (QiniuEntry entry : this.entries) {
if (entry.noUploadOnFailure && buildFailed) {
logger.println("构建失败,跳过上传");
continue;
}
QiniuProfile profile = this.getDescriptor().getProfileByName(
entry.profileName);
if (profile == null) {
logger.println("找不到配置项,跳过");
continue;
}
Mac mac = new Mac(profile.getAccessKey(), profile.getSecretKey());
// 请确保该bucket已经存在
PutPolicy putPolicyGlobal = new PutPolicy(entry.bucket);
String uptoken;
PutExtra extra = new PutExtra();
String expanded = Util.replaceMacro(entry.source, envVars);
FilePath[] paths = ws.list(expanded);
for (FilePath path : paths) {
String keyPath = path.getRemote().replace(wsPath, "");
String key = keyPath.replace(File.separator, "/");
PutPolicy putPolicy;
if (entry.noUploadOnExists) {
putPolicy = putPolicyGlobal;
} else {
putPolicy = new PutPolicy(entry.bucket);
putPolicy.scope = entry.bucket + ":" + key;
}
try {
uptoken = putPolicy.token(mac);
PutRet ret = IoApi.putFile(uptoken, key,
new File(path.getRemote()), extra);
JSONObject retJsonObject = (JSONObject) JSONSerializer
.toJSON(ret.toString());
if (ret.ok()) {
logger.println("上传 " + keyPath + " 到 " + entry.bucket
+ " 成功: " + retJsonObject.getString("key"));
} else {
logger.print("上传 " + keyPath + " 到 " + entry.bucket
+ " 失败: ");
String error = retJsonObject.getString("error");
if (error != null) {
logger.print(error);
}
logger.println();
}
logger.println(" " + ret);
} catch (Exception e) {
e.printStackTrace();
build.setResult(Result.UNSTABLE);
}
}
}
logger.println("上传到七牛成功...");
return true;
}
// Overridden for better type safety.
// If your plugin doesn't really define any property on Descriptor,
// you don't have to do this.
@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}
/**
* Descriptor for {@link QiniuPublisher}. Used as a singleton. The class is
* marked as public so that it can be accessed from views.
*
* <p>
* See
* <tt>src/main/resources/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly</tt>
* for the actual HTML fragment for the configuration screen.
*/
@Extension
// This indicates to Jenkins that this is an implementation of an extension
// point.
public static final class DescriptorImpl extends
BuildStepDescriptor<Publisher> {
/**
* To persist global configuration information, simply store it in a
* field and call save().
*
* <p>
* If you don't want fields to be persisted, use <tt>transient</tt>.
*/
private final CopyOnWriteList<QiniuProfile> profiles = new CopyOnWriteList<QiniuProfile>();
public List<QiniuProfile> getProfiles() {
return Arrays.asList(profiles.toArray(new QiniuProfile[0]));
}
public QiniuProfile getProfileByName(String profileName) {
List<QiniuProfile> profiles = this.getProfiles();
for (QiniuProfile profile : profiles) {
System.console().printf(profile.getName() + "\n");
if (profileName.equals(profile.getName())) {
return profile;
}
}
return null;
}
/**
* In order to load the persisted global configuration, you have to call
* load() in the constructor.
*/
public DescriptorImpl() {
load();
}
/**
* Performs on-the-fly validation of the form field 'name'.
*
* @param value
* This parameter receives the value that the user has typed.
* @return Indicates the outcome of the validation. This is sent to the
* browser.
* <p>
* Note that returning {@link FormValidation#error(String)} does
* not prevent the form from being saved. It just means that a
* message will be displayed to the user.
*/
public FormValidation doCheckAccessKey(@QueryParameter String value)
throws IOException, ServletException {
if (value.length() == 0)
return FormValidation.error("Access Key 不能为空");
return FormValidation.ok();
}
public FormValidation doCheckProfileName(@QueryParameter String value)
throws IOException, ServletException {
if (value.length() == 0)
return FormValidation.error("设置项名称不能为空");
return FormValidation.ok();
}
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project
// types
return true;
}
/**
* This human readable name is used in the configuration screen.
*/
public String getDisplayName() {
return "上传到七牛";
}
@Override
public QiniuPublisher newInstance(StaplerRequest req,
JSONObject formData) throws FormException {
List<QiniuEntry> entries = req.bindJSONToList(QiniuEntry.class,
formData.get("e"));
QiniuPublisher pub = new QiniuPublisher();
pub.getEntries().addAll(entries);
return pub;
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData)
throws FormException {
profiles.replaceBy(req.bindJSONToList(QiniuProfile.class,
formData.get("profile")));
save();
return true;
}
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.STEP;
}
public List<QiniuEntry> getEntries() {
return entries;
}
}