Jenkins and Groovy
-
Upload
kiyotaka-oku -
Category
Technology
-
view
4.674 -
download
0
description
Transcript of Jenkins and Groovy
Jenkinsエンジニアのためのモテる Groovy入門
2011年2月26日土曜日
自己紹介奥清隆 (@kiy0taka、id:kiy0taka)
大阪在住
日本Grails/Groovyユーザグループ
Jenkins Plugin
Terminal、jQuery、jQuery UI
前回のLT「モテるHudsonエンジニア」
http://www.ustream.tv/recorded/108018382011年2月26日土曜日
モテなかったクリスマスも一人。
チョコも貰ってません。
合コンにも誘われてない。
結論
「Jenkinsエンジニアはモテない。ソースは俺。」
2011年2月26日土曜日
リア充爆発しろ!
2011年2月26日土曜日
Slide # DevLOVE ぐるぐるGroovy 2011.1.24
GroovyJVM上で動作するスクリプト言語簡潔・高機能記述量Java比1/2~1/5
Javaとの高い相互運用性Groovy Class=Java ClassGroovy Object=Java Object
6
2011年2月26日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
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日土曜日
Slide # DevLOVE ぐるぐるGroovy 2011.1.24
HTTPに限ればprintln new URL("http://www.java-users.jp").text
23
2011年2月26日土曜日
Grape
Maven/Ivyベースのモジュール管理機能
依存関係も解決
必要なjarファイルを実行時にダウンロード
2回目以降はキャッシュを利用
実行時にクラスパスに追加
2011年2月26日土曜日
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日土曜日
@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日土曜日
@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日土曜日
2011年2月26日土曜日
インストール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日土曜日
groovysh
2011年2月26日土曜日
groovyConsole
2011年2月26日土曜日
ぐるーびー�と じぇんきんす
2011年2月26日土曜日
GroovyとJenkins
GroovyでJenkinsを管理する
Jenkins上でGroovyを実行する
2011年2月26日土曜日
Jenkinsを管理する
2011年2月26日土曜日
Groovyで管理$JENKINS_HOME/init.groovy
スクリプトコンソール
http://jenkinsserver/script
Jenkins CLI
ローカルGroovyスクリプトをJenkins上で実行
groovyshをJenkins上で実行
2011年2月26日土曜日
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日土曜日
スクリプトコンソールブラウザからGroovyコードをJenkins上で実行
http://jenkinsserver/script
2011年2月26日土曜日
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日土曜日
Jenkins CLI (groovysh)$ java -jar jenkins-cli.jar \> -s http://jenkinsserver groovysh
2011年2月26日土曜日
便利ですが...
スクリプトコンソール
コードを試しながら実行するのには不便
Groovyスクリプト実行
エディタとコマンドを行ったり来たり
groovysh
慣れるまでが大変2011年2月26日土曜日
JenkinsGroovyConsoleGroovyConsoleの実行先をJenkinsに変更
ファイルの読み込み、保存、インデント、
ハイライトなどGroovyConsoleの機能そのまま
jenkins-cli.jarを準備する必要なし
groovy JenkinsGroovyConsole [jenkins_url]
https://gist.github.com/843507
2011年2月26日土曜日
@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日土曜日
Demo
スレーブを追加する(JNLP)
スレーブを追加する(SSH)
-Dmven.test.skip=true
2011年2月26日土曜日
CIではないジョブをGroovyで
2011年2月26日土曜日
まずは、JenkinsでGroovyを使う準備
2011年2月26日土曜日
1. Groovyプラグインを入れる
2011年2月26日土曜日
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日土曜日
3. Jenkinsの設定
http://jenkinsserver/configure
2011年2月26日土曜日
GroovyなJobを作成
2011年2月26日土曜日
2011年2月26日土曜日
利用例Commons VFSを使ってリモートのファイルをバックアップ
Jenkins用にプロジェクトの設定ファイルを書き換える
ビルドとは関係なくIRCにくだらない事を
流すBotを動かす
2011年2月26日土曜日
Groovyをもっと知るためにもっとみんなに知ってほしい
日本Grails/Groovyユーザグループ(JGGUG)
http://www.jggug.org
首都圏、名古屋、関西、仙台などで勉強会
定期(?)電子書籍 G*Magazine 発行
http://grails.jp/g_mag_jp/
2011年2月26日土曜日
ご清聴ありがとうございました。
2011年2月26日土曜日