|
| 1 | +/* |
| 2 | + *Staging tested all scenarios |
| 3 | + *This script will revert all changes applied for the projects |
| 4 | + */ |
| 5 | +package org.wso2.build; |
| 6 | +import hudson.plugins.git.UserRemoteConfig; |
| 7 | +import hudson.plugins.git.extensions.impl.WipeWorkspace; |
| 8 | +import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsBuildWrapper; |
| 9 | +import hudson.plugins.git.* |
| 10 | +import hudson.maven.*; |
| 11 | +import hudson.plugins.git.util.*; |
| 12 | +import hudson.model.*; |
| 13 | +import hudson.model.Item; |
| 14 | +import hudson.model.Job; |
| 15 | +import hudson.model.Queue; |
| 16 | +import hudson.model.TaskListener; |
| 17 | +import hudson.model.queue.Tasks; |
| 18 | +import hudson.model.AbstractProject; |
| 19 | +import hudson.model.Project; |
| 20 | +import hudson.maven.RedeployPublisher.DescriptorImpl; |
| 21 | +import hudson.plugins.git.util.Build; |
| 22 | +import hudson.triggers.SCMTrigger; |
| 23 | +import hudson.triggers.Trigger; |
| 24 | +import hudson.triggers.TriggerDescriptor; |
| 25 | +import hudson.maven.AbstractMavenProject; |
| 26 | +import hudson.maven.MavenBuild; |
| 27 | +import hudson.maven.MavenModule; |
| 28 | +import hudson.maven.MavenModuleSet; |
| 29 | +import hudson.maven.MavenModuleSetBuild; |
| 30 | +import hudson.Extension; |
| 31 | +import hudson.Util; |
| 32 | +import hudson.plugins.git.extensions.GitSCMExtension; |
| 33 | +import hudson.plugins.git.extensions.GitSCMExtensionDescriptor; |
| 34 | +import com.cloudbees.jenkins.*; |
| 35 | +import com.google.common.base.Charsets; |
| 36 | +import com.google.common.base.Preconditions; |
| 37 | +import hudson.console.AnnotatedLargeText; |
| 38 | +import hudson.triggers.SCMTrigger; |
| 39 | +import hudson.triggers.Trigger; |
| 40 | +import hudson.triggers.TriggerDescriptor; |
| 41 | +import hudson.util.FormValidation; |
| 42 | +import hudson.util.NamingThreadFactory; |
| 43 | +import hudson.util.SequentialExecutionQueue; |
| 44 | +import hudson.util.StreamTaskListener; |
| 45 | +import jenkins.model.ParameterizedJobMixIn; |
| 46 | +import jenkins.triggers.SCMTriggerItem.SCMTriggerItems; |
| 47 | +import org.kohsuke.accmod.Restricted; |
| 48 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 49 | +import org.kohsuke.stapler.AncestorInPath; |
| 50 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 51 | +import org.slf4j.Logger; |
| 52 | +import org.slf4j.LoggerFactory; |
| 53 | +import org.jvnet.hudson.plugins.m2release.M2ReleaseBuildWrapper; |
| 54 | +import hudson.Extension; |
| 55 | +import hudson.Launcher; |
| 56 | +import hudson.Util; |
| 57 | +import hudson.maven.AbstractMavenProject; |
| 58 | +import hudson.maven.MavenBuild; |
| 59 | +import hudson.maven.MavenModule; |
| 60 | +import hudson.maven.MavenModuleSet; |
| 61 | +import hudson.maven.MavenModuleSetBuild; |
| 62 | +import hudson.model.Action; |
| 63 | +import hudson.model.BuildListener; |
| 64 | +import hudson.model.Result; |
| 65 | +import hudson.model.AbstractBuild; |
| 66 | +import hudson.model.AbstractProject; |
| 67 | +import hudson.model.Descriptor; |
| 68 | +import hudson.model.Hudson; |
| 69 | +import hudson.model.Item; |
| 70 | +import hudson.model.Run; |
| 71 | +import hudson.security.Permission; |
| 72 | +import hudson.security.PermissionGroup; |
| 73 | +import hudson.tasks.BuildWrapper; |
| 74 | +import hudson.tasks.BuildWrapperDescriptor; |
| 75 | +import hudson.tasks.Builder; |
| 76 | +import hudson.util.FormValidation; |
| 77 | +import hudson.util.RunList; |
| 78 | +import hudson.plugins.git.util.Build; |
| 79 | +import java.io.IOException; |
| 80 | +import java.lang.reflect.Array; |
| 81 | +import java.lang.reflect.Constructor; |
| 82 | +import java.lang.reflect.Field; |
| 83 | +import java.lang.reflect.InvocationTargetException; |
| 84 | +import java.net.MalformedURLException; |
| 85 | +import java.net.URL; |
| 86 | +import java.util.Map; |
| 87 | +import static org.apache.commons.lang3.StringUtils.isEmpty; |
| 88 | +import static org.jenkinsci.plugins.github.util.JobInfoHelpers.asParameterizedJobMixIn; |
| 89 | +import static com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsBuildWrapper.VarPasswordPair; |
| 90 | + |
| 91 | +jenkins = Hudson.instance |
| 92 | +// Define hour to compare (hour=24 will find builds that were built more than 1 day ago) |
| 93 | +hour=24; |
| 94 | +minute=60; |
| 95 | +second=60; |
| 96 | +oneDayInSecond=hour*minute*second; |
| 97 | +now=Calendar.instance; |
| 98 | +list=[]; |
| 99 | + |
| 100 | +println("The build is run at ${now.time}"); |
| 101 | + |
| 102 | +for (item in jenkins.items){ |
| 103 | + |
| 104 | + BuildListener buildListener; |
| 105 | + AbstractProject project; |
| 106 | + GitSCM gitSCM; |
| 107 | + List <VarPasswordPair> varPasswordPairs; |
| 108 | + |
| 109 | + if(item.getLastBuild()!=null&&(item.name ==~ /(.*)identity-(.*)/)){ |
| 110 | + |
| 111 | + // if(item.getLastBuild()!=null&&(item.name ==~ /(.*)siddhi-(.*)/)){ |
| 112 | + |
| 113 | + /*Uncomment this if you want to update a single project |
| 114 | + * |
| 115 | + |
| 116 | + String projName="siddhi-test-shavantha" |
| 117 | + if(item.getLastBuild()!=null&&(item.name ==projName)){ |
| 118 | + */ |
| 119 | + |
| 120 | + |
| 121 | + println(item.name) |
| 122 | + |
| 123 | + /* |
| 124 | + This section handles the mask password plugin v2.8 |
| 125 | + |
| 126 | + */ |
| 127 | + varPasswordPairs = new ArrayList(); |
| 128 | + MaskPasswordsBuildWrapper mpw =new MaskPasswordsBuildWrapper(varPasswordPairs) |
| 129 | + this.varPasswordPairs=varPasswordPairs |
| 130 | + |
| 131 | + msk = item.getBuildWrappers().get(MaskPasswordsBuildWrapper.class); |
| 132 | + |
| 133 | + if(msk !=null){ |
| 134 | + rw = item.getBuildWrappers().clear() |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + /* |
| 140 | + This section handles the build triggers |
| 141 | + */ |
| 142 | + |
| 143 | + |
| 144 | + GitHubPushTrigger gitTrigger=new GitHubPushTrigger() |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + //SCM Trigger |
| 149 | + TriggerDescriptor SCM_TRIGGER_DESCRIPTOR = Hudson.instance.getDescriptorOrDie(SCMTrigger.class) |
| 150 | + def scmtrigger = item.getTriggers().get(SCM_TRIGGER_DESCRIPTOR) |
| 151 | + |
| 152 | + |
| 153 | + TriggerDescriptor GIT_TRIGGER_DESCRIPTOR = Hudson.instance.getDescriptor(GitHubPushTrigger.class) |
| 154 | + def gittrigger = item.getTriggers().get(GIT_TRIGGER_DESCRIPTOR) |
| 155 | + |
| 156 | + |
| 157 | + if(scmtrigger == null){ |
| 158 | + println("SCM triggers is applied") |
| 159 | + item.addTrigger(new SCMTrigger("")) |
| 160 | + item.save() |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + if(gittrigger != null){ |
| 165 | + println("Build when a change is pushed to GitHub trigger removed") |
| 166 | + item.removeTrigger(GIT_TRIGGER_DESCRIPTOR) |
| 167 | + item.save() |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + /* |
| 175 | + This section handles the credentials dropdown |
| 176 | + */ |
| 177 | + |
| 178 | + //Assign the credentialsID for the required value |
| 179 | + final String credentialsId="" |
| 180 | + |
| 181 | + |
| 182 | + try{ |
| 183 | + AbstractBuild build=item.getLastBuild() |
| 184 | + |
| 185 | + gitSCM = (GitSCM) build.getProject().getScm(); |
| 186 | + |
| 187 | + }catch(Exception e){} |
| 188 | + |
| 189 | + List<UserRemoteConfig> userRemoteConfigs = gitSCM.getUserRemoteConfigs(); |
| 190 | + |
| 191 | + |
| 192 | + if (userRemoteConfigs.isEmpty()) { |
| 193 | + println("[WSO2 Maven Release] " + "Could not find the git remote URL for the project. \n"); |
| 194 | + } |
| 195 | + if(userRemoteConfigs.get(0).getCredentialsId() == null){ |
| 196 | + println("credentials applied") |
| 197 | + userRemoteConfigs.get(0).credentialsId=credentialsId |
| 198 | + |
| 199 | + item.save() |
| 200 | + }else{ |
| 201 | + println("credentials applied") |
| 202 | + userRemoteConfigs.get(0).credentialsId=credentialsId |
| 203 | + item.save() |
| 204 | + |
| 205 | + } |
| 206 | + /* |
| 207 | + This section handles the post build actions |
| 208 | + */ |
| 209 | + |
| 210 | + String id="wso2.snapshots" |
| 211 | + String url="http://maven.wso2.org/nexus/content/repositories/snapshots/"; |
| 212 | + boolean uniqueVersion=true; |
| 213 | + boolean evenIfUnstable=false; |
| 214 | + String releaseEnvVar="IS_M2RELEASEBUILD"; |
| 215 | + BuildListener listener; |
| 216 | + |
| 217 | + |
| 218 | + RedeployPublisher rdp =new RedeployPublisher(id,url,uniqueVersion,evenIfUnstable,releaseEnvVar) |
| 219 | + this.id = id; |
| 220 | + this.url = url; |
| 221 | + this.uniqueVersion = uniqueVersion; |
| 222 | + this.evenIfUnstable = evenIfUnstable; |
| 223 | + this.releaseEnvVar = releaseEnvVar; |
| 224 | + |
| 225 | + |
| 226 | + envVarValue=item.getPublishersList().get(RedeployPublisher.class) |
| 227 | + |
| 228 | + if (envVarValue != null) |
| 229 | + { |
| 230 | + envVarValue=item.getPublishersList().clear() |
| 231 | + item.save() |
| 232 | + action = "established" |
| 233 | + } |
| 234 | + |
| 235 | + /* |
| 236 | + This section handles the maven release build |
| 237 | + */ |
| 238 | + |
| 239 | + def DEFAULT_RELEASE_GOALS = "release:clean release:prepare release:perform -P wso2-release"; |
| 240 | + def DEFAULT_DRYRUN_GOALS = "release:clean release:prepare release:perform -P wso2-release"; |
| 241 | + |
| 242 | + |
| 243 | + M2ReleaseBuildWrapper m2obj = new M2ReleaseBuildWrapper(DEFAULT_RELEASE_GOALS,DEFAULT_DRYRUN_GOALS,false,false,false,"IS_M2RELEASEBUILD","","",0) |
| 244 | + this.releaseGoals=DEFAULT_RELEASE_GOALS |
| 245 | + this.dryRunGoals=DEFAULT_DRYRUN_GOALS |
| 246 | + this.selectCustomScmCommentPrefix=true |
| 247 | + this.selectAppendHudsonUsername=true |
| 248 | + this.selectScmCredentials=false |
| 249 | + this.releaseEnvVar="" |
| 250 | + this.scmUserEnvVar="" |
| 251 | + this.scmPasswordEnvVar="" |
| 252 | + this.numberOfReleaseBuildsToKeep=0 |
| 253 | + |
| 254 | + build=item.getLastBuild() |
| 255 | + // println("Script applies to project" + "${item.name}"+"\tand build\t"+"${build.toString()}"); |
| 256 | + |
| 257 | + rw = item.getBuildWrappers().get(M2ReleaseBuildWrapper.class); |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + if (rw != null) |
| 263 | + { |
| 264 | + rw = item.getBuildWrappers().clear() |
| 265 | + item.save() |
| 266 | + action = "established" |
| 267 | + } |
| 268 | + |
| 269 | +/* |
| 270 | + this section handles the wipeout workspace and force clone |
| 271 | + */ |
| 272 | + |
| 273 | + WipeWorkspace work =new WipeWorkspace(); |
| 274 | + |
| 275 | + gitSCM = (GitSCM) build.getProject().getScm(); |
| 276 | + |
| 277 | + List<GitSCMExtension> gitExtensions = gitSCM.getExtensions(); |
| 278 | + |
| 279 | + |
| 280 | + if(gitExtensions !=null){ |
| 281 | + println("wipe out workspace and force clone") |
| 282 | + gitExtensions.clear() |
| 283 | + item.save() |
| 284 | + } |
| 285 | + if(gitExtensions ==null){ |
| 286 | + println("wipe out workspace and force clone already added") |
| 287 | + |
| 288 | + } |
| 289 | + |
| 290 | + |
| 291 | + } |
| 292 | +} |
| 293 | + |
0 commit comments