learn jobdsl for jenkins

17
Larry cai <[email protected]>

Upload: larry-cai

Post on 14-Aug-2015

224 views

Category:

Technology


1 download

TRANSCRIPT

Larry cai <[email protected]>

Agenda Introduction Exercise 1: Run the example to know the

power Exercise 2: Hello World – first DSL Exercise 3: One complete JobDSL Exercise 4: Jenkins View and Loop Exercise 5: Configure Block to extend DSL Reference

2 04/15/23

Environment Preparation (docker) Boot2docker Installer (127M)

Contains latest docker already, fast Container persistence via disk automount on /var/lib/docker $ docker -v User/Passwd: docker/tcuser (192.168.59.103)

Download Jenkins JobDSL docker images $ docker pull larrycai/jenkins-jobdsl

Notepad++ & MobaXterm are recommended

3 04/15/23

http://boot2docker.io/

Introduction Jenkins is a de-facto CI standard used for lots

of products Easy to start, configure and extending (300+

plugins) Configure jobs in UI mostly Weak parts:

Update git repo for many jobs Create dozens of jobs in fly for new project Monitor the changes

4 04/15/23

Learn new way to configure jenkins Jobs

Exercise 1: Hello World Run first app inside docker environment

$ cd /c/<Users>/codingwithme/jobdsl # create in advance$ docker run -it –v $PWD:/opt/jenkins/data/jobs/jobdsl/workspace -p 8080:8080 larrycai/jenkins-jobdsl

Browse: http://192.168.59.103:8080 Clean jenkins UI with one Job “HelloWorld”

Exercise :1. Execute job “HelloWorld”, and check result

2. See configuration

3. Run it again

4. Delete job “Compile-DSL-Tutorial-0” and Update job “Package-DSL-Tutorial-0” and Run it again

5 04/15/23

JobDSL is Groovy based DSL : Domain Specific Language, easy to read

and understand JobDSL is jenkins plugin,

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

Groovy based

6 04/15/23

• Groovy is a multi-faceted language for the Java platform

• Groovy used in jenkins to access internal data

JobDSL & Idempotent JobDSL helps to simplify the maintenance of jenkins

jobs

An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application Keep the environment stable, reliable

No matter the job is changed by someone, if the jobDSL script is run, it will reach to the desired status 7 04/15/23

Exercise 2:Hello World Configure “HelloWorld” job

Replace script like below

freeStyleJob("Hello2") {

steps {

shell('echo "Hello Larry"')

}

}

Select option “Action for removed jobs” to “Delete” and option “Action for removed views” to “Delete”, Run it again

8 04/15/23

Jenkins Job mapping to JobDSL

9 04/15/23

Job Type (freeStyleJob) Build Parameters (parameters) SCM (scm) Build Triggers (triggers) Steps (steps) Post Build Actions (publishers)

https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-reference

Exercise 3: Complete JobDSL example Follow the existing Jenkins job “Sample” and

convert it into JobDSL in “jobdsl” Cover Scm/trigger/shell/archive

Steps: New job “jobdsl” (freestyle) New build step – Process Job DSLs Choose “Look on Filesystem”

complete.groovy

In file, use freeStyleJob(“Hello3”) Edit it in Windows env. (codingwith/jobdsl) # mapped

into docker Compare the result

10 04/15/23

Reference: https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-reference

Jenkins View & Groovy DSL Jenkins View is simple as

jobs Name/filter/column .

Learn some groovy to handle common tasks Loop Read & Parse external

config files

11 04/15/23

Exercise 4: List View and Loop Create different List View and generate multi

jobs based on the config file Based on HelloWorld Example for jobs Json file: HelloWorld/workspace/views.json

Steps Create new view.groovy Run inside job jobdsl

12 04/15/23

Configure block Not every jenkins Plugin are supported in

JobDSL (may changes in the future) Raise issues to get support

Configure blocks are used inside the Job DSL to give direct access to underlying XML of the Jenkins config.xml

13 04/15/23

https://github.com/jenkinsci/job-dsl-plugin/wiki/The-Configure-Block

Exercise 5: Configure Block for testNG Change the Sample job to export testNG result,

which is not support by jobDSL yet. Steps:

Configure Sample job to publish testNG result in post-build

Check generate config.xml http://localhost:8080/view/CodingWithMe/job/Sample/config.xml

Write testng.groovy based on complete.groovy Generate job Hello5

Compare result14 04/15/23

Summary JobDSL is the powerful to simplify the

maintenance of your jenkins jobs and keep it in professional way Put jobDSL scripts in git repo

What we learn Execute JobDSL in jenkins jobs

Inside script & script file Create job/View in jobDSL Simple Groovy knowledge to handle loop/read

external data Configure Block in jobDSL to handle not direct

supported plugins

15 04/15/23

Reference jobDSL slides

http://www.slideshare.net/daspilker/configuration-as-code-the-job-dsl-plugin

JobDSL ref https://github.com/jenkinsci/job-dsl-plugin/wiki/View-Ref

erence https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-

reference https://github.com/jenkinsci/job-dsl-plugin/wiki/The-

Configure-Block Groovy Json

http://www.groovy-lang.org/json.html Exercise Gist

https://gist.github.com/larrycai/aeb9a3b037dcb17df9b2 16 04/15/23

CodingWithMe "CodingWithMe - Learning by Coding" is a

coding workshop, it aims to learn one technology in 90 minutes by practice

Features: 4-5 well tuned hands on exercises in 90

minutes First exercise shall be started in 20 minutes

It well follows Bob Pike’s 90/20/8 rules (happen to know)

It is recommended arranged for several parallel sessions if it is organized in the company. 17 04/15/23

http://www.larrycaiyu.com/blog/2014/03/18/codingwithme_introduction/