75
01

Jenkinsfile: friend or foe for GR8conf 2017

Embed Size (px)

Citation preview

Page 1: Jenkinsfile: friend or foe for GR8conf 2017

01

Page 2: Jenkinsfile: friend or foe for GR8conf 2017

02

Page 3: Jenkinsfile: friend or foe for GR8conf 2017

Let's start!03

Page 4: Jenkinsfile: friend or foe for GR8conf 2017

Historylesson

04

Page 5: Jenkinsfile: friend or foe for GR8conf 2017

A bit of history: HudsonWork started in the summer of 2004 at Sun by Kohsuke Kawaguchi.

First release: February 2005.

In May, 2008, Hudson won the Duke's Choice Award in the Developer

Solutions category.

In 2010, Sun is acquired by Oracle... discussions, negotiations.

Kohsuke Kawaguchi leaves Oracle.

•••

••

05

Page 6: Jenkinsfile: friend or foe for GR8conf 2017

A bit of history: JenkinsIn 2010, majority of Hudson's community votes for forking and name

changing.

Initial release of Jenkins: 2nd of February, 2011.

Oracle still supports and develops Hudson.

Around 2010­2011, CloudBees is created and declared Jenkins as its

backbone product.

•••

06

Page 7: Jenkinsfile: friend or foe for GR8conf 2017

Pipelines inJenkins

07

Page 8: Jenkinsfile: friend or foe for GR8conf 2017

Steps

08

Page 9: Jenkinsfile: friend or foe for GR8conf 2017

Steps

09

Page 10: Jenkinsfile: friend or foe for GR8conf 2017

Job DSLjob(jobName) {

  scm {

    git("git://github.com/project.git", branchName)

  }

  steps {

    maven("clean test")

    maven("install")

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

09. 10

Page 11: Jenkinsfile: friend or foe for GR8conf 2017

Gradle + Job DSL

11

Page 12: Jenkinsfile: friend or foe for GR8conf 2017

Gradle + Job DSL

12

Page 13: Jenkinsfile: friend or foe for GR8conf 2017

Build flow

13

Page 14: Jenkinsfile: friend or foe for GR8conf 2017

Build flow

14

Page 15: Jenkinsfile: friend or foe for GR8conf 2017

Build flow

15

Page 16: Jenkinsfile: friend or foe for GR8conf 2017

Build flow

16

Page 17: Jenkinsfile: friend or foe for GR8conf 2017

Build flow

17

Page 18: Jenkinsfile: friend or foe for GR8conf 2017

Build flow

18

Page 19: Jenkinsfile: friend or foe for GR8conf 2017

Groovy Postbuild

19

Page 20: Jenkinsfile: friend or foe for GR8conf 2017

Groovy Postbuild

20

Page 21: Jenkinsfile: friend or foe for GR8conf 2017

Groovy Postbuild

21

Page 22: Jenkinsfile: friend or foe for GR8conf 2017

It still works,but...

22

Page 23: Jenkinsfile: friend or foe for GR8conf 2017

Future ahead!23

Page 24: Jenkinsfile: friend or foe for GR8conf 2017

Pipeline­as­code

24

Page 25: Jenkinsfile: friend or foe for GR8conf 2017

Jenkinsfilenode {

  stage('build') {

  }

  stage('test') {

  }

  stage('deploy') {

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

25

Page 26: Jenkinsfile: friend or foe for GR8conf 2017

Jenkinsfilepipeline {

  agent any

  stages {

    ...

  }

}

01.

02.

03.

04.

05.

06.

26

Page 27: Jenkinsfile: friend or foe for GR8conf 2017

Stage view

27

Page 28: Jenkinsfile: friend or foe for GR8conf 2017

New job types

28

Page 29: Jenkinsfile: friend or foe for GR8conf 2017

Folder types

29

Page 30: Jenkinsfile: friend or foe for GR8conf 2017

Pipeline script aka Jenkinfile

30

Page 31: Jenkinsfile: friend or foe for GR8conf 2017

Questions in 2016Why not Job DSL?

Why not extending Build Flow?

Why no integration with existing plug­ins?

Why? Why? Why?

••••

31

Page 32: Jenkinsfile: friend or foe for GR8conf 2017

Problems in 2016No good documentation

No easy to use examples and tutorials

User interface is, well,... puzzling!

Missing functionality and unstable DSL/API

Lack of plugins and integrations

•••••

32

Page 33: Jenkinsfile: friend or foe for GR8conf 2017

BUT...33

Page 34: Jenkinsfile: friend or foe for GR8conf 2017

It'simproving!34

Page 35: Jenkinsfile: friend or foe for GR8conf 2017

Updated documentation

35

Page 36: Jenkinsfile: friend or foe for GR8conf 2017

Snippet generator

36

Page 37: Jenkinsfile: friend or foe for GR8conf 2017

Snippet generator

37

Page 38: Jenkinsfile: friend or foe for GR8conf 2017

DSLexamples

38

Page 39: Jenkinsfile: friend or foe for GR8conf 2017

DSL: read/write fileswriteFile file: "output/usefulfile.txt", 

          text: "This file is useful, need to archive it."

...

String content = readFile file: "output/usefulfile.txt"

01.

02.

03.

04.

39

Page 40: Jenkinsfile: friend or foe for GR8conf 2017

DSL: archivearchiveArtifacts artifacts: 'output/*.txt', 

                 excludes: 'output/*.md'

archiveArtifacts allowEmptyArchive: true, 

                 artifacts: "**/build/libs/*.jar", 

                 fingerprint: true

01.

02.

03.

04.

05.

40

Page 41: Jenkinsfile: friend or foe for GR8conf 2017

DSL: current directorynode {

  stage('report') {

    dir('tmp') {

      sh "mkdir ‐p output"

    }

    ...

  }

01.

02.

03.

04.

05.

06.

07.

41

Page 42: Jenkinsfile: friend or foe for GR8conf 2017

DSL: withEnvwithEnv(['MYTOOL_HOME=/usr/local/mytool']) {

  sh '$MYTOOL_HOME/bin/start'

}

01.

02.

03.

42

Page 43: Jenkinsfile: friend or foe for GR8conf 2017

DSL: stashingstash name: "first‐stash", includes: "output/*"

...

unstash "first‐stash" 

01.

02.

03.

43

Page 44: Jenkinsfile: friend or foe for GR8conf 2017

DSL: timestampstimestamps {

  stage "First echo"

  echo "Hey, look, I'm echoing with a timestamp!"

  stage "Sleeping"

  sleep 30

  stage "Second echo"

  echo "Wonder what time it is now?"

}

01.

02.

03.

04.

05.

06.

07.

08.

44

Page 45: Jenkinsfile: friend or foe for GR8conf 2017

DSL: junitstage('test') {

  try {

    sh './gradlew test'

  } finally {

    junit allowEmptyResults: true, 

          keepLongStdio: true, 

          testResults: '**/build/test‐results/**/*.xml'

  }    

}

01.

02.

03.

04.

05.

06.

07.

08.

09. 45

Page 46: Jenkinsfile: friend or foe for GR8conf 2017

DSL: listing changesfor (def changeSetList : currentBuild.changeSets) {

  for (def changeSet : changeSetList) {

    println "Message: ${changeSet.comment}"

    for (String path: changeSet.affectedPaths) {

      println "Affected path: ${path}"

    }

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

46

Page 47: Jenkinsfile: friend or foe for GR8conf 2017

DSL: control exceptionsnode { try {

    ...

} catch (e) {

    currentBuild.result = "FAILURE"

    throw e

} finally {

    ...

}}

01.

02.

03.

04.

05.

06.

07.

08.

47

Page 48: Jenkinsfile: friend or foe for GR8conf 2017

DSL: slack notifydef buildStatus = currentBuild.result ?: "SUCCESS"

def subject = "*build and deployment of testapi*\n ${buildStatus}: Job '${env.JOB_NAME}'"

def summary = "${subject} (https://jenkins/job/${env.JOB_NAME}/${env.BUILD_NUMBER})"

def color = currentBuild.result == "FAILURE" ? "#D24939" : "#3D732B"

slackSend(color: color, channel: '#backend', message: summary, 

          teamDomain: 'jenkins', token: '...')

01.

02.

03.

04.

05.

06.

48

Page 49: Jenkinsfile: friend or foe for GR8conf 2017

DSL: build wrapperswrap([$class: 'AnsiColorBuildWrapper']) {

    // Just some echoes to show the ANSI color.

    stage "\u001B[31mI'm Red\u001B[0m Now not"

}

01.

02.

03.

04.

49

Page 50: Jenkinsfile: friend or foe for GR8conf 2017

DSL: dockerdocker.image('maven:3.3.3‐jdk‐8').inside {

  git 'http://github.com/mysource.git'

  sh 'mvn ‐B clean install'

}

01.

02.

03.

04.

50

Page 51: Jenkinsfile: friend or foe for GR8conf 2017

DSL: parallelparallel(

  "test": {

     sh './gradlew check'

  },

  "docs": {

     sh './gradlew javadoc'

  }

)

01.

02.

03.

04.

05.

06.

07.

08.

51

Page 52: Jenkinsfile: friend or foe for GR8conf 2017

DSL: retryretry(2) {

  // Yes, flakey tests are bad!

  sh './gradlew test'

}

01.

02.

03.

04.

52

Page 53: Jenkinsfile: friend or foe for GR8conf 2017

PitfallsGroovy is sandboxed during execution of Jenkinsfile

Not all libraries can be used directly

Not all plug­ins expose functionality usable in Jenkinsfile

•••

53

Page 54: Jenkinsfile: friend or foe for GR8conf 2017

Pipelinelibraries

54

Page 55: Jenkinsfile: friend or foe for GR8conf 2017

Library structure

55

Page 56: Jenkinsfile: friend or foe for GR8conf 2017

LevelsGlobal library

Folder library

Jenkinsfile

•••

56

Page 57: Jenkinsfile: friend or foe for GR8conf 2017

Use libraries@Library('somelib')

import com.mycorp.pipeline.somelib.Helper

int useSomeLib(Helper helper) {

   helper.prepare()

   return helper.count()

}

echo useSomeLib(new Helper('some text'))

01.

02.

03.04.

05.

06.

07.

08.09.

57

Page 58: Jenkinsfile: friend or foe for GR8conf 2017

Use libraries@Library('github.com/fabric8io/' + 

         'fabric8‐pipeline‐library@master')

gitTag {

  releaseVersion = '0.0.1'

}

01.

02.

03.04.

05.

06.

58

Page 59: Jenkinsfile: friend or foe for GR8conf 2017

Blue ocean59

Page 60: Jenkinsfile: friend or foe for GR8conf 2017

Blue oceanJenkins UI redesign effort

Can be installed as a plugin to Jenkins 2.x

Reached v1.0 in 2017

•••

60

Page 61: Jenkinsfile: friend or foe for GR8conf 2017

Install

61

Page 62: Jenkinsfile: friend or foe for GR8conf 2017

Contributions

62

Page 63: Jenkinsfile: friend or foe for GR8conf 2017

Dashboard

63

Page 64: Jenkinsfile: friend or foe for GR8conf 2017

Pipeline view

64

Page 65: Jenkinsfile: friend or foe for GR8conf 2017

Create pipeline

65

Page 66: Jenkinsfile: friend or foe for GR8conf 2017

Edit pipeline

66

Page 67: Jenkinsfile: friend or foe for GR8conf 2017

Save pipeline

67

Page 68: Jenkinsfile: friend or foe for GR8conf 2017

Success screen

68

Page 69: Jenkinsfile: friend or foe for GR8conf 2017

Failure screen

69

Page 70: Jenkinsfile: friend or foe for GR8conf 2017

Conclusion70

Page 71: Jenkinsfile: friend or foe for GR8conf 2017

Take­awaysJenkins is still a friend!

Jenkins is evolving and changing!

It will take some time...

Just be patient :)!

••••

71

Page 72: Jenkinsfile: friend or foe for GR8conf 2017

Referenceshttps://jenkins.io/doc/book/pipeline/

https://jenkins.io/doc/pipeline/steps/

https://jenkins.io/doc/pipeline/examples/

•••

72

Page 73: Jenkinsfile: friend or foe for GR8conf 2017

That's all!73

Page 74: Jenkinsfile: friend or foe for GR8conf 2017

Thank you!74

Page 75: Jenkinsfile: friend or foe for GR8conf 2017

75