Jenkinsfile: friend or foe for GR8conf 2017

Post on 22-Jan-2018

268 views 2 download

Transcript of Jenkinsfile: friend or foe for GR8conf 2017

01

02

Let's start!03

Historylesson

04

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

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

Pipelines inJenkins

07

Steps

08

Steps

09

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

Gradle + Job DSL

11

Gradle + Job DSL

12

Build flow

13

Build flow

14

Build flow

15

Build flow

16

Build flow

17

Build flow

18

Groovy Postbuild

19

Groovy Postbuild

20

Groovy Postbuild

21

It still works,but...

22

Future ahead!23

Pipeline­as­code

24

Jenkinsfilenode {

  stage('build') {

  }

  stage('test') {

  }

  stage('deploy') {

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

25

Jenkinsfilepipeline {

  agent any

  stages {

    ...

  }

}

01.

02.

03.

04.

05.

06.

26

Stage view

27

New job types

28

Folder types

29

Pipeline script aka Jenkinfile

30

Questions in 2016Why not Job DSL?

Why not extending Build Flow?

Why no integration with existing plug­ins?

Why? Why? Why?

••••

31

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

BUT...33

It'simproving!34

Updated documentation

35

Snippet generator

36

Snippet generator

37

DSLexamples

38

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

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

                 excludes: 'output/*.md'

archiveArtifacts allowEmptyArchive: true, 

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

                 fingerprint: true

01.

02.

03.

04.

05.

40

DSL: current directorynode {

  stage('report') {

    dir('tmp') {

      sh "mkdir ‐p output"

    }

    ...

  }

01.

02.

03.

04.

05.

06.

07.

41

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

  sh '$MYTOOL_HOME/bin/start'

}

01.

02.

03.

42

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

...

unstash "first‐stash" 

01.

02.

03.

43

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

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

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

DSL: control exceptionsnode { try {

    ...

} catch (e) {

    currentBuild.result = "FAILURE"

    throw e

} finally {

    ...

}}

01.

02.

03.

04.

05.

06.

07.

08.

47

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

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

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

DSL: parallelparallel(

  "test": {

     sh './gradlew check'

  },

  "docs": {

     sh './gradlew javadoc'

  }

)

01.

02.

03.

04.

05.

06.

07.

08.

51

DSL: retryretry(2) {

  // Yes, flakey tests are bad!

  sh './gradlew test'

}

01.

02.

03.

04.

52

PitfallsGroovy is sandboxed during execution of Jenkinsfile

Not all libraries can be used directly

Not all plug­ins expose functionality usable in Jenkinsfile

•••

53

Pipelinelibraries

54

Library structure

55

LevelsGlobal library

Folder library

Jenkinsfile

•••

56

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

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

         'fabric8‐pipeline‐library@master')

gitTag {

  releaseVersion = '0.0.1'

}

01.

02.

03.04.

05.

06.

58

Blue ocean59

Blue oceanJenkins UI redesign effort

Can be installed as a plugin to Jenkins 2.x

Reached v1.0 in 2017

•••

60

Install

61

Contributions

62

Dashboard

63

Pipeline view

64

Create pipeline

65

Edit pipeline

66

Save pipeline

67

Success screen

68

Failure screen

69

Conclusion70

Take­awaysJenkins is still a friend!

Jenkins is evolving and changing!

It will take some time...

Just be patient :)!

••••

71

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

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

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

•••

72

That's all!73

Thank you!74

75