Jenkins FreeStyle or Pipeline Job


FreeStyle Job:

  1. 需要在頁面添加模塊配置項與參數完成配置

  2. 每個Job僅能實現一個開發功能

  3. 無法將配置代碼化,不利於Job配置遷移與版本控制

  4. 邏輯相對簡單,無額外學習成本

 

Pipeline Job:

  1. 所有模塊,參數配置都可以體現為一個Pipeline腳本

  2. 可以定義多個Stage構建為一個Pipeline工作集

  3. 所有配置代碼化,方便Job配置遷移與版本控制

  4. 需要Pipeline腳本語法基礎,學習成本高

 

Pipeline 基本架構

  1. 所有代碼塊包裹在Pipeline{} 層內

  2. stages{} 層用來包含Pipeline中的所有stage子層

  3. stage{} 層用來包含具體需要編寫任務的steps{} 子層

  4. steps{} 層用來添加我們具體需要調用的模塊語句

 

Pipeline示例1

pipeline{
    agent any    // 定義pipeline在哪台jennkins主機上運行,可以使用any, none 或具體的Jenkins node主機名
    environment{    // 定義全局環境變量,也可以定義在具體的Stage區域中
        host='test.example.com'
        user='deploy'
    }
    stages{        // 任務區域
        stage('build'){     // 任務階段
            steps{    // 任務步驟
                sh 'cat $host'
                echo $deploy
            }
        }
    }
}

 Pipeline示例2

#!groovy

pipeline{
    agent {node {label 'master'}}
    
    environment {
        PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
    }
    
    parameters {
        choice(
            choices: 'dev\nprod',
            description: 'choose deploy environment',
            name: 'deploy_env'
        )
        string(name: 'version', defaultValue: '1.0.0', description: 'bulder version')
    }
    
    stages{
        stage("Checkout test repo"){
            steps{
                sh 'git config --global http.sslVerify false'
                dir("${env.WORKSPACE}"){
                    git branch: 'master', credentialsId: "aad1a0c7-29ef-482c-b5dc-86dc0416fb30", url: "https://root@gitlab.pso.com/root/test-repo.git"
                }
            }
        }
        stage("Print env variable"){
            steps{
                dir("${env.WORKSPACE}"){
                    sh """
                    echo "[INFO] Print env variable"
                    echo "Current deployment environment is $deploy_env" >> test.properties
                    echo "The build is $version" >> test.properties
                    echo "[INFO] Done..."
                    """
                }
            }
        }
        stage("Check test properties"){
            steps{
                dir("${env.WORKSPACE}"){
                    sh """
                    echo "[INFO] Check test properties"
                    if [ -s test.properties ]
                    then
                        cat test.properties
                        echo "[INFO] Done..."
                    else
                        echo "test.propreties is empty"
                    fi
                    """
                    echo "[INFO] Build finished..."
                }
            }
        }
    }
}

 

提示:git工具關閉SSL驗證 -> git config --system http.sslVerify false


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM