头图

image.png

image.png

image.png


import groovy.json.JsonSlurper
import java.lang.Integer
import java.lang.String
import jenkins.model.Jenkins



node {
    stage('获取environment变量') {
        gitUrl = 'http://10.2x.1x.zz:/hera/sxx-front.git'
        projectName = JOB_NAME.split("_")[1].toLowerCase()
        projectEnvironment = JOB_NAME.split("_")[0].toLowerCase()
        ServiceType = JOB_NAME.split("_")[2].toLowerCase()
        //发版所用的环境 //如果环境的变量中含有_字符DEV_service,环境就获取_下划线分隔的0数组DEV
        //jenvironment = JOB_NAME.split("_")[0].toLowerCase().indexOf("-")!=-1 ? JOB_NAME.split("_")[0].toLowerCase().split("-")[0].toLowerCase() : JOB_NAME.split("_")[0].toLowerCase()
        //echo environment_rdb
        //projectname = JOB_NAME.split("_")[0].toLowerCase().indexOf("-")!=-1 ? JOB_NAME.split("-")[1].split("_")[0]+"-"+${projectname} : ${projectname}
        toPortDev = '30001'
        toPortProd = '30001'
        buildCommand = "node -v && npm config set registry http://registry.npm.taobao.org/  && pnpm install && pnpm run dev"

    }
    parameters {
        gitParameter(branch: '',
                branchFilter: 'origin/(.*)',
                defaultValue: 'master',
                description: '选择分支',
                name: 'repoBranch',
                quickFilterEnabled: false,
                selectedValue: 'NONE',
                sortMode: 'NONE',
                tagFilter: '*',
                type: 'PT_BRANCH')
    }


}






node {
    stage('getBranch') {
        script {
            try {
                if ("${branch}" != "") {
                    println "----------webhook式触发-----------"
                    branchName = branch - "refs/heads"
                    env.branchName = sh(returnStdout: true, script: "echo ${branchName}|awk -F '/' '{print \$NF}'").trim()
                    println "webhook触发的分支是: " + "${branchName}"
                }
            } catch (exc) {
            }
            if ("${params.repoBranch}" != "") {
                println "-----------手动方式触发------------"
                env.branchName = "${params.repoBranch}"
                println "手动触发的分支是: " + "${branchName}"
            }
        }
    }
    stage('checkOut') {
        checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'aa2xxxxxxxxxxxxxxxxxxxxxx6e3d79aa2a', url: "${gitUrl}"]]])
        script {
            gitTagTmp = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
            env.gitTag = "${gitTagTmp}-${BUILD_NUMBER}"
        }
        echo "-----------test print data-------------"
        println "${gitUrl}"
        println "${gitTag}"
        println "${projectName}"
        println "${projectEnvironment}"
        println "${ServiceType}"
        println "生产端口:${toPortProd}"
        println "测试端口:${toPortDev}"
        
        println "${buildCommand}"

        echo "-----------test print data-------------"
    }


    
    stage('build-front') {
        stage("${projectEnvironment}-编译代码") {
            script {
                if (ServiceType == "front") {
                    println "${projectEnvironment}环境编译代码 ${branchName}"
                    projectName1 = "${projectEnvironment}pipeline-${projectName}"
                    println "${gitTag}"
                    println "发布${projectEnvironment}环境,删除dist"
                    sh "rm -rf dist.tar.gz"
                    sh "rm -rf dist"
                    sh "${buildCommand}"
                    sh "ls"
                }
            }
        }
    }   
      
    stage('deploy-nodeTo_nginx') {
        stage("${projectEnvironment}环境部署到nginx") {
            script {
                if (ServiceType == "front) {
                    println "部署到${projectEnvironment}环境nginx,分支为 ${branchName}"
                    
                    if (deployEnv == "prod") {
                                                appPathProd = "/home/nfs/insurance/"
                                                prodHost = "1x.1x.1x.11"
                        sh "ssh ${prodHost} 'if [ ! -d ${appPathProd } ]; then mkdir -p ${appPathProd }; fi && ls ${appPathProd }'"
                        sh "scp dist.tar.gz ${prodHost}:${appPathProd }/ "
                        sh "ssh ${prodHost} tar xf ${appPathProd}/dist.tar.gz -C ${appPathProd }/"
                        sh "ssh ${prodHost} rm -rf ${appPathProd}/dist.tar.gz"
                    }else if (deployEnv == "dev"){
                                                appPathDev = "/home/nfs/insurance/"
                                                prodHost = "1x.1x.1x.11"
                        sh "ssh ${dev_usehost} 'if [ ! -d ${appPathDev } ]; then mkdir -p ${appPathDev }; fi && ls ${appPathDev }'"
                        sh "scp dist.tar.gz ${prodHost }:${appPathDev }/ "
                        sh "ssh ${prodHost } tar xf ${appPathDev }/dist.tar.gz -C ${appPathDev }/"
                        sh "ssh ${prodHost } rm -rf ${appPathDev }/dist.tar.gz"
                    }
                }
            }
        }
    }
    
    
}

image.png

image.png


锅包肉
89 声望17 粉丝

这个人很懒,没有什么说的。


引用和评论

0 条评论