Skip to content

venkatasykam/jenkins-groovy-examples

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

jenkins-groovy-examples

Some samples of groovy scripts used in jenkins automation

Get API token for a user

user = hudson.model.User.get('username')
prop = user.getProperty(jenkins.security.ApiTokenProperty.class)
println(prop.getApiToken())

Enable matrix auth

Gives all authenticated users admin access

import jenkins.model.*
def instance = Jenkins.getInstance()

import hudson.security.*
def realm = new HudsonPrivateSecurityRealm(false)
instance.setSecurityRealm(realm)

def strategy = new hudson.security.GlobalMatrixAuthorizationStrategy()
strategy.add(Jenkins.ADMINISTER, 'authenticated')
instance.setAuthorizationStrategy(strategy)

instance.save()

Configure the slack plugin as if the form was submitted

import jenkins.model.*
def instance = Jenkins.getInstance()

// configure slack
def slack = Jenkins.instance.getExtensionList(
  jenkins.plugins.slack.SlackNotifier.DescriptorImpl.class
)[0]
def params = [
  slackTeamDomain: "domain",
  slackToken: "token",
  slackRoom: "",
  slackBuildServerUrl: "$JENKINS_URL",
  slackSendAs: ""
]
def req = [
  getParameter: { name -> params[name] }
] as org.kohsuke.stapler.StaplerRequest
slack.configure(req, null)
slack.save()

Configure Maven options

import jenkins.model.*
def instance = Jenkins.getInstance()

// Tell jenkins where maven is
def mavenTask = Jenkins.instance.getExtensionList(
  hudson.tasks.Maven.DescriptorImpl.class
)[0]
mavenTask.setInstallations(
  new hudson.tasks.Maven.MavenInstallation(
    "Maven", "$M2_HOME", []
  )
)
mavenTask.save()

// Configure global maven options
def maven = Jenkins.instance.getExtensionList(
  hudson.maven.MavenModuleSet.DescriptorImpl.class
)[0]
maven.setGlobalMavenOpts("-Dmaven.test.failure.ignore=false")
maven.save()

No executors on master

import jenkins.model.*
def instance = Jenkins.getInstance()

// No jobs on master
instance.setNumExecutors(0)

Global email settings

import jenkins.model.*
def instance = Jenkins.getInstance()

// set email
def location_config = JenkinsLocationConfiguration.get()
location_config.setAdminAddress("[email protected]")

About

Some samples of groovy scripts used in jenkins automation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published