jcconf2016 jenkins pipeline

17
Jenkins Pipeline 簡介 qrtt1 #快講

Upload: ching-yi-chan

Post on 23-Jan-2018

478 views

Category:

Technology


0 download

TRANSCRIPT

Jenkins Pipeline 簡介qrtt1

#快講

什麼是 Pipeline• Delivery Pipeline (from Continuous Delivery)

• Jenkins Workflow Plugins

• Job Workflow Pipeline

• Pipeline 由多個 Step 組成,並且⽀支援 Pause/Resume

• Pipeline 內的狀態必需可序列化 (Serializable)

• 或宣告為不必列⼊入狀態 @NonCPS 的 function

為什麼是 Pipeline• 依然是 Jenkins Job 的⼀一種,依然很 FreeStyle。

• 為的是 Pipeline as Code

• 它是 groovy DSL (domain specific language)

版本控制

比 FreeStyle 更 FreeStyle

Pipeline DSL• Jenkins 的 Pipeline 介紹

https://jenkins.io/solutions/pipeline/ • Pipeline 寫作教學

https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md • Pipeline 設計說明

https://github.com/jenkinsci/workflow-cps-plugin/blob/master/README.md • Pipeline Steps Reference

https://jenkins.io/doc/pipeline/steps/ • Pipeline 最佳實踐

https://www.cloudbees.com/blog/top-10-best-practices-jenkins-pipeline-plugin

主要看這個

遇到問題查這個

建⽴立 Pipeline Job• Pipeline Script from in-place editor

• Pipeline Script from SCM

• Multi-branch Pipeline (from SCM)推薦使用 SCM 的方式

適合單 1 專案,不同階段的 Pipeline 自動建立

僅推薦應用於證驗 Script 語法

建⽴立 Pipeline 的⽅方式• Pipeline Script Editor

建⽴立 Pipeline 的⽅方式• Pipeline Script from SCM

Pipeline 規劃SCM Checkout

WarBuild Test

SCM Commit

DeployRelease

Pipeline Script DEMO/Overview

Steps: node, stage, echonode { stage ('checkout') { echo 'checkout from git' } stage ('build') { echo 'build' } // … skip … }

在哪一台機器跑 哪一個階段

做些什麼

Steps: git

stage ('checkout') { echo 'checkout from git' git url: 'https://my-scm/path/my-project.git' branch: 'master' }

optional

Steps: dir, pwd, sh (bat)dir ('project') { pwd() stage ('build') { sh """gradle clean build""" } stage ('test') { sh """gradle check""" } // … skip … }

Steps: step, archiveArtifactsstage ('archive') { step([$class: 'ArtifactArchiver', artifacts: "build/**/*.war", fingerprint: true]) }

stage ('archive') { archiveArtifacts artifacts: "build/**/*.war", fingerprint: true }

舊的寫法

新的寫法

requires Jenkins 2.2+ with newer plugins

Refactoring Pipeline• 使⽤用 Function 合併多個 steps 為⼀一個有意義的動作

• 實作新的 Pipeline Step

• 使⽤用 Pipeline Shared Libraries

你瘋了嗎?

無法跨 Pipeline Script 使用

只能推惹!https://github.com/jenkinsci/workflow-cps-global-lib-plugin

https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Shared+Groovy+Libraries+Plugin

Pipeline Shared Library• 使⽤用⽅方法

• 啟⽤用 Jenkins ssh (因為 Jenkins 內建的 git repo ⺫⽬目前僅能 ssh auth)

• 安裝 Plugin (id: workflow-cps-global-lib)

• 使⽤用 git 指令 commit & push 提交 Shared Library 並在 Pipeline

Script 開始使⽤用

https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+SSH

web console 搜尋 plugin 找不到,用 cli 直接按裝吧!

Pipeline 使⽤用⼼心得• Pipeline as Code 可以⽅方便版本管理

• 內建的 Pipeline Step 不夠多,得常得依賴 sh 或 bat 呼叫外部指令。

• 間接使⽤用 DevOps Tool 處理

• Ansible, Chef, Puppet

• Ant, Maven, Gradle

Q & A