Jenkins之pipeline的使用

Declarative Pipeline

jenkinsfile(declarative pipeline)
pipeline{
    agent any
    stages{
        stage('Build'){
            steps{
            
            }
        }
        stage('Test'){
            steps{
            
            }
        }
        stage('Deploy'){
            steps{
            
            }
        }
    }
}

Scripted Pipeline

腳本化的pipeline是基于groovy的一種DSL語言,可去此處學習:https://www.w3cschool.cn/groovy/index.html

node('node_name'){
    stage('stage_name'){
        //清除工作空間
        cleanWs()
        //拉取代碼
        checkout([$class: 'GitSCM',
              branches: [[name: '*/master']],
              userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
        //進入指定路徑完成后續(xù)的操作
        dir('path'){
        //執(zhí)行單條shell指令
        sh 'xxxxxx'
        //執(zhí)行多條shell指令

        sh ```#!/bin/bash
                echo "hello"
                rm -f xxx
        ```
        }
    }
}

腳本化pipeline是串行執(zhí)行的

Jenkinsfile (Scripted Pipeline)
node {
    stage('Example') {
        if (env.BRANCH_NAME == 'master') {
            echo 'I only execute on the master branch'
        } else {
            echo 'I execute elsewhere'
        }
    }
}

異常捕捉

Jenkinsfile (Scripted Pipeline)
node {
    stage('Example') {
        try {
            sh 'exit 1'
        }
        catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}

二、如何快速編寫pipeline

在創(chuàng)建的pipeline job中,選擇Pipeline Syntax


image.png
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容