一、pipeline簡介
1、pipeline是什麼
pipeline是Jenkins的一個插件,以Jenkinsfile的形式存在,用來定義流水線的各個階段,實現從代碼到構建、測試、部署的一整套流程。Pipeline的最大優勢是可以通過代碼來定義流水線,使得流水線的定義及修改變得簡單易維護。
2、pipeline的優勢
使用pipeline,可以將原先分散在各個任務中的邏輯都整合到一個pipeline腳本中,清晰的展示整個流水線的過程,方便重用和維護。
二、pipeline基本語法
1、pipeline語法結構
pipeline {
agent any
stages {
stage('Build') {
steps {
// build action
}
}
stage('Test') {
steps {
// test action
}
}
stage('Deploy') {
steps {
// deploy action
}
}
}
}
2、pipeline語法說明
pipeline可以包含多個stage,每個stage又可以包含多個步驟。每個步驟都可以是Jenkins中可執行的任何內容,例如,Shell命令、groovy腳本等。steps中的代碼會按順序運行,一旦前一步執行失敗,後續步驟不再執行。
3、pipeline示例
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://github.com//.git']]])
}
}
stage('Build') {
steps {
sh 'make'
}
}
stage('Test') {
steps {
sh 'make test'
}
}
stage('Deploy') {
steps {
sh 'make deploy'
}
}
}
}
三、流程式控制制語句
1、流程式控制制指令
在pipeline中,可以使用流程式控制制指令來判斷條件是否成立,並根據結果來執行下一步操作。
2、使用示例
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'make'
}
}
stage('Test') {
when { expression { params.RUN_TESTS == 'true' } }
steps {
sh 'make test'
}
}
stage('Deploy') {
when { branch 'master' }
steps {
sh 'make deploy'
}
}
}
}
3、語法結構解析
when用於判斷條件,支持expression、branch、environment、changeset、allOf、anyOf等屬性。expression表示執行一個Groovy表達式,branch表示特定分支時執行,environment表示特定環境變數時執行,changeset表示當代碼發生變化時執行,allOf表示同時成立時執行,anyOf表示任意一項成立時執行。
四、環境變數的使用
1、環境變數的作用
Jenkins中可以使用環境變數進行腳本編寫,簡化操作流程,增強可維護性。
2、語法結構解析
pipeline {
agent any
environment {
NEXUS_USER = credentials('').username
NEXUS_PSW = credentials('').password
}
stages {
stage('Build') {
steps {
// build action
}
}
stage('Test') {
steps {
// test action
}
}
stage('Deploy') {
steps {
sh 'make deploy NEXUS_USER=$NEXUS_USER NEXUS_PSW=$NEXUS_PSW'
}
}
}
}
3、環境變數示例
在使用環境變數的情況下,將ID由「nexus-credentials」改為您自己使用的ID。
五、自定義函數
1、函數的作用
Jenkins pipeline允許用戶定義自己的Groovy函數,可以將可重用的代碼模塊進行封裝,以方便重複利用。
2、函數使用示例
def deploy(server, domain, artifact) {
sshagent(['deployer']) {
sh "scp ${artifact} deploy@${server}:${domain}/${artifact}"
}
}
pipeline {
agent any
stages {
stage('Build') {
steps {
// build action
}
}
stage('Test') {
steps {
// test action
}
}
stage('Deploy') {
steps {
deploy('server01.example.com', '/opt/web', 'app.war')
}
}
}
}
3、函數說明
在上面的例子中,定義了名為deploy的函數,它接受三個參數,即伺服器名稱、域名和要部署的文件名。在調用deploy函數時,直接傳入所需參數即可。
六、參數化構建
1、參數化構建的作用
在pipeline構建時,可以使用參數化構建,多次構建相同的pipeline,只需要在執行時輸入不同的參數即可,這樣有利於大大增強pipeline的靈活性與可重用性。
2、參數化構建使用示例
properties([
parameters([
string(name: 'APP_NAME', defaultValue: 'my-app', description: 'The name of your app')
])
])
pipeline {
agent any
stages {
stage('Build') {
steps {
// build action
}
}
stage('Test') {
steps {
// test action
}
}
stage('Deploy') {
steps {
sh "make deploy APP_NAME=${APP_NAME}"
}
}
}
}
3、參數化構建說明
在上面的例子中,使用了properties來定義輸入參數,該例中定義一個名為APP_NAME的字元串參數,其默認值為my-app,描述為「您的應用程序名稱」。在腳本中,可以通過${APP_NAME}來使用該參數。
七、錯誤處理
1、異常捕獲的作用
在pipeline構建時,可能會遇到異常情況,需要進行捕獲並進行正確的反應,例如,當構建失敗時,需要發布通知以便其他相關人員知曉。Jenkins支持try-catch-finally語法結構,允許您根據異常情況執行不同的操作。
2、異常捕獲使用示例
pipeline {
agent any
stages {
stage ('Build') {
steps {
try {
sh 'make'
}
catch (exc) {
slackSend(message: "Build Failed: Could not build - ${exc}")
}
finally {
archiveArtifacts artifacts: '*.war'
}
}
}
}
}
3、異常捕獲說明
在上面的例子中,當構建產生異常時,將程序控制權轉移到catch塊中,執行slack通知操作,並在finally塊中執行archiveArtifacts操作。
原創文章,作者:MWCU,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/136386.html