Jenkins and Groovy

64
Jenkinsエンジニアのための モテる Groovy入門 2011226日土曜日

description

 

Transcript of Jenkins and Groovy

Page 1: Jenkins and Groovy

Jenkinsエンジニアのためのモテる Groovy入門

2011年2月26日土曜日

Page 2: Jenkins and Groovy

自己紹介奥清隆 (@kiy0taka、id:kiy0taka)

大阪在住

日本Grails/Groovyユーザグループ

Jenkins Plugin

Terminal、jQuery、jQuery UI

前回のLT「モテるHudsonエンジニア」

http://www.ustream.tv/recorded/108018382011年2月26日土曜日

Page 3: Jenkins and Groovy

モテなかったクリスマスも一人。

チョコも貰ってません。

合コンにも誘われてない。

結論

「Jenkinsエンジニアはモテない。ソースは俺。」

2011年2月26日土曜日

Page 4: Jenkins and Groovy

リア充爆発しろ!

2011年2月26日土曜日

Page 5: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

GroovyJVM上で動作するスクリプト言語簡潔・高機能記述量Java比1/2~1/5

Javaとの高い相互運用性Groovy Class=Java ClassGroovy Object=Java Object

6

2011年2月26日土曜日

Page 6: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Javaimport java.io.*;import java.net.*;

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}

7

2011年2月26日土曜日

Page 7: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!import java.io.*;import java.net.*;

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}

8

2011年2月26日土曜日

Page 8: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!import java.io.*;import java.net.*;

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}ポイント: 正しいJavaコードは一般に正

しいGroovyコードでもある(例外もある)

8

2011年2月26日土曜日

Page 9: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!import java.io.*;import java.net.*;

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}

9

2011年2月26日土曜日

Page 10: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!import java.io.*;import java.net.*;

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}

ポイント: java.io.*, java.net.*などは暗黙にimport済み

9

2011年2月26日土曜日

Page 11: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}

10

2011年2月26日土曜日

Page 12: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

public class SocketAccess {

public static void main(String[] args) { Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} } }}

ポイント: クラス定義、mainメソッドは省略可

10

2011年2月26日土曜日

Page 13: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} }

11

2011年2月26日土曜日

Page 14: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

Socket soc = null; InputStream ins = null; OutputStream outs = null; try { soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); try { if (soc != null) soc.close(); } catch(IOException ex) {} }

ポイント: チェック例外も非チェック扱い

11

2011年2月26日土曜日

Page 15: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

Socket soc = null; InputStream ins = null; OutputStream outs = null; soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); }

if (soc != null) soc.close();

12

2011年2月26日土曜日

Page 16: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

Socket soc = null; InputStream ins = null; OutputStream outs = null; soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

BufferedReader bis = new BufferedReader(new InputStreamReader(ins)); String line; while ((line = bis.readLine()) != null) { System.out.println(line); }

if (soc != null) soc.close();

ポイント: 変数の型宣言は省略可能

12

2011年2月26日土曜日

Page 17: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

def soc = null; def ins = null; def outs = null; soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

bis = new BufferedReader(new InputStreamReader(ins)); def line = null; while ((line = bis.readLine()) != null) { System.out.println(line); }

if (soc != null) soc.close();

13

2011年2月26日土曜日

Page 18: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

def soc = null; def ins = null; def outs = null; soc = new Socket("www.java-users.jp", 80); ins = soc.getInputStream(); outs = soc.getOutputStream(); outs.write("GET / HTTP/1.0\n\n".getBytes());

bis = new BufferedReader(new InputStreamReader(ins)); def line = null; while ((line = bis.readLine()) != null) { System.out.println(line); }

if (soc != null) soc.close();

ポイント: 読み込みストリームの自動クローズなど

13

2011年2月26日土曜日

Page 19: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs ->

outs.write("GET / HTTP/1.0\n\n".getBytes());

bis = new BufferedReader(new InputStreamReader(ins)); def line = null; while ((line = bis.readLine()) != null) { System.out.println(line); } }

14

2011年2月26日土曜日

Page 20: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs ->

outs.write("GET / HTTP/1.0\n\n".getBytes());

bis = new BufferedReader(new InputStreamReader(ins)); def line = null; while ((line = bis.readLine()) != null) { System.out.println(line); } }

ポイント: 行読み込みストリームに対する行単位の処理と、自動クローズ

14

2011年2月26日土曜日

Page 21: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs ->

outs.write("GET / HTTP/1.0\n\n".getBytes());

ins.eachLine{ line -> System.out.println(line); } }

15

2011年2月26日土曜日

Page 22: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".getBytes()); ins.eachLine { line -> System.out.println(line); } }

16

