grooving with jenkins

24
Jenkins World Tour 2015 Tel Aviv, Israel July 2015

Upload: anton-weiss

Post on 13-Aug-2015

225 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Grooving with Jenkins

Jenkins World Tour 2015Tel Aviv, Israel July 2015

Page 2: Grooving with Jenkins

#jenkinsconf

Grooving with the ButlerAnton Weiss, (otomato)

Page 3: Grooving with Jenkins

#jenkinsconf

About me

Ant(on) WeissDevOps Evangelist&Enabler, CI/CD/ALM Expertnow at PrimaryData [email protected]: @antweisshttp://otomato.link

Page 4: Grooving with Jenkins

#jenkinsconf

GROOVY

Page 5: Grooving with Jenkins

#jenkinsconf

The Groovy Language

• Easy to learn and use• Compiles to JVM bytecode• Dynamic, optionally-typed• Fully interoperates with Java• Provides scripting capabilities• Allows DSL authoring• Meta-programming• Functional programming

Page 6: Grooving with Jenkins

#jenkinsconf

Hello , groovy world!

def sayHello(name) {

    println("Hello $name!")

}

def name = 'world'

sayHello(name)

Page 7: Grooving with Jenkins

#jenkinsconf

Groovy use in Jenkins

• Perform Maintenance Tasks• Extend Functionality (instead of writing a full-fledged

plugin)• Retrieve Information

Page 8: Grooving with Jenkins

#jenkinsconf

Get into the Groove!

• Groovy to Manage your Jenkins

Page 9: Grooving with Jenkins

#jenkinsconf

Jenkins Script Console

Page 10: Grooving with Jenkins

#jenkinsconf

Jenkins Script Console

import jenkins.model.*import hudson.plugins.git.*Hudson.instance.items.each() { job -> def SCM = job.getScm() if(SCM instanceof GitSCM ) { def url = SCM.userRemoteConfigs[0].url println url new_url = url.replaceAll(/antweiss/, "otomato") SCM.userRemoteConfigs[0].url = new_url job.save() } }

Page 11: Grooving with Jenkins

#jenkinsconf

Jenkins Script Console

• No version control for scripts• Requires manual editing to change values

Solution:• Jenkins Scriptler Plugin

https://wiki.jenkins-ci.org/display/JENKINS/Scriptler+Plugin

Page 12: Grooving with Jenkins

#jenkinsconf

Go on grooving!

• Grooving with Parameters!

Page 13: Grooving with Jenkins

#jenkinsconf

Groovy for Dynamic Parameters

• Retrieve parameter values dynamically:• from Jenkins itself• from external data sources:

• e.g: SCM, DB, file system

Page 14: Grooving with Jenkins

#jenkinsconf

Groovy for Parameters - Example

• Retrieve a list of successful builds of a project:

import jenkins.model.*

job = Jenkins.instance.getItem(jobname)

job.getBuilds().each() { build ->

if ( build.result.toString() == "SUCCESS")

{

println build.number

}

}

Page 15: Grooving with Jenkins

#jenkinsconf

Groovy for Dynamic Parameters

• Use with:• Dynamic Parameter Plugin (+Scriptler)• Extended Choice Parameter Plugin• Extensible Choice Parameter Plugin

Page 16: Grooving with Jenkins

#jenkinsconf

Groovier Yet!

• Grooving with Build Steps!

Page 17: Grooving with Jenkins

#jenkinsconf

Groovy Build Steps

• Jenkins Groovy Plugin• run regular groovy scripts• run “system groovy scripts” :

• inside Jenkins master JVM• schedule maintenance scripts• extend functionality:

• build.setDescription(build.buildVariables.get(‘VERSION_NUM'))

Page 18: Grooving with Jenkins

#jenkinsconf

Go on grooving!

• Grooving after Builds!

Page 19: Grooving with Jenkins

#jenkinsconf

Groovy Post-Build Plugin

• Provides ‘manager’ object• Provides log parser methods: logContains(regexp)

getLogMatcher(regexp) - returns a java.util.regex.Matcher• Provides decorator methods:

manager.addShortText(“$manager.getEnvVariable('DD_VERSION')}")

Page 20: Grooving with Jenkins

#jenkinsconf

Groovy Post-Build Plugin

def exit_status = manager.getEnvVariable("EXIT_FLAG")def SETUP_NAME=manager.getEnvVariable("name")switch (exit_status) { case "0": manager.buildSuccess() break case "9": manager.buildUnstable() manager.addBadge("delete.gif", "release setup", “http://my.server.com:8080/view/integration/job/drms-release-setup/parambuild/?name=$SETUP_NAME”) manager.createSummary("orange-square.png").appendText("<b>Setup name:</b> $SETUP_NAME", false, false, false, "grey") break default: manager.buildFailure()}

Page 21: Grooving with Jenkins

#jenkinsconf

Groovy Emails

• Using Groovy with Email-ext plugin

+

Page 22: Grooving with Jenkins

#jenkinsconf

Groovy Emails

• In ‘Recipient List’ field:• ${SCRIPT, script=“upstream_committers.groovy”}

• For email templates:• ${SCRIPT, template="groovy-html1.template"}

Page 23: Grooving with Jenkins

#jenkinsconf

Still want a plugin?

• The Groovy Way to Write Jenkins Plugins• by Shiran Rubin (jfrog)

Page 24: Grooving with Jenkins

#jenkinsconf

Footer

Divider Slide

Thanks and Be Groovy!!!