Jenkins Google Driver Uploader
This plugin allows you to upload artifacts to your google service account.
Install
Create an HPI file to install in Jenkins (HPI file will be in target/google-drive-upload.hpi
).
1 2 |
mvn clean package |
ScreenShot
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 |
package com.generalmobile.googledriveupload; import com.google.api.client.auth.oauth2.Credential; import com.google.jenkins.plugins.credentials.domains.DomainRequirementProvider; import com.google.jenkins.plugins.credentials.domains.RequiresDomain; import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials; import hudson.Extension; import hudson.Launcher; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; import hudson.model.Result; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; import org.springframework.util.StringUtils; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Objects; import static com.google.common.base.Preconditions.checkNotNull; @RequiresDomain(value = DriveScopeRequirement.class) public final class GoogleDriveUploader extends Recorder { private final String credentialsId; private final String driveFolderName; private final String uploadFolder; private final String userMail; @DataBoundConstructor public GoogleDriveUploader(String credentialsId, String driveFolderName, String uploadFolder, String userMail) { this.credentialsId = checkNotNull(credentialsId); this.driveFolderName = checkNotNull(driveFolderName); this.uploadFolder = checkNotNull(uploadFolder); this.userMail = checkNotNull(userMail); } public FormValidation doCheckUserMail(@QueryParameter String value) { return FormValidation.error("Not a number"); /* int at =StringUtils.countOccurrencesOf(value,"@"); int semicolon =StringUtils.countOccurrencesOf(value,";"); if (at-1==semicolon) return FormValidation.warning("Please check mail again. If you using multi mail please separate each mail with ;"); else return FormValidation.ok();*/ } public String getUploadFolder() { return uploadFolder; } public String getUserMail() { return userMail; } public String getDriveFolderName() { return driveFolderName; } @Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { try { listener.getLogger().println("Google Drive Uploading Plugin Started."); GoogleRobotCredentials credentials = GoogleRobotCredentials.getById(getCredentialsId()); GoogleDriveManager driveManager = new GoogleDriveManager(authorize(credentials)); String workspace = Objects.requireNonNull(build.getWorkspace()).getRemote(); if (uploadFolder.length() > 0) { if (uploadFolder.startsWith("$")) { workspace += "/" + build.getEnvironment(listener).get(uploadFolder.replace("$", "")); } else { workspace += "/" + uploadFolder; } } listener.getLogger().println("Uploading folder: " + workspace); driveManager.uploadFolder(workspace, getDriveFolderName(), listener, userMail); } catch (GeneralSecurityException e) { build.setResult(Result.FAILURE); return false; } return true; } private Credential authorize(GoogleRobotCredentials credentials) throws GeneralSecurityException { GoogleRobotCredentials googleRobotCredentials = credentials.forRemote(getRequirement()); return googleRobotCredentials.getGoogleCredential(getRequirement()); } private DriveScopeRequirement getRequirement() { return DomainRequirementProvider.of(getClass(), DriveScopeRequirement.class); } private String getCredentialsId() { return credentialsId; } @Override public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; } @Extension public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { @Override public String getDisplayName() { return "Google Drive Uploader"; } @Override public boolean isApplicable(Class<? extends AbstractProject> aClass) { return true; } } } |
https://github.com/general-mobile/jenkins-google-drive-uploader