Commit 1f8d95c0 authored by liaozan's avatar liaozan 🏀

Initial commit

parent e3e3cc42
Pipeline #698 failed with stages
in 0 seconds
......@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.4</version>
<version>4.33</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
......@@ -13,7 +13,7 @@
<packaging>hpi</packaging>
<properties>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>2.7.3</jenkins.version>
<jenkins.version>2.334</jenkins.version>
<java.level>8</java.level>
<!-- Other properties you may want to use:
~ jenkins-test-harness.version: Jenkins Test Harness version you use to test the plugin. For Jenkins version >= 1.580.1 use JTH 2.0 or higher.
......@@ -34,53 +34,53 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.7</version>
<version>1.24</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.12</version>
<version>2.24</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.39</version>
<version>2.94.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.11.2</version>
<version>2.42</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.6</version>
<version>2.24</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.13</version>
<version>2.39</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.20</version>
<version>2.47</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.14</version>
<version>3.8</version>
<scope>test</scope>
</dependency>
</dependencies>
......@@ -113,4 +113,4 @@
</pluginRepository>
</pluginRepositories>
</project>
</project>
\ No newline at end of file
package org.inurl.jenkins.plugin;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import javax.annotation.Nonnull;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.EnvVars;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.FormValidation;
import hudson.util.Secret;
import jenkins.tasks.SimpleBuildStep;
......@@ -25,7 +21,13 @@ import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
public class OSSPublisher extends Publisher implements SimpleBuildStep {
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
@SuppressWarnings("unused")
public class OSSPublisher extends Recorder implements SimpleBuildStep {
private final String endpoint;
......@@ -39,7 +41,19 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
private final String remotePath;
private final String maxRetries;
private final Integer maxRetries;
@DataBoundConstructor
public OSSPublisher(String endpoint, String accessKeyId, String accessKeySecret, String bucketName,
String localPath, String remotePath, Integer maxRetries) {
this.endpoint = endpoint;
this.accessKeyId = accessKeyId;
this.accessKeySecret = Secret.fromString(accessKeySecret);
this.bucketName = bucketName;
this.localPath = localPath;
this.remotePath = remotePath;
this.maxRetries = maxRetries;
}
public String getEndpoint() {
return endpoint;
......@@ -66,19 +80,7 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
}
public int getMaxRetries() {
return StringUtils.isEmpty(maxRetries) ? 3 : Integer.parseInt(maxRetries);
}
@DataBoundConstructor
public OSSPublisher(String endpoint, String accessKeyId, String accessKeySecret, String bucketName,
String localPath, String remotePath, String maxRetries) {
this.endpoint = endpoint;
this.accessKeyId = accessKeyId;
this.accessKeySecret = Secret.fromString(accessKeySecret);
this.bucketName = bucketName;
this.localPath = localPath;
this.remotePath = remotePath;
this.maxRetries = maxRetries;
return maxRetries == null ? 3 : maxRetries;
}
@Override
......@@ -87,14 +89,13 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
}
@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher,
@Nonnull TaskListener listener) throws InterruptedException, IOException {
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull EnvVars envVars,
@Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
PrintStream logger = listener.getLogger();
EnvVars envVars = run.getEnvironment(listener);
OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret.getPlainText());
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret.getPlainText());
String local = localPath.substring(1);
String[] remotes = remotePath.split(",");
for (String remote : remotes) {
remote = remote.substring(1);
......@@ -113,11 +114,10 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
logger.println("upload file success");
}
}
}
private void upload(OSSClient client, PrintStream logger, String base, FilePath path, boolean root)
throws InterruptedException, IOException {
private void upload(OSS client, PrintStream logger, String base, FilePath path, boolean root) throws InterruptedException, IOException {
if (path.isDirectory()) {
for (FilePath f : path.list()) {
upload(client, logger, base + (root ? "" : ("/" + path.getName())), f, false);
......@@ -127,8 +127,7 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
uploadFile(client, logger, base + "/" + path.getName(), path);
}
private void uploadFile(OSSClient client, PrintStream logger, String key, FilePath path)
throws InterruptedException, IOException {
private void uploadFile(OSS client, PrintStream logger, String key, FilePath path) throws InterruptedException, IOException {
if (!path.exists()) {
logger.println("file [" + path.getRemote() + "] not exists, skipped");
return;
......@@ -149,8 +148,7 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
throw new RuntimeException("upload fail, more than the max of retries");
}
private void uploadFile0(OSSClient client, PrintStream logger, String key, FilePath path)
throws InterruptedException, IOException {
private void uploadFile0(OSS client, PrintStream logger, String key, FilePath path) throws InterruptedException, IOException {
String realKey = key;
if (realKey.startsWith("/")) {
realKey = realKey.substring(1);
......@@ -198,6 +196,17 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
return checkBeginWithSlash(value);
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
@Nonnull
@Override
public String getDisplayName() {
return Messages.OSSPublish_DisplayName();
}
private FormValidation checkBeginWithSlash(String value) {
if (!value.startsWith("/")) {
return FormValidation.error(Messages.OSSPublish_MustBeginWithSlash());
......@@ -212,16 +221,6 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
return FormValidation.ok();
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
@Nonnull
@Override
public String getDisplayName() {
return Messages.OSSPublish_DisplayName();
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment