Skip to content
SRE运维进阶之路SRE运维进阶之路
devops
github icon
    • Jenkins 学习笔记
        • 4.1 开始编写Jenkinsfile
          • 4.2 Jenkins流水线语法
            • 4.3 Groovy 简明教程
              • 4.4 Jenkins共享库应用
                • 4.5 共享库之钉钉消息推送
                  • 4.6 流水线开发工具
                    • 4.7 pipeline 生产配置实例
                      • pipeline配置java项目
                        • pipeline 配置前端项目
                          • 局部变量的定义和传递
                            • exit code, stdout and stderr 返回值和输出
                            • 参考链接
                            • 4.8 在 VS Code 中校验 Jenkinsfile
                            • 8 Jenkins 常见问题

                            4.7 pipeline 生产配置实例

                            author iconClaycalendar icon2023年4月19日category icon
                            • 自动化工具
                            tag icon
                            • Jenkins
                            timer icon大约 3 分钟

                            此页内容
                            • pipeline配置java项目
                            • pipeline 配置前端项目
                              • 局部变量的定义和传递
                              • exit code, stdout and stderr 返回值和输出
                            • 参考链接

                            # 4.7 pipeline 生产配置实例

                            # pipeline配置java项目

                            pipeline {
                                agent { label 'slave' }
                                options {
                                    timestamps()
                                    disableConcurrentBuilds()
                                    buildDiscarder(
                                        logRotator(
                                            numToKeepStr: '20',
                                            daysToKeepStr: '30',
                                        )
                                    )
                                }
                                parameters {
                                    choice(
                                       name: "DEPLOY_FLAG",
                                       choices: ['deploy', 'rollback'],
                                       description: "发布/回滚"
                                    )
                                }
                                /*=======================================常修改变量-start=======================================*/
                                environment {
                                    gitUrl = "git地址"
                                    branchName = "分支名称"
                                    gitlabCredentialsId = "认证凭证"
                                    projectRunDir = "项目运行目录"
                                    jobName = "${env.JOB_NAME}"
                                    serviceName = "服务名称"
                                    serviceType = "jar"
                                    runHosts = "192.168.167.xx,192.168.167.xx"
                                    rollbackVersion = ""
                                }
                                stages {
                                    stage('Deploy'){
                                        when {
                                            expression { return params.DEPLOY_FLAG == 'deploy' }
                                        }
                                        stages {
                                            stage('Pre Env') {
                                                steps {
                                                    echo "======================================项目名称 = ${env.JOB_NAME}"
                                                    echo "======================================项目 URL = ${gitUrl}"
                                                    echo "======================================项目分支 = ${branchName}"
                                                    echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}"
                                                }
                                            }
                                            stage('Git Clone') {
                                                steps {
                                                    git branch: "${branchName}",
                                                    credentialsId: "${gitlabCredentialsId}",
                                                    url: "${gitUrl}"
                                                }
                                            }
                                            stage('Mvn Build') {
                                                steps {
                                                    withMaven(jdk: 'jdk1.8', maven: 'maven') {
                                                        sh "mvn clean package -Dmaven.test.skip=true -U -f ${serviceName}/pom.xml"
                                                    }
                                                }
                                            }
                                            stage('Ansible Deploy') {
                                                steps{
                                                    script {
                                                        sleep 5
                                                        ansiColor('xterm') {
                                                            ansiblePlaybook colorized: true, extras: '-e "directory=${projectRunDir}" -e "job=${jobName}" -e "service=${serviceName}" -e "type=${serviceType}"', installation: 'ansible', inventory: '/etc/ansible/hosts.yml', limit: "${runHosts}", playbook: '/etc/ansible/playbook/deploy-jenkins.yml'                            
                                                        }
                                                    }
                                                }
                                            }
                                        }   
                                    }
                                    stage('Rollback') {
                                        when {
                                            expression { return params.DEPLOY_FLAG == 'rollback' }
                                        }
                                        steps{
                                            script {
                                                rollbackVersion = input(
                                                    message: "请填写要回滚的版本",
                                                    parameters: [
                                                        string(name:'last_number')
                                                    ]
                                                )
                                                sh """
                                                    echo "正在回滚至就近第${rollbackVersion}个版本"
                                                    ansible ${runHosts} -m shell -a "sh ${projectRunDir}/rollback.sh ${rollbackVersion} ${serviceName}"
                                                """
                                            }
                                        }
                                    }
                                }
                                post {
                                    always {
                                        deleteDir()
                                    }
                                    success {
                                        echo 'This task is successful!'
                                    }
                                }
                            }
                            
                            1
                            2
                            3
                            4
                            5
                            6
                            7
                            8
                            9
                            10
                            11
                            12
                            13
                            14
                            15
                            16
                            17
                            18
                            19
                            20
                            21
                            22
                            23
                            24
                            25
                            26
                            27
                            28
                            29
                            30
                            31
                            32
                            33
                            34
                            35
                            36
                            37
                            38
                            39
                            40
                            41
                            42
                            43
                            44
                            45
                            46
                            47
                            48
                            49
                            50
                            51
                            52
                            53
                            54
                            55
                            56
                            57
                            58
                            59
                            60
                            61
                            62
                            63
                            64
                            65
                            66
                            67
                            68
                            69
                            70
                            71
                            72
                            73
                            74
                            75
                            76
                            77
                            78
                            79
                            80
                            81
                            82
                            83
                            84
                            85
                            86
                            87
                            88
                            89
                            90
                            91
                            92
                            93
                            94
                            95
                            96
                            97
                            98
                            99
                            100

                            # pipeline 配置前端项目

                            vue

                            pipeline {
                                agent { label 'master'}
                            
                                options {
                                    timestamps()
                                    disableConcurrentBuilds()
                                    buildDiscarder(
                                        logRotator(
                                            numToKeepStr: '20',
                                            daysToKeepStr: '30',
                                        )
                                    )
                                }
                            
                                parameters {
                                    choice(
                                       name: "DEPLOY_FLAG",
                                       choices: ['deploy', 'rollback'],
                                       description: "发布/回滚"
                                    )
                                }
                            
                                /*=======================================常修改变量-start=======================================*/
                            
                                environment {
                                    gitUrl = "http://gitlab.schengle.com/driving-frontend/h5.git"
                                    branchName = "master"
                                    gitlabCredentialsId = "437c69b5-a874-4da6-8fe1-61e0275fdf0d"
                                    projectBuildDir = "build"
                                    projectBuildPath = "${env.WORKSPACE}/${projectBuildDir}/"
                                    nginxIp = "192.168.16.141"
                                    nginxHtmlRoot = "/tmp/${env.JOB_NAME}"
                                    owner = "font"
                                    group = "font"
                                    backupRootDir = "/opt/backup"
                                    backupJob = "${backupRootDir}/${env.JOB_NAME}"
                                    backupDir = "${backupJob}/${env.BUILD_NUMBER}"
                                    rollbackVersion = ""
                                }
                            
                                /*=======================================常修改变量-end=======================================*/
                            
                                stages {
                                    stage('Deploy') {
                                        when {
                                            expression { return params.DEPLOY_FLAG == 'deploy' }
                                        }
                                        stages {
                                            stage('Pre Env') {
                                                steps {
                                                    echo "======================================项目名称 = ${env.JOB_NAME}"
                                                    echo "======================================项目 URL = ${gitUrl}"
                                                    echo "======================================项目分支 = ${branchName}"
                                                    echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}"
                                                    echo "======================================项目 Build 文件夹路径 = ${projectBuildPath}"
                                                    echo "======================================项目 Nginx 的 ROOT 路径 = ${nginxHtmlRoot}"
                                                }
                                            }
                            
                                            stage('Git Clone') {
                                                steps {
                                                    git branch: "${branchName}",
                                                    credentialsId: "${gitlabCredentialsId}",
                                                    url: "${gitUrl}"
                                                } 
                                            }
                            
                                            stage('NPM Install') {
                                                steps {
                                                    nodejs('nodejs') {
                                                        sh "npm install"
                                                    }
                                                }
                                            }
                            
                                            stage('NPM Build') {
                                                steps {
                                                    nodejs('nodejs') {
                                                        sh "npm run build"
                                                    }
                                                }
                                            }
                            
                                            stage('Backup') {
                                                agent { label 'ansible'}
                                                steps {
                                                    script {
                                                        try {
                                                            isItBackupToday = sh (returnStatus: true, script:'ansible ${nginxIp} -m shell -a "ls -l --time-style=+%D ${backupJob} | grep $(date +%D)"')
                                                            if (isItBackupToday !=0){
                                                                try {
                                                                    sh 'ansible ${nginxIp} -m file -a "path=${backupDir} state=directory owner=${owner} group=${group}"'
                                                                    sh 'ansible ${nginxIp} -m shell -a "cp -a ${nginxHtmlRoot}/* ${backupDir}"'
                                                                }
                                                                catch (exc) {
                                                                    echo 'Something failed!'
                                                                }  
                                                            }
                                                        }
                                                        catch (exc) {
                                                            echo 'Something failed!'
                                                        }                             
                                                    }
                                                }
                                            }
                            
                                            stage('Nginx Deploy') {
                                                agent { label 'ansible'}
                                                steps {
                                                    sh 'ansible ${nginxIp} -m synchronize -a "src=${projectBuildPath} dest=${nginxHtmlRoot} delete=yes"'
                                                    sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} owner=${owner} group=${group} recurse=yes"'
                                                }
                                            }
                            
                                            stage('Tar Build') {
                                                steps {
                                                    sh "tar -zcf  ${env.JOB_NAME}.tar.gz ${projectBuildDir}"
                                                }
                                            }
                            
                                            stage('Archive Artifacts') {
                                                steps {
                                                    archiveArtifacts "${env.JOB_NAME}.tar.gz"
                                                }
                                            }
                                        }
                                    }
                            
                                    stage('Rollback') {
                                        when {
                                            expression { return params.DEPLOY_FLAG == 'rollback' }
                                        }
                                        agent { label 'ansible'}
                                        steps{
                                            script {
                                                sh 'ansible ${nginxIp} -m shell -a "ls -l  ${backupJob}" | grep -v "CHANGED"'
                                                rollbackVersion = input(
                                                    message: "请填写要回滚的版本",
                                                    parameters: [
                                                        string(name:'BUILD_NUMBER')
                                                    ]
                                                )
                                                sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} state=absent"'
                                                sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} state=directory owner=${owner} group=${group}"'                    
                                                withEnv(["rollbackVersion=${rollbackVersion}"]){
                                                    sh 'ansible ${nginxIp} -m shell -a "cp -a  ${backupJob}/${rollbackVersion}/* ${nginxHtmlRoot}"'
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            
                            1
                            2
                            3
                            4
                            5
                            6
                            7
                            8
                            9
                            10
                            11
                            12
                            13
                            14
                            15
                            16
                            17
                            18
                            19
                            20
                            21
                            22
                            23
                            24
                            25
                            26
                            27
                            28
                            29
                            30
                            31
                            32
                            33
                            34
                            35
                            36
                            37
                            38
                            39
                            40
                            41
                            42
                            43
                            44
                            45
                            46
                            47
                            48
                            49
                            50
                            51
                            52
                            53
                            54
                            55
                            56
                            57
                            58
                            59
                            60
                            61
                            62
                            63
                            64
                            65
                            66
                            67
                            68
                            69
                            70
                            71
                            72
                            73
                            74
                            75
                            76
                            77
                            78
                            79
                            80
                            81
                            82
                            83
                            84
                            85
                            86
                            87
                            88
                            89
                            90
                            91
                            92
                            93
                            94
                            95
                            96
                            97
                            98
                            99
                            100
                            101
                            102
                            103
                            104
                            105
                            106
                            107
                            108
                            109
                            110
                            111
                            112
                            113
                            114
                            115
                            116
                            117
                            118
                            119
                            120
                            121
                            122
                            123
                            124
                            125
                            126
                            127
                            128
                            129
                            130
                            131
                            132
                            133
                            134
                            135
                            136
                            137
                            138
                            139
                            140
                            141
                            142
                            143
                            144
                            145
                            146
                            147
                            148
                            149
                            150
                            151
                            152

                            这个jenkinsfile中有保存 制品,

                            需要注意的是:制品的保存时间,和jenkins丢弃旧的构建参数,相同

                            # 局部变量的定义和传递

                            自定义变量(局部)

                            def username = 'Jenkins'
                            echo "Hello Mr.${username}"
                            
                            1
                            2

                            环境变量(局部)

                            withEnv(['MYTOOL_HOME=/usr/local/mytool']){
                                sh '$MYTOOL_HOME/bin/start'
                            }
                            
                            1
                            2
                            3

                            # exit code, stdout and stderr 返回值和输出

                            其做法是,把stdout定向到一个文件,sh 配置 returnStatus: true,它的返回是一个0或非0的整数,然后从文件读取stdout的内容。stderr同理可得。

                            def status = sh(returnStatus: true, script: "git merge --no-edit $branches > merge_output.txt")
                            if (status != 0) {
                              currentBuild.result = 'FAILED'
                              def output = readFile('merge_output.txt').trim()
                              slackSend channel: SLACK_CHANNEL, message: "<${env.JOB_URL}|${env.JOB_NAME}> ran into an error merging the PR branches into the ${TARGET_BRANCH} branch:\n```\n${output}\n```\n<${env.BUILD_URL}/console|See the full output>", color: 'warning', tokenCredentialId: 'slack-token'
                              error 'Merge conflict'
                            }
                            sh 'rm merge_output.txt'
                            
                            1
                            2
                            3
                            4
                            5
                            6
                            7
                            8

                            # 参考链接

                            Jenkins 安装和配置| judasn/Linux-Tutorial | githubopen in new window

                            jenkins pipeline 局部变量定义及传递 | triThirty | csdnopen in new window

                            Jenkins pipeline 中获取 exit code, stdout and stderr 返回值和输出 | pekkle | 博客园open in new window

                            edit icon编辑此页open in new window
                            上次编辑于: 2023/4/19 13:50:36
                            贡献者: clay-wangzhi
                            上一页
                            4.6 流水线开发工具
                            下一页
                            4.8 在 VS Code 中校验 Jenkinsfile
                            备案号:冀ICP备2021007336号
                            Copyright © 2023 Clay