jenkins持续集成go应用


上文讲到使用supervisor管理我们的终端应用,这次讲一下使用jenkins持续集成

下面分别讲一下pipeline里每一个段落的含义

agent any   使用任意节点构建
parameters  模块使用参数化构建
environment 脚本全局变量
options     脚本定义参数,超时时间等
stage       每个步骤
checkout    拉取代码命令
when        当全局变量environment的value期待值符合的时候执行steps
post       结束后执行,always代表无论项目是否执行成功都要执行模块里的内容。常用的还有changed,failure,success,unstable
其他地方大多数使用了ansible和shell命令,比如ansible ${ip} -m shell -a "supervisorctl restart beepkg" 是使用ansible的shell 调用$ip的supervisorctl 重启beepkg队列
def createVersion() {
    // 定义一个版本号作为当次构建的版本,输出结果 20191210175842_69
    return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
}

pipeline{
    agent any
    parameters {
        gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '选择代码分支', name: 'branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
        choice choices: ['吴增辉'], description: '填写开发人员', name: 'developer'
        choice choices: ['yes','no'], description: '是否备份', name: 'backup'
        text defaultValue: '', description: '请填写更新日志', name: 'record'
    }
    environment { 
        def ip = "172.16.0.62_go2noticeposp"
        def remotedir = "/data/beego/cups-api"
        def _version = createVersion()
        def tmpdir = "/tmp/cups-api_${_version}"
    }
    
    options {
        disableConcurrentBuilds()
        timeout(time: 3, unit: 'MINUTES')
        buildDiscarder(logRotator(numToKeepStr: '10'))
    }

    stages{
        stage('拉取代码'){
            steps{
                deleteDir()
                checkout([$class: 'GitSCM', branches: [[name: '$branch']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9df82ae3-5563-4d32-808b-00e8aa9ae26c', url: 'git@192.168.80.13:wuzh/cups-api.git']]])
            }    
        }
        stage('打包应用'){
            options {
                timeout(time: 4, unit: 'MINUTES') 
            }
            steps{
                sh label: '', script: '/root/go/bin/bee pack'
            }
        }
        stage('备份服务器文件'){
            when {
                environment name: 'backup',
                value: 'yes'
            }
            steps{
                sh label: '', script: '''ansible ${ip} -m file -a "path=$tmpdir state=directory" 
                ansible ${ip} -m shell -a "rsync -qa $remotedir/*  $tmpdir --exclude=\'logs\'"'''
                
            }
        }
        stage('发布文件到服务器'){
            steps{
                sh label: '', script: 'tar -zxf ${WORKSPACE}/go_online-cups-api.tar.gz'
                sh label: '', script: 'ansible ${ip} -m synchronize -a "src=${WORKSPACE}/go_online-cups-api dest=$remotedir delete=no archive=yes compress=yes rsync_opts=--exclude=\'conf\'"'
            }
        }
        stage('重启supervisor队列'){
            steps{
                sh label: '', script: 'ansible ${ip} -m shell -a "supervisorctl restart beepkg"'
                sh label: '', script: 'ansible ${ip} -m shell -a "supervisorctl status beepkg"'
            }
        }
        stage('生成日志'){
            steps{
                sh label: '', script: '''today=`date "+%Y_%m_%d"`
                now=`date "+%Y_%m_%d_%H_%M_%S"`
                echo "时间: $now" >> /root/update_log/$today
                echo "开发者: $developer" >> /root/update_log/$today
                echo "git地址: git@192.168.80.13:wuzh/cups-api.git" >> /root/update_log/$today
                echo "分支名字: $branch" >> /root/update_log/$today
                echo "更新日志: $record" >> /root/update_log/$today
                echo -e \'-----------------------------------\\n\' >> /root/update_log/$today'''
            }
        }
    }
    post('send mail'){
        always{
            emailext(
                subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -${BUILD_STATUS}!',
                body: '${FILE,path="/root/script/email.html"}',
                to: 'guoyabin@96199.com.cn')
        }
    }
}

执行效果如下: