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
prod',
            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

原文地址:https://www.cnblogs.com/vincenshen/p/10487599.html