Commit 08f4e3fb authored by raylax's avatar raylax

update

parent 4713f40d
...@@ -21,8 +21,8 @@ ...@@ -21,8 +21,8 @@
~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin. ~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin.
--> -->
</properties> </properties>
<name>TODO Plugin</name> <name>Aliyun OSS Upload</name>
<description>TODO</description> <description>Aliyun OSS Upload plugin</description>
<!-- The default licence for Jenkins OSS Plugins is MIT. Substitute for the applicable one if needed. --> <!-- The default licence for Jenkins OSS Plugins is MIT. Substitute for the applicable one if needed. -->
<licenses> <licenses>
<license> <license>
...@@ -36,6 +36,11 @@ ...@@ -36,6 +36,11 @@
<artifactId>structs</artifactId> <artifactId>structs</artifactId>
<version>1.7</version> <version>1.7</version>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
<dependency> <dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId> <groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId> <artifactId>workflow-step-api</artifactId>
...@@ -80,14 +85,13 @@ ...@@ -80,14 +85,13 @@
</dependency> </dependency>
</dependencies> </dependencies>
<!-- If you want this to appear on the wiki page:
<developers> <developers>
<developer> <developer>
<id>bhacker</id> <id>raylax</id>
<name>Bob Q. Hacker</name> <name>YangchunYu</name>
<email>bhacker@nowhere.net</email> <email>raylax@inurl.org</email>
</developer> </developer>
</developers> --> </developers>
<!-- Assuming you want to host on @jenkinsci: <!-- Assuming you want to host on @jenkinsci:
<url>https://wiki.jenkins.io/display/JENKINS/TODO+Plugin</url> <url>https://wiki.jenkins.io/display/JENKINS/TODO+Plugin</url>
...@@ -109,4 +113,5 @@ ...@@ -109,4 +113,5 @@
<url>https://repo.jenkins-ci.org/public/</url> <url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository> </pluginRepository>
</pluginRepositories> </pluginRepositories>
</project> </project>
package org.inurl.jenkins.plugin; package org.inurl.jenkins.plugin;
import com.aliyun.oss.OSSClient;
import hudson.Extension;
import hudson.FilePath; import hudson.FilePath;
import hudson.Launcher; import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run; import hudson.model.Run;
import hudson.model.TaskListener; import hudson.model.TaskListener;
import hudson.util.FormValidation;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.StringUtils;
import org.inurl.jenkins.plugin.Messages;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor; import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher; import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.QueryParameter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
public class OSSPublisher extends Publisher implements SimpleBuildStep { public class OSSPublisher extends Publisher implements SimpleBuildStep {
...@@ -76,39 +78,68 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -76,39 +78,68 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
@Override @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 Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("endpoint => " + endpoint + "!"); PrintStream logger = listener.getLogger();
listener.getLogger().println("accessKeyId => " + accessKeyId + "!"); OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
listener.getLogger().println("accessKeySecret => " + accessKeySecret + "!"); String local = localPath.substring(1);
listener.getLogger().println("bucketName => " + bucketName + "!"); String remote = remotePath.substring(1);
listener.getLogger().println("localPath => " + localPath + "!"); logger.println("workspace => " + workspace);
listener.getLogger().println("remotePath => " + remotePath + "!"); FilePath p = new FilePath(workspace, local);
if (p.isDirectory()) {
logger.println("upload dir => " + p);
upload(client, logger, remote, p, true);
} else {
logger.println("upload file => " + p);
uploadFile(client, logger, remote, p);
}
}
private void upload(OSSClient 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);
}
return;
}
uploadFile(client, logger, base + "/" + path.getName(), path);
}
private void uploadFile(OSSClient client, PrintStream logger, String key, FilePath path) throws InterruptedException, IOException {
String realKey = key;
if (realKey.startsWith("/")) {
realKey = realKey.substring(1);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
path.copyTo(outputStream);
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
logger.println("uploading [" + path.getRemote() + "] to [" + realKey + "]");
client.putObject(bucketName, realKey, inputStream);
} }
@Extension @Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public FormValidation doCheckEndpoint(@QueryParameter String value) { public FormValidation doCheckEndpoint(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingEndpoint()); return checkValue(value, Messages.OSSPublish_MissingEndpoint());
} }
public FormValidation doCheckAccessKeyId(@QueryParameter String value) { public FormValidation doCheckAccessKeyId(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingAccessKeyId()); return checkValue(value, Messages.OSSPublish_MissingAccessKeyId());
} }
public FormValidation doCheckAccessKeySecret(@QueryParameter String value) { public FormValidation doCheckAccessKeySecret(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingAccessKeySecret()); return checkValue(value, Messages.OSSPublish_MissingAccessKeySecret());
} }
public FormValidation doCheckBucketName(@QueryParameter String value) { public FormValidation doCheckBucketName(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingBucketName()); return checkValue(value, Messages.OSSPublish_MissingBucketName());
} }
public FormValidation doCheckLocalPath(@QueryParameter String value) { public FormValidation doCheckLocalPath(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingLocalPath()); return checkValue(value, Messages.OSSPublish_MissingLocalPath());
} }
public FormValidation doCheckRemotePath(@QueryParameter String value) { public FormValidation doCheckRemotePath(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingRemotePath()); return checkValue(value, Messages.OSSPublish_MissingRemotePath());
} }
......
...@@ -4,5 +4,5 @@ OSSPublish.MissingEndpoint=\u8bf7\u8bbe\u7f6eEndpoint ...@@ -4,5 +4,5 @@ OSSPublish.MissingEndpoint=\u8bf7\u8bbe\u7f6eEndpoint
OSSPublish.MissingAccessKeyId=\u8bf7\u8bbe\u7f6eAccessKeyId OSSPublish.MissingAccessKeyId=\u8bf7\u8bbe\u7f6eAccessKeyId
OSSPublish.MissingAccessKeySecret=\u8bf7\u8bbe\u7f6eAccessKeySecret OSSPublish.MissingAccessKeySecret=\u8bf7\u8bbe\u7f6eAccessKeySecret
OSSPublish.MissingBucketName=\u8bf7\u8bbe\u7f6eBucketName OSSPublish.MissingBucketName=\u8bf7\u8bbe\u7f6eBucketName
OSSPublish.MissingLocalPath=\u8bf7\u8bbe\u7f6eLocalPath OSSPublish.MissingLocalPath=\u8bf7\u8bbe\u7f6e\u672c\u5730\u8def\u5f84
OSSPublish.MissingRemotePath=\u8bf7\u8bbe\u7f6eRemotePath OSSPublish.MissingRemotePath=\u8bf7\u8bbe\u7f6e\u8fdc\u7a0b\u8def\u5f84
\ No newline at end of file \ No newline at end of file
<div>
Aliyun OSS BucketName<br/>
<a target="_blank" href="https://oss.console.aliyun.com">config</a>
</div>
<div>
Remote file or directory, must begin with `/`
</div>
\ No newline at end of file
package org.inurl.jenkins.plugin;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Label;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
public class HelloWorldBuilderTest {
@Rule
public JenkinsRule jenkins = new JenkinsRule();
final String name = "Bobby";
@Test
public void testConfigRoundtrip() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new HelloWorldBuilder(name));
project = jenkins.configRoundtrip(project);
jenkins.assertEqualDataBoundBeans(new HelloWorldBuilder(name), project.getBuildersList().get(0));
}
@Test
public void testConfigRoundtripFrench() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
HelloWorldBuilder builder = new HelloWorldBuilder(name);
builder.setUseFrench(true);
project.getBuildersList().add(builder);
project = jenkins.configRoundtrip(project);
HelloWorldBuilder lhs = new HelloWorldBuilder(name);
lhs.setUseFrench(true);
jenkins.assertEqualDataBoundBeans(lhs, project.getBuildersList().get(0));
}
@Test
public void testBuild() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
HelloWorldBuilder builder = new HelloWorldBuilder(name);
project.getBuildersList().add(builder);
FreeStyleBuild build = jenkins.buildAndAssertSuccess(project);
jenkins.assertLogContains("Hello, " + name, build);
}
@Test
public void testBuildFrench() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
HelloWorldBuilder builder = new HelloWorldBuilder(name);
builder.setUseFrench(true);
project.getBuildersList().add(builder);
FreeStyleBuild build = jenkins.buildAndAssertSuccess(project);
jenkins.assertLogContains("Bonjour, " + name, build);
}
@Test
public void testScriptedPipeline() throws Exception {
String agentLabel = "my-agent";
jenkins.createOnlineSlave(Label.get(agentLabel));
WorkflowJob job = jenkins.createProject(WorkflowJob.class, "test-scripted-pipeline");
String pipelineScript
= "node {\n"
+ " greet '" + name + "'\n"
+ "}";
job.setDefinition(new CpsFlowDefinition(pipelineScript, true));
WorkflowRun completedBuild = jenkins.assertBuildStatusSuccess(job.scheduleBuild2(0));
String expectedString = "Hello, " + name + "!";
jenkins.assertLogContains(expectedString, completedBuild);
}
}
\ 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