writing a jenkins / hudson plugin

31
Writing a Hudson / Jenkins plugin Anthony Dahanne @ EclipseCon NA 2014, March 19th

Upload: anthony-dahanne

Post on 08-May-2015

3.031 views

Category:

Technology


2 download

DESCRIPTION

Those are the slides of the talk I gave during EclipseCon 2014 : https://www.eclipsecon.org/na2014/session/writing-hudson-jenkins-plugin

TRANSCRIPT

Page 1: Writing a Jenkins / Hudson plugin

Writing a Hudson / Jenkins pluginAnthony Dahanne @ EclipseCon NA 2014, March 19th

Page 2: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins pluginConfoo 2013

About me …

"2

§ Software Engineer at Terracotta – Working on EhCache management REST API and

webapp (aka Terracotta Management Console, TMC) – Strong interest in CI & build tools (Maven, Jenkins,

Hudson, Nexus plugins author) – Android developer when time permits ...

Page 3: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins pluginConfoo 2013

Terracotta

"3

§ Founded 2003 in San Francisco, CA § Joined Software AG in 2011 § Present in India, Europe

and pretty much all over the globe! § The company behind :

Page 4: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Agenda

§ Jenkins / Hudson ? Quick intro § Do I need to create a plugin ? § Let’s build a plugin ! § How to (integration) test this plugin § Share the plugin with the community

"4

Page 5: Writing a Jenkins / Hudson plugin

Jenkins / Hudson ? Quick intro

Page 6: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Jenkins / Hudson ?

§ Hudson is an open source Continuous Integration (CI) tool created in 2005 by Kohsuke Kawaguchi

§ Became really popular from 2008 § The project split in November 2010 § Hudson moved to the Eclipse Foundation in May

2011 § Still the most popular CI tool(s) in 2014

"6

Page 7: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

InfoQ CI popularity results

"7

HudsonJenkins

Page 8: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Jenkins / Hudson few differences (figures)

§ 5000 commits § 325 contributors § 70 000 installs (9/13) § 889 plugins

"8

§ 1230 commits § 15 contributors § 65 000 downloads (9/13) § 379 plugins

Since September 2011

Page 9: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Jenkins / Hudson few differences (features)

§ plugins hot install § write views in Groovy § UI improvements

"9

§ maven3 plugin § cascading project settings § Eclipse IP clean § team concept

Since September 2011

Page 10: Writing a Jenkins / Hudson plugin

Do I need to create a plugin ?

Page 11: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Do we need yet another plugin ?

§ 100’s of existing plugins in the update centers ! § Customized reports / trigger a build

– CLI – Remote API

§ Groovy plugin(s) – Allow you to run a groovy script during build or post build

"11

Page 12: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Hudson / Jenkins CLI

§ You can list jobs, start them, delete them (same applies for nodes) !

§ $ java -jar hudson-cli.jar -s http://localhost:8080/ list-jobs§ $ java -jar hudson-cli.jar -s http://localhost:8080/ build ehcache-jcache_master

!

§ Perfect for command line scripts !

"12

Page 13: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Remote API

§ Take any Hudson / Jenkins url, add – /api/xml : xml representation of your view – /api/json : json representation of your view

§ Depth to control the level of detail – http://ci.jruby.org/api/xml?depth=1

§ Tree to specify what you want – http://ci.jruby.org/api/xml?

tree=jobs[displayName,lastBuild[result]] § Enjoy powerful xpath filters (with /api/xml only)

– http://ci.jruby.org/api/xml?tree=jobs[displayName,lastBuild[result]]&exclude=hudson/job[lastBuild[result=%27SUCCESS%27]]

"13

Page 14: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Groovy plugin(s)

§ Allow you to run a groovy script during build or post build – EnvInject Plugin : inject System env. variable to your

build – Groovy Postbuild plugin : execute a groovy script in the

Jenkins VM § Usage of those plugins is powerful (dangerous?), they

usually have access to the Jenkins API and runtime

"14

Page 15: Writing a Jenkins / Hudson plugin

Let’s write a plugin !

Page 16: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Let’s write a plugin !

§ hpi:create to bootstrap the creation $ mvn org.eclipse.hudson.tools:maven-hpi-plugin:create

§ hpi:run to run it in a fresh container $ mvn package org.eclipse.hudson.tools:maven-hpi-plugin:run

"16

Page 17: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

What got created

"17

Diagram copied from the book Hudson Continuous Integration in Practice

Page 18: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Extensions points : job configuration

"18

SCM

Trigger

Axis

Builder

Recorder

Page 19: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Extensions points : dashboard

"19

ListView ListViewColumnRootAction

PageDecorator

Page 20: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Extensions points : job view

"20

TransientProjectActionFactory

BuildBadgeAction

Page 21: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

UI Descriptor and Jelly views

"21

Use the class name for your resources folder

Plugin description (appears in the update center)help caption for the field “name”UI configuration for the system configuration page

UI configuration for the Job configuration page

Page 22: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Variables accessible from Jelly

"22

§ app : maps to Hudson.getInstance() § app.getUrl() maps to Hudson.getInstance().getUrl()

§ it : maps to the UI element rendered by Jelly § it.name maps to HelloWorldBuilder.getName()

§ h : maps to the Functions class § h.jsStringEscape() maps to Functions.jsStringEscape()

Page 23: Writing a Jenkins / Hudson plugin

How to (integrate) test a plugin

Page 24: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Write integration tests

§ Write a class that extends HudsonTestCase § Benefit from its helper methods to configure

Hudson : – createFreeStyleProject() and friends – assertBuildStatus() and others – WebClient.getPage() and WebAssert.assertElementPresent()

§ or inject existing configuration with @LocalData

"24

Page 25: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Write integration tests (local data layout)

"25

Use the class name for your resources folder

Page 26: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Run your integration tests

§ From your IDE, Run as… / Debug as … § With Maven, the usual

– $ mvn clean test (-Dtest=org.my.Test)

§ A friend’s advice : fork your VM when running a test suite § Add in your pom :

"26

<build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId> <configuration> <!-- make sure each test spawns a different vm --> <forkMode>always</forkMode> </configuration> </plugin> </plugins> </build>

Page 27: Writing a Jenkins / Hudson plugin

Share the plugin with the community

Page 28: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Share your Jenkins plugin with the community

§ Test it and target the lowest API level possible § Add Maven metadata § Ask [email protected] to create a

github repo to host your plugin § Create a wiki page on https://wiki.jenkins-ci.org/

display/JENKINS/Home § Release your plugin with maven to the Jenkins repo § Built on https://jenkins.ci.cloudbees.com/job/plugins/

"28

Page 29: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

Share your Hudson plugin with the community

§ Test it and target the lowest API level possible § Add Maven metadata § Ask [email protected] to create a github repo

to host your plugin § Create a wiki page on http://wiki.hudson-ci.org § Release your plugin with maven to the Sonatype

OSS repo § Built on http://ci.hudson-ci.org/

"29

Page 30: Writing a Jenkins / Hudson plugin

EclipseCon NA 2014 — Writing a Hudson / Jenkins plugin

References

§ Hudson Continuous Integration in Practice, by Winston Prakash and Ed Burns § Jenkins User Conference 2011 (on slideshare) § Hudson (on wikipedia) § Writing your first Hudson plugin § List of Extension points § Writing tests for your plugin § Hosting Hudson plugins and Releasing Hudson plugins § Hosting and releasing Jenkins Plugins § InfoQ : What CI Server do you use ? § Developing a plugin for both Jenkins and Hudson

"30

Page 31: Writing a Jenkins / Hudson plugin