├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Glen Mailer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jenkins-groovy-examples 2 | Some samples of groovy scripts used in jenkins automation 3 | 4 | 5 | ## Get API token for a user 6 | 7 | ```groovy 8 | user = hudson.model.User.get('username') 9 | prop = user.getProperty(jenkins.security.ApiTokenProperty.class) 10 | println(prop.getApiToken()) 11 | ``` 12 | 13 | ## Enable matrix auth 14 | 15 | Gives all authenticated users admin access 16 | 17 | ```groovy 18 | import jenkins.model.* 19 | def instance = Jenkins.getInstance() 20 | 21 | import hudson.security.* 22 | def realm = new HudsonPrivateSecurityRealm(false) 23 | instance.setSecurityRealm(realm) 24 | 25 | def strategy = new hudson.security.GlobalMatrixAuthorizationStrategy() 26 | strategy.add(Jenkins.ADMINISTER, 'authenticated') 27 | instance.setAuthorizationStrategy(strategy) 28 | 29 | instance.save() 30 | ``` 31 | 32 | ## Configure the slack plugin as if the form was submitted 33 | 34 | ```groovy 35 | import jenkins.model.* 36 | def instance = Jenkins.getInstance() 37 | 38 | // configure slack 39 | def slack = Jenkins.instance.getExtensionList( 40 | jenkins.plugins.slack.SlackNotifier.DescriptorImpl.class 41 | )[0] 42 | def params = [ 43 | slackTeamDomain: "domain", 44 | slackToken: "token", 45 | slackRoom: "", 46 | slackBuildServerUrl: "$JENKINS_URL", 47 | slackSendAs: "" 48 | ] 49 | def req = [ 50 | getParameter: { name -> params[name] } 51 | ] as org.kohsuke.stapler.StaplerRequest 52 | slack.configure(req, null) 53 | slack.save() 54 | ``` 55 | 56 | 57 | ## Configure Maven options 58 | 59 | ```groovy 60 | import jenkins.model.* 61 | def instance = Jenkins.getInstance() 62 | 63 | // Tell jenkins where maven is 64 | def mavenTask = Jenkins.instance.getExtensionList( 65 | hudson.tasks.Maven.DescriptorImpl.class 66 | )[0] 67 | mavenTask.setInstallations( 68 | new hudson.tasks.Maven.MavenInstallation( 69 | "Maven", "$M2_HOME", [] 70 | ) 71 | ) 72 | mavenTask.save() 73 | 74 | // Configure global maven options 75 | def maven = Jenkins.instance.getExtensionList( 76 | hudson.maven.MavenModuleSet.DescriptorImpl.class 77 | )[0] 78 | maven.setGlobalMavenOpts("-Dmaven.test.failure.ignore=false") 79 | maven.save() 80 | ``` 81 | 82 | 83 | ## No executors on master 84 | 85 | ```groovy 86 | import jenkins.model.* 87 | def instance = Jenkins.getInstance() 88 | 89 | // No jobs on master 90 | instance.setNumExecutors(0) 91 | ``` 92 | 93 | 94 | ## Global email settings 95 | 96 | ```groovy 97 | import jenkins.model.* 98 | def instance = Jenkins.getInstance() 99 | 100 | // set email 101 | def location_config = JenkinsLocationConfiguration.get() 102 | location_config.setAdminAddress("jenkins@azsb.skybet.net") 103 | ``` 104 | --------------------------------------------------------------------------------