2011年2月26日土曜日

Page 23: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".getBytes()); ins.eachLine { line -> System.out.println(line); } }

17

2011年2月26日土曜日

Page 24: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".getBytes()); ins.eachLine { line -> System.out.println(line); } } ポイント: プロパティア

クセス記法でgetterを呼び出せる

17

2011年2月26日土曜日

Page 25: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes); ins.eachLine { line -> System.out.println(line); } }

18

2011年2月26日土曜日

Page 26: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes); ins.eachLine { line -> System.out.println(line); } } ポイント: System.out.println,

Ssytem.out.printなどはprintln,printと書ける

18

2011年2月26日土曜日

Page 27: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes); ins.eachLine { line -> println(line); } }

19

2011年2月26日土曜日

Page 28: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes); ins.eachLine { line -> println(line); } } ポイント: クロージャの引数

が1つなら、暗黙のクロージャ引数itで参照できる。

19

2011年2月26日土曜日

Page 29: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes); ins.eachLine { println(it); } }

20

2011年2月26日土曜日

Page 30: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes); ins.eachLine { println(it); } } ポイント: 行末のセミコロ

ンは多くの場合省略可

20

2011年2月26日土曜日

Page 31: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes) ins.eachLine { println(it) } }

21

2011年2月26日土曜日

Page 32: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write("GET / HTTP/1.0\n\n".bytes) ins.eachLine { println(it) } } ポイント: 式文のトップレ

ベルがメソッド呼び出しなら引数の括弧は省略可能

21

2011年2月26日土曜日

Page 33: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

コード例: Groovy!

new Socket("www.java-users.jp", 80).withStreams { ins, outs -> outs.write "GET / HTTP/1.0\n\n".bytes ins.eachLine { println it } }

22

2011年2月26日土曜日

Page 34: Jenkins and Groovy

Slide # DevLOVE ぐるぐるGroovy 2011.1.24

HTTPに限ればprintln new URL("http://www.java-users.jp").text

23

2011年2月26日土曜日

Page 35: Jenkins and Groovy

Grape

Maven/Ivyベースのモジュール管理機能

依存関係も解決

必要なjarファイルを実行時にダウンロード

2回目以降はキャッシュを利用

実行時にクラスパスに追加

2011年2月26日土曜日

Page 36: Jenkins and Groovy

Grapeアノテーションで

@Grab(‘groupId:artifactId:version’)

コードで

import groovy.grape.Grape

Grape.grab(group:'groupId', module:'artifactId', version:'version')

コマンドで

grape install <groupId> <artifactId> <version>

2011年2月26日土曜日

Page 37: Jenkins and Groovy

@Grab('org.apache.poi:poi:3.2-FINAL')import org.apache.poi.hssf.usermodel.*

def workBook = new HSSFWorkbook(new File('./foo.xls')workBook.newInputStream()).sheets.each { sheet -> sheet.firstRowNum.upto(sheet.lastRowNum) { sheet.getRow(it).with { row -> row.firstCellNum.upto(row.lastCellNum - 1) { println row.getCell(it).stringCellValue } } }}

@Grabコード例 (その1)

2011年2月26日土曜日

Page 38: Jenkins and Groovy

@Grab('org.mortbay.jetty:jetty-embedded:6.1.25')@Grab('com.h2database:h2:1.2.144')@Grab('mysql:mysql-connector-java:5.1.13')import org.mortbay.jetty.Serverimport org.mortbay.jetty.servlet.Contextimport org.h2.server.web.WebServlet

def server = new Server(8080)new Context(server, "/", Context.SESSIONS).addServlet(WebServlet, "/*")

server.start()

@Grabコード例 (その1)

2011年2月26日土曜日

Page 39: Jenkins and Groovy

2011年2月26日土曜日

Page 40: Jenkins and Groovy

インストールhttp://groovy.codehaus.org/Download

$ unzip groovy-binary-1.7.8.zip -d $JENKINS_HOME/tools/$ chmod -R +x /opt/groovy-1.7.8/$ export GROOVY_HOME=/opt/groovy-1.7.8/$ export PATH=$GROOVY_HOME/bin

2011年2月26日土曜日

Page 41: Jenkins and Groovy

groovysh

2011年2月26日土曜日

Page 42: Jenkins and Groovy

groovyConsole

2011年2月26日土曜日

Page 43: Jenkins and Groovy

ぐるーびー�と じぇんきんす

2011年2月26日土曜日

Page 44: Jenkins and Groovy

GroovyとJenkins

GroovyでJenkinsを管理する

Jenkins上でGroovyを実行する

2011年2月26日土曜日

Page 45: Jenkins and Groovy

Jenkinsを管理する

2011年2月26日土曜日

Page 46: Jenkins and Groovy

Groovyで管理$JENKINS_HOME/init.groovy

スクリプトコンソール

http://jenkinsserver/script

Jenkins CLI

ローカルGroovyスクリプトをJenkins上で実行

groovyshをJenkins上で実行

2011年2月26日土曜日

Page 47: Jenkins and Groovy

init.groovy

$JENKINS_HOME/init.groovyに起動時の処理を記述できる

Post-initialization scriptimport hudson.model.*;

// start in the state that doesn't do any build.Hudson.instance.doQuietDown();

http://wiki.jenkins-ci.org/display/JENKINS/Post-initialization+script

2011年2月26日土曜日

Page 48: Jenkins and Groovy

スクリプトコンソールブラウザからGroovyコードをJenkins上で実行

http://jenkinsserver/script

2011年2月26日土曜日

Page 49: Jenkins and Groovy

Jenkins CLI (Script)$ java -jar jenkins-cli.jar \> -s http://jenkinsserver \> groovy hoge.groovy

http://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

http://scriptlerweb.appspot.com/

2011年2月26日土曜日

Page 50: Jenkins and Groovy

Jenkins CLI (groovysh)$ java -jar jenkins-cli.jar \> -s http://jenkinsserver groovysh

2011年2月26日土曜日

Page 51: Jenkins and Groovy

便利ですが...

スクリプトコンソール

コードを試しながら実行するのには不便

Groovyスクリプト実行

エディタとコマンドを行ったり来たり

groovysh

慣れるまでが大変2011年2月26日土曜日

Page 52: Jenkins and Groovy

JenkinsGroovyConsoleGroovyConsoleの実行先をJenkinsに変更

ファイルの読み込み、保存、インデント、

ハイライトなどGroovyConsoleの機能そのまま

jenkins-cli.jarを準備する必要なし

groovy JenkinsGroovyConsole [jenkins_url]

https://gist.github.com/843507

2011年2月26日土曜日

Page 53: Jenkins and Groovy

@GrabResolver('http://maven.glassfish.org/content/groups/public/')@Grab('org.jenkins-ci.main:cli:1.398')import hudson.cli.CLIimport groovy.ui.Console

jenkinsUrl = args ? args[0] : 'http://localhost:8080'Console.metaClass.newScript = { ClassLoader parent, Binding binding -> delegate.shell = new GroovyShell(parent, binding) delegate.shell.metaClass.run = { String scriptText, String fileName, List list -> def file = File.createTempFile('jenkinsgroovyconsole', '.groovy') file.text = scriptText new CLI(jenkinsUrl.toURL()).execute(['groovy', file.absolutePath], System.in, System.out, System.err) file.delete() null }}new Console(Console.class.classLoader.getRootLoader()).run()

2011年2月26日土曜日

Page 54: Jenkins and Groovy

Demo

スレーブを追加する(JNLP)

スレーブを追加する(SSH)

-Dmven.test.skip=true

2011年2月26日土曜日

Page 55: Jenkins and Groovy

CIではないジョブをGroovyで

2011年2月26日土曜日

Page 56: Jenkins and Groovy

まずは、JenkinsでGroovyを使う準備

2011年2月26日土曜日

Page 57: Jenkins and Groovy

1. Groovyプラグインを入れる

2011年2月26日土曜日

Page 58: Jenkins and Groovy

2. Groovyをインストールhttp://groovy.codehaus.org/Download

$ unzip groovy-binary-1.7.8.zip -d $JENKINS_HOME/tools/$ chmod -R +x $JENKINS_HOME/tools/groovy-1.7.8/

2011年2月26日土曜日

Page 59: Jenkins and Groovy

3. Jenkinsの設定

http://jenkinsserver/configure

2011年2月26日土曜日

Page 60: Jenkins and Groovy

GroovyなJobを作成

2011年2月26日土曜日

Page 61: Jenkins and Groovy

2011年2月26日土曜日

Page 62: Jenkins and Groovy

利用例Commons VFSを使ってリモートのファイルをバックアップ

Jenkins用にプロジェクトの設定ファイルを書き換える

ビルドとは関係なくIRCにくだらない事を

流すBotを動かす

2011年2月26日土曜日

Page 63: Jenkins and Groovy

Groovyをもっと知るためにもっとみんなに知ってほしい

日本Grails/Groovyユーザグループ(JGGUG)

http://www.jggug.org

首都圏、名古屋、関西、仙台などで勉強会

定期(?)電子書籍 G*Magazine 発行

http://grails.jp/g_mag_jp/

2011年2月26日土曜日

Page 64: Jenkins and Groovy

ご清聴ありがとうございました。

2011年2月26日土曜日