pipeline k8s持续部署

2019-07-08  本文已影响0人  定_格

java项目打包到docker仓库,并更新k8s集群应用

Jenkinsfile_nexus

/*
    jenkins 项目名 gprp_history-server_master_pro_CI
    jenkins GUI上的变量配置

        String: NODE, TAG,REPO_URL, BRANCH, EMAIL, credentialsId, HUB_DOCKER, PACKAGE_COMMAND, MODULE

        * JOB_NAME: 项目名
        * NODE: 发布机节点
        * TAG值:
            0,正常打包发布版本
            1,更改uat、fat的版本号
            其他,则回退到该tag版本git checkout tag,并发布
        * REPO_URL: git项目仓库地址
        * BRANCH: 发布的分支
        * EMAIL: 项目负责人
        * credentialsId: 拉取代码的public key,要有写权限
        * HUB_DOCKER: docker的仓库地址
        * PACKAGE_COMMAND: 特殊打包命令,可覆盖jenkinsfile的默认打包命令
        * MODULE: 打包不依赖父pom的独立的子模块时需填写子模块的目录名,否则为空字符串
        * USER_NAME,USER_EMAIL: git的账户及邮箱

*/
node(NODE) {
    try {
        env.GROUP = JOB_NAME.split("_")[0]
        env.APP_NAME = JOB_NAME.split("_${BRANCH}_")[0].split("${env.GROUP}_")[1] //全局变量
        env.filename="${MODULE}.jar"
        //副本数
        env.UAT_REPLICAS = 2
        env.FAT_REPLICAS = 1

        checkOut()
        getBranchEnv()
        getVersion()
        compile()
        updateDeployment()

        stage('通知负责人') {
            emailext body: "构建项目:${BUILD_URL}\r\n构建完成", subject: "构建项目${JOB_NAME}【成功】", to: "${EMAIL}"
        }
    } catch (e) {
        echo "${e.toString()}"
        emailext body: "构建项目:${BUILD_URL}\r\n构建失败,\r\n错误消息:${e.toString()}", subject: "构建项目${JOB_NAME}【失败】", to: "${EMAIL}"
        throw e
    }
}

//分支对应环境
def getBranchEnv(){
    env.ENV = ""
    if (BRANCH == "master"){
        env.ENV = "pro"
    }else if(BRANCH == "develop"){
        env.ENV = "fat"
    }else if(BRANCH == "release"){
        env.ENV = "uat"
    }
}


//拉取代码
def checkOut() {
    stage('检出代码') {
        git branch: "${BRANCH}", credentialsId: "${credentialsId}", url: "${REPO_URL}"
        if (TAG != '0' && TAG != '1') {
            sh "git checkout ${TAG}"
        }
        echo "检测若为master分支时,不可拉取snapshot包或取名包含snapshot的包名"
        if (BRANCH == 'master') {
            sh """
                if [[ `sed -n '/dependencies/,/dependencies/p' pom.xml| grep 'SNAPSHOT'` ]];then
                    exit 1
                fi
            """
        }
    }
}

//获取 版本信息
def getVersion() {
    //get pom
    def pom = readMavenPom file: "pom.xml"
    if (MODULE != '') {
        pom = readMavenPom file: "${MODULE}/pom.xml"

    }
    // get project version
    env.VERSION = pom.getVersion()
    if(env.VERSION == null) {
        env.VERSION = pom.getParent().getVersion()
    }

    // get release version
    env.RELEASE = env.VERSION.split("-")[0]


    echo "pom version $env.VERSION"
    echo "release version: $env.RELEASE"
}




//编译 构建
def compile() {

    stage('编译,构建镜像') {
        if (PACKAGE_COMMAND != '') {
            sh "$PACKAGE_COMMAND"
        } else {
            MVN = "/usr/local/maven/bin/mvn -Dmaven.test.skip=true "
            if (MODULE != '') {
                subM = getSubM(MODULE)
            }else {
                subM = ''
            }
            //master 分之
            if (BRANCH == 'master') {
                sh """
                    $MVN release:clean
                    git config user.name $USER_NAME
                    git config  user.email $USER_EMAIL
                    $MVN release:prepare $subM -P pro -U
                """

            }else if (BRANCH == 'develop') {
                sh "$MVN clean package $subM -P fat -U"

            }else if(BRANCH == 'release') {
                sh "$MVN clean package $subM -P uat -Ddefault.version=${env.RELEASE} -U"

            }
        }

    }

}

def updateDeployment() {
    stage('更新应用') {
        if (MODULE == ''){
            MODULE = env.APP_NAME
        }

        if (BRANCH == 'master') {
            sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl set image deployment  $MODULE-$env.ENV $MODULE=$HUB_DOCKER/gprp/$MODULE:$env.RELEASE\""

        }else  {
            if (BRANCH == 'develop') {
                sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl scale deployment $MODULE-$env.ENV --replicas=0\""
                sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl set image deployment  $MODULE-$env.ENV $MODULE=$HUB_DOCKER/gprp-snapshot/$MODULE:$env.VERSION\""
                sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl scale deployment $MODULE-$env.ENV --replicas=$env.FAT_REPLICAS\""

            }else if(BRANCH == 'release') {
                sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl scale deployment $MODULE-$env.ENV --replicas=0\""
                sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl set image deployment  $MODULE-$env.ENV $MODULE=$HUB_DOCKER/gprp/$MODULE:$env.VERSION\""
                sh "ssh hadoop@k8smaster -i /home/jenkins/.ssh/id_rsa_ansible -C \"/k8s/kubernetes/bin/kubectl scale deployment $MODULE-$env.ENV --replicas=$env.UAT_REPLICAS\""

            }
        }
    }
}



def getSubM(String mod) {
    if (mod != "") {
        return " -pl $mod"
    }
    return ""
}
上一篇下一篇

猜你喜欢

热点阅读