jenkins一个通用的pipline模版,区分部署回滚,支持参数化构建,多环境,多节点

Posted Sicc1107

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jenkins一个通用的pipline模版,区分部署回滚,支持参数化构建,多环境,多节点相关的知识,希望对你有一定的参考价值。

使用的docker镜像进行构建的,git认证,ssh认证都在镜像中

//开发环境
def DEPLOY_DEV_HOST = [ '10.10.1.2','10.10.1.3','10.10.1.4']
//测试环境
def DEPLOY_TEST_THOST = [ '10.10.1.12','10.10.1.13','10.10.1.14']
//生产环境
def DEPLOY_PRO_THOST = [ '10.10.1.22','10.10.1.23','10.10.1.24']

def project = "agent"
def app_name = "agent"
def git_address = "ssh://git@git.example.com:10022/cloud3d/$project.git"

// 认证
def SSH_CREDENTIAL = "/root/.ssh/"
def SSH_KEY = "/home/jenkins/.ssh/"

pipeline 
    // 拉去go环境镜像
    agent 
        docker  
            label 'jenkins-dev'
            image 'golang:1.17-alpine' 
            args "-v $SSH_KEY:$SSH_CREDENTIAL"
        
    

    parameters 
    //string类型参数
    string(name: 'youvar', defaultValue: 'sichenhao', description: '给我一个你的参数')
    //choice的默认值,为第一个参数选项
    choice (name: 'deploymode',choices: ['deploy', 'rollback'],description: '选择部署方式', )
    //git参数
    gitParameter( name: 'branch',branchFilter: 'origin/(.*)',defaultValue: 'dev', type: 'PT_BRANCH_TAG',description: '选择git分支')
    

    environment  code_check_status = "" 

    stages 
        stage ("git_clone") 
            when 
                environment name:'deploymode', value:'deploy' 
                       
            steps   
             git branch: "$branch",  url: "$git_address"
            
        

        stage ("build") 
            when 
                environment name:'deploymode', value:'deploy' 
                
            steps   
             println("开始构建$branch分支")
            
        

        stage ("docker_build") 
            when 
                environment name:'deploymode', value:'deploy' 
                
            steps   
                println("开始制造镜像")
            //获取git当前head简短
                script
                build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                println(build_tag)
                
            
        

        stage ("deploy") 
            when 
                environment name:'deploymode', value:'deploy' 
            
            steps   
                script 
                    switch("$branch")
                        case 'dev':
                            println("开始部署$branch分支")
                            for (deployip in DEPLOY_DEV_HOST)
                                println("$deployip")
                            
                        break;
                        case 'test':
                            println("开始部署$branch分支")
                            for (deployip in DEPLOY_TEST_THOST)
                                println("$deployip")
                            
                            break;
                        case 'master':
                            println("开始部署$branch分支")
                            for (deployip in DEPLOY_PRO_THOST)
                                println("$deployip")
                            
                        break;
                       
                              
            
        
 
        stage ("rollback") 
            when 
                environment name:'deploymode', value:'rollback' 
            
            steps   
            println("开始回滚")
                script 
                    switch("$branch")
                        case 'dev':
                            println("开始回滚$branch环境")
                            for (rollbackip in DEPLOY_PRO_THOST)
                                println("$rollbackip")
                            
                        break;
                        case 'test':
                            println("开始回滚$branch环境")
                            for (rollbackip in DEPLOY_PRO_THOST)
                                println("$rollbackip")
                            
                            break;
                        case 'master':
                            println("开始回滚$branch环境")
                            for (rollbackip in DEPLOY_PRO_THOST)
                                println("$rollbackip")
                            
                        break;
                       
                
            
        
    

以上是关于jenkins一个通用的pipline模版,区分部署回滚,支持参数化构建,多环境,多节点的主要内容,如果未能解决你的问题,请参考以下文章

jenkins一个通用的pipline模版,区分部署回滚,支持参数化构建,多环境,多节点

jenkins Pipline 庖丁解牛

06 . Jenkins分布式构建和Pipline

jenkins在pipline中运行后台命令

jenkins pipline和jenkinsfile

jenkins pipline 和 jenkinsfile