Upload
kiyotaka-oku
View
3.506
Download
0
Tags:
Embed Size (px)
Citation preview
Jenkinsプラグイン開発
メモ@kiy0taka
13年4月5日金曜日
自己紹介•奥清隆(おく きよたか)
•id:kiy0taka、@kiy0taka ※ 「0」は数字のゼロ
•株式会社ニューキャスト所属
•日本Grails/Groovyユーザーグループ
•関西Javaエンジニアの会
13年4月5日金曜日
プラグイン開発歴(公式)
•Terminal
•jQuery•jQueryUI•MongoDB
•TEPCO•Groovy Remote Control
13年4月5日金曜日
プラグイン開発歴(非公式)
•Backup•GCrawler•go-con•Xmas
•irof
13年4月5日金曜日
多分書きます
http://beta.mybetabook.com/b/kiy0taka/Building+Jenkins+plugin+with+Groovy
13年4月5日金曜日
プラグインを作る
•Apache Jelly•Stapler•Extension Point•http://wiki.jenkins-ci.org/display/JENKINS/Extension+points
13年4月5日金曜日
前準備
•JDK 1.6以上
•Maven 3.0.4以上が望ましい
•好きなIDE
13年4月5日金曜日
Maven設定
•~/.m2/settings.xmlに設定を追記
•参考:https://gist.github.com/kiy0taka/850703
13年4月5日金曜日
settings.xml<settings> [...] <pluginGroups> <pluginGroup>org.jenkins-ci.tools</pluginGroup> </pluginGroups> <profiles> [...] <profile> <id>jenkins</id> <pluginRepositories> <pluginRepository> <id>m.g.o-public</id> <url>http://maven.glassfish.org/content/groups/public/</url> </pluginRepository> </pluginRepositories> <repositories> <repository> <id>m.g.o-public</id> <url>http://maven.glassfish.org/content/groups/public/</url> </repository> </repositories> </profile> </profiles> [...]</settings> ※ profileを分けなくても良い
13年4月5日金曜日
Maven Jenkins Plugin
•プロジェクト作成
•mvn hpi:create -P jenkins
•実行
•mvn hpi:run
•パッケージ、リリースなどは通常のターゲットで。(package, release:prepare)
13年4月5日金曜日
Describable/Descriptor
•Describableインターフェースを実装するクラスにはDescriptorの実装が必要
•Descriptorはシングルトン、DescribableなインスタンスはDescriptorから作られる
13年4月5日金曜日
HelloWorldBuilderpublic class HelloWorldBuilder extends Builder {
@Override public DescriptorImpl getDescriptor() { return (DescriptorImpl)super.getDescriptor(); }
@Extension public static final class DescriptorImpl extends BuildStepDescriptor<Builder> { }}
13年4月5日金曜日
13年4月5日金曜日
13年4月5日金曜日
Stapler•JenkinsのオブジェクトとURLをマッピング
•/descriptorByName/org.jenkinsci.plugins.sample.HelloWorldBuilder/checkName?value=Hello
•Jenkins.getInstance().getDescriptorByName("org.jenkinsci.plugins.sample.HelloWorldBuilder")).doCheckName("Hello")
13年4月5日金曜日
Builder#perform()•AbstractBuild•実行中のビルド
•Launcher•ノード上でコマンドを実行する
•BuildListener•ログ出力したり
13年4月5日金曜日
プロセス実行
// コマンドArgumentListBuilder args = new ArgumentListBuilder("ls", "-la");
// 実行launcher.launch().cmds(args) // コマンド設定.stdout(listener.getLogger()) // 標準出力をビルドのログに出力.join(); // 実行して待つ
13年4月5日金曜日
プロセス実行
// コマンドArgumentListBuilder args = new ArgumentListBuilder("ls", "-la");
// 実行launcher.launch().cmds(args) // コマンド設定.stdout(listener.getLogger()) // 標準出力をビルドのログに出力.join(); // 実行して待つ
13年4月5日金曜日
ノード上で処理// リモート上で実行する処理static class MyTask implements Callable<String, Exception> { public String call() throws Exception { return "hello"; }}
public boolean perform(...) { // 処理実行 launcher.getChannel().call(new MyTask());}
13年4月5日金曜日
ノード上で処理
public boolean perform(...) {
// callの引数はシリアライズされてリモートノード上で実行される // BuilderはSerializableではないのでこれはダメ launcher.getChannel().call(new Callable<String, Exception> { public String call() throws Exception { return "hello"; } });}
13年4月5日金曜日
Jelly
•JenkinsのViewを書く
•Groovyスクリプトでも記述可能
•書き方についてはUI Samplesを参照
•どのjellyファイルを書かないといけないかは各ExtensionのJavadoc等を参照
13年4月5日金曜日
マイグレーション
13年4月5日金曜日
13年4月5日金曜日
マイグレーション
•モデルのプロパティを変更した場合、古いデータも変更する必要がある
•プラグインのバージョンを上げてジョブの設定が飛んだとかならないように
•バージョンを下げても動くように
13年4月5日金曜日
マイグレーション
// private final String name;private final String username;
// Initializerを使って対象のデータが読み込まれる前に@Initializer(before = InitMilestone.JOB_LOADED)public static void migration() { Items.XSTREAM2.aliasAttribute( HelloWorldBuilder.class, "username", "name");}
13年4月5日金曜日
テスト
13年4月5日金曜日
Jenkinsとの結合テスト
•extens HudsonTestCase•@Rule JenkinsRule
13年4月5日金曜日
@Rulepublic JenkinsRule rule = new JenkinsRule();
@Testpublic void test() throws Exception {
FreeStyleProject myJob = rule.createFreeStyleProject(); HelloWorldBuilder builder = new HelloWorldBuilder("World"); myJob.getBuildersList().add(builder); myJob.save();
FreeStyleBuild build = myJob.scheduleBuild2(0).get();
assertEquals(build.getResult(), Result.SUCCESS); assertTrue(readFileToString(build.getLogFile()) .contains("Hello, World!"));}
13年4月5日金曜日
@Test@LocalDatapublic void test() throws Exception {
FreeStyleProject myJob = (FreeStyleProject) rule.jenkins.getItem("myJob"); FreeStyleBuild build = myJob.scheduleBuild2(0).get();
assertEquals(build.getResult(), Result.SUCCESS); assertTrue(readFileToString(build.getLogFile()) .contains("Hello, World!"));}
src/test/resources/org/jenkinsci/plugins/sample/HelloWorldBuilderTest/test以下にテスト用の
JENKINS_HOMEを作っておく
13年4月5日金曜日
@Recipe
•LocalData•PresetData•WithPlugin
•WithPluginManager
•WithTimeout
13年4月5日金曜日