1. Pipeline簡介
1. 概念
Pipeline,簡單來說,就是一套運行在 Jenkins 上的工作流框架,將原來獨立運行於單個或者多個節點
的任務連接起來,實現單個任務難以完成的復雜流程編排和可視化的工作。
2. 使用Pipeline有以下好處(來自翻譯自官方文檔):
- 代碼:
Pipeline以代碼的形式實現,通常被檢入源代碼控制,使團隊能夠編輯,審查和迭代其傳送流程。 - 持久:無論是計划內的還是計划外的服務器重啟,
Pipeline都是可恢復的。 - 可停止:
Pipeline可接收交互式輸入,以確定是否繼續執行Pipeline。 - 多功能:
Pipeline支持現實世界中復雜的持續交付要求。它支持fork/join、循環執行,並行執行任務的功能。 - 可擴展:
Pipeline插件支持其DSL的自定義擴展 ,以及與其他插件集成的多個選項。
3. 如何創建 Jenkins Pipeline呢?
Pipeline腳本是由Groovy語言實現的,但是我們沒必要單獨去學習GroovyPipeline支持兩種語法:Declarative(聲明式)和Scripted Pipeline(腳本式)語法Pipeline也有兩種創建方法:可以直接在Jenkins的Web UI界面中輸入腳本;也可以通過創建一個Jenkinsfile腳本文件放入項目源碼庫中(一般我們都推薦在Jenkins中直接從源代碼控制(SCM)中直接載入JenkinsfilePipeline這種方法)。
2. 安裝Pipeline插件
Manage Jenkins->Manage Plugins->可選插件



如果安裝插件失敗可以點高級,里面選擇上傳插件,手動上傳插件包,插件包的下載地址
http://updates.jenkins-ci.org/download/plugins/

安裝插件后,創建項目的時候多了 “流水線”類型

3. Pipeline語法快速入門
1. Declarative聲明式-Pipeline
創建項目

點擊確定
流水線 ->選擇HelloWorld模板

生成內容如下:
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
stages :代表整個流水線的所有執行階段。通常stages只有1個,里面包含多個stage
stage :代表流水線中的某個階段,可能出現n個。一般分為拉取代碼,編譯構建,部署等階段。
steps:代表一個階段內需要執行的邏輯。steps里面是shell腳本,git拉取代碼,ssh遠程發布等任意內容。
編寫一個簡單聲明式Pipeline:
pipeline {
agent any
stages {
stage('pull code') {
steps {
echo 'pull code'
}
}
stage('build code') {
steps {
echo 'build code'
}
}
stage('deploy project') {
steps {
echo 'deploy project'
}
}
}
}
點擊應用,保存,點擊Build Now,可以看到整個構建過程


2. Scripted Pipeline腳本式-Pipeline
創建項目

點擊確定
這次選擇 "Scripted Pipeline"

node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'M3'
}
stage('Build') {
// Run the maven build
withEnv(["MVN_HOME=$mvnHome"]) {
if (isUnix()) {
sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
} else {
bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archiveArtifacts 'target/*.jar'
}
}
Node:節點,一個Node就是一個Jenkins節點,Master或者Agent,是執行Step的具體運行環境,后續講到Jenkins的Master-Slave架構的時候用到。Stage:階段,一個Pipeline可以划分為若干個Stage,每個Stage代表一組操作,比如:Build、Test、Deploy,Stage是一個邏輯分組的概念。Step:步驟,Step是最基本的操作單元,可以是打印一句話,也可以是構建一個Docker鏡像,由各類Jenkins插件提供,比如命令:sh ‘make’,就相當於我們平時shell終端中執行make命令一樣。
編寫一個簡單的腳本式Pipeline
node {
def mvnHome
stage('pull code') {
echo '拉取代碼'
}
stage('build code') {
echo '編譯構建代碼'
}
stage('deploy code') {
echo '部署代碼'
}
}
應用,保存,執行Build Now

4. 從github拉取代碼,並編譯部署
新建Item

點擊確定
在流水線中選擇Hello World,點擊流水線語法

這里有工具可以生成需要的代碼腳本

1. 生成gitlab拉取代碼腳本

填入gitlab的ssh地址憑證填入gitlab配置的憑證,選擇對應分支,點擊生成流水線腳本

將生成的代碼復制出來備用
git credentialsId: 'cce455e2-ba69-459e-93bc-c58ce1e6278f', url: 'ssh://git@192.168.2.4:23456/itheima_group/web_demo.git'
2. 生成編譯構建腳本


sh label: '', script: 'mvn clean package'
3. 生成部署項目腳本

在jenkins的憑證中添加Tomcat用戶憑證
在tomcat部署服務器上配置tomcat的用戶角色

deploy adapters: [tomcat9(credentialsId: 'da6aa960-cb2b-415f-a31e-e161ba704f28', path: '', url: 'http://192.168.2.5:8080')], contextPath: null, war: 'target/*.war'
4. 整合流水線腳本
pipeline {
agent any
stages {
stage('pull code') {
steps {
git credentialsId: 'cce455e2-ba69-459e-93bc-c58ce1e6278f', url: 'ssh://git@192.168.2.4:23456/itheima_group/web_demo.git'
}
}
stage('build code') {
steps {
sh label: '', script: 'mvn clean package'
}
}
stage('deploy code') {
steps {
deploy adapters: [tomcat9(credentialsId: 'da6aa960-cb2b-415f-a31e-e161ba704f28', path: '', url: 'http://192.168.2.5:8080')], contextPath: null, war: 'target/*.war'
}
}
}
}

應用,保存
5. 執行
點擊Build Now



部署成功
5. 將Jenkins腳本放入項目中
剛才我們都是直接在Jenkins的UI界面編寫Pipeline代碼,這樣不方便腳本維護,建議把Pipeline腳本放
在項目中(一起進行版本控制)
1. 在項目根目錄建立Jenkinsfile文件,把內容復制到該文件中

把 Jenkinsfile上傳到Gitlab
git add .
git commit -m "add Jenkinsfile"
git push

2. 在項目中引用該文件

3. 測試



