groovy 脚本实例 推送成品包到公网jfrog

groovy 脚本实例 推送成品包到公网jfrog

package common.isv

node('ecs_wuhan_docker_release') {
    properties([
            parameters([
                    listGitBranches(branchFilter: '.*', credentialsId: 'ecs_git', defaultValue: '', name: 'FROM_TAG', quickFilterEnabled: true, remoteURL: 'https://192.168.48.50/ecs-platform/ecs2.git', selectedValue: 'NONE', sortMode: 'DESCENDING_SMART', tagFilter: 'ECS2.[0-9].*', type: 'PT_TAG'),
                    string(defaultValue: '', description: '通知人手机号', name: 'MOBILE_NUM', trim: true),
            ]),
            disableConcurrentBuilds()
    ])

    try {
        stage('check param') {
            if (FROM_TAG == '') {
                throw new Exception('FROM_TAG 选择不能为空!')
            }
        }
        def tagName = FROM_TAG.split("/").last()

        stage("checkout src") {
            deleteDir()
            cloneAllProjectAll(tagName, 'jenkins')
        }
        parallel(
                "maven": {
                    deployMaven()
                },
                "npm": {
                    deployNpm(tagName)
                },
                "jfrog": {
                    uploadJfrog(tagName)
                }
        )
        stage("success") {
            deleteDir()
            wxNotice("tag:${tagName},推送成功", ['24c8480a-2fcb-4eff-9d12-64303454be9d'])
        }
    } catch (e) {
        wxFailNotice('24c8480a-2fcb-4eff-9d12-64303454be9d', MOBILE_NUM)
        throw e
    }
}

def deployNpm(tagName) {
    stage("deploy npm") {
        dir('push_npm') {
            def npmVersion = sh(returnStdout: true, script: "npm view yn-p1@1.0.0-${tagName} version").trim()
            sh "curl -O http://192.168.12.28:4873/yn-p1/-/yn-p1-${npmVersion}.tgz"
            sh "tar zxvf  yn-p1-${npmVersion}.tgz"
            dir('package') {
                try {
                    sh 'npm publish --registry http://106.75.24.148:14003/'
                } catch (ignore) {

                }
            }
        }
    }
}

def deployMaven() {
    stage("deploy maven") {
        String source = 'http://192.168.60.37:8080/repository/maven-releases/'
        String target = 'http://106.75.24.148:14001/repository/maven-releases/'
        sh "find ./ -name pom.xml -exec sed -i 's|>${source}<|>${target}<|g' {} \\;"
        def deployBuild = ['ynbase/mbase', 'ynbase/mbase-micro', 'inf/com.yuanian.infrastructure', 'ecs2/ecs', 'ecs2/upgrade']
        mavenBuildProject(deployBuild, 'package deploy')
    }
}

def uploadJfrog(tagName) {
    stage("deploy jfrog artifact") {
        String sourceUrl = "http://ecs_read:ecs_read@192.168.48.96:8081/artifactory/ecs2_java_prod/tags/${tagName}/"
        String targetUrl = "http://123.15.21.108:91231/artifactory/ecs2_java_prod/tags/${tagName}/"
        String[] listPath = jfrogPath()
        withCredentials([usernameColonPassword(credentialsId: 'public-jfrong-admin', variable: 'JFROG_ADMIN_PASSWD')]) {
            dir('jfrog_upload') {
                for (String p : listPath) {
                    sh "rm -rf ./*"
                    sh "curl -O ${sourceUrl}/${p}"
                    sh "mv * file"
                    if (fileExists("file")) {
                        sh "curl -X PUT -u ${JFROG_ADMIN_PASSWD} -T file ${targetUrl}/${p}"
                    }
                }
            }
        }
    }
}

def jfrogPath() {
    return [
            "project/custom/console-custom.zip",
            "project/custom/ecs-custom.zip",
            "project/custom/ecs-service-console.zip",
            "project/initializr/product-project.zip",
            "project/initializr/product-service-project.zip",
            "project/initializr/product-project-custom.zip",
            "project/initializr/upgrade-custom.zip",
            "project/tools/rename-project-tools.zip",
            "project/readme.md",
            "project/metadata-src.zip",
            "project/mobile/mobile-src.zip",

            "release/console/html/ecs-console.zip",
            "release/console/html/ecs-console-mobile.zip",
            "release/console/html/ecs-mobile.zip",
            "release/console/html/ecs_console.zip",
            "release/console/html/fssc-web.zip",
            "release/console/html/ecs-web.zip",
            "release/console/html/designer.zip",
            "release/console/nginx.conf",

            "release/console/backend/application.yml",
            "release/console/backend/application-dev.yml",
            "release/console/backend/metadata.zip",
            "release/console/backend/console-custom-metadata.zip",
            "release/console/backend/console.jar",
            "release/console/backend/micro/console.jar",
            "release/console/readme.md",

            "release/ecs/html/ecs-console.zip",
            "release/ecs/html/ecs-console-mobile.zip",
            "release/ecs/html/ecs-mobile.zip",
            "release/ecs/html/ecs_console.zip",
            "release/ecs/html/fssc-web.zip",
            "release/ecs/html/ecs-web.zip",
            "release/ecs/html/designer.zip",
            "release/ecs/nginx.conf",

            "release/ecs/backend/ecs.jar",
            "release/ecs/backend/application.yml",
            "release/ecs/backend/application-dev.yml",
            "release/ecs/backend/metadata.zip",
            "release/ecs/readme.md",

            "release/upgrade/upgrade.zip",
            "release/upgrade/readme.md"
    ]
}

def wxNotice(msg, tokenKeys) {
    for (tokenKey in tokenKeys) {
        sh """
        curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${tokenKey}' \
        -H 'Content-Type: application/json' \
        -d '
           {
             "msgtype": "text",
             "text": {
                 "content": "${msg}",
                 "mentioned_mobile_list":["@all"]
             }
           }'
       """
    }
}

def wxFailNotice(wxKey, MOBILE_NUM) {
    def text = "<font color=info>推送</font>任务执行<font color=warning>失败!!!</font>\\n >[查看控制台](${BUILD_URL}console)"
    sh """curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='${wxKey}'' -H 'Content-Type: application/json' -d '{ "msgtype": "markdown", "markdown": { "content": "${text}", } }'"""
    if (MOBILE_NUM != '') {
        sh """curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='${wxKey}'' -H 'Content-Type: application/json' -d '{ "msgtype": "text", "text": { "mentioned_mobile_list":["${MOBILE_NUM}"] } }'"""
    }
}

def cloneAllProjectAll(branch, userName) {
    parallel(
            "java": {
                cloneJavaPlatformProject(branch, userName)
            },
            "web": {
                cloneWebPlatformProject(branch, userName)
            },
    )
}

def cloneJavaPlatformProject(branch, userName) {
    stage('clone Java') {
        cloneProjectBranch(['ynbase', 'inf', 'ecs2', 'ynmicro'], 'ecs-platform', branch, userName)
    }
}

def cloneWebPlatformProject(branch, userName) {
    stage('clone Web') {
        parallel(
                "ecs-web2": {
                    cloneProjectBranch(['ecs-web2', 'ecs_console'], 'ecs-platform', branch, userName)
                },
                "yn_p1_designer": {
                    cloneProjectBranch(['yn_p1_designer', 'yn_p1_frontend'], 'p1', branch, userName)
                },
        )
    }
}

def cloneProjectBranch(projectNames, group, branch, userName) {
    withCredentials([usernamePassword(credentialsId: 'ecs_git', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
        def encodedPass = URLEncoder.encode(PASS, "UTF-8")
        Map parallelMap = [:]
        for (item in projectNames) {
            def projectName = item
            parallelMap.put(projectName, {
                sh "git clone --depth=1 -b ${branch} https://${USER}:${encodedPass}@192.168.48.50/${group}/${projectName}.git ${projectName}"
            })
        }
        parallel(parallelMap)
    }
}


def mavenBuildProject(paths = [], mavenCmd) {
    if (paths == null || paths.size() == 0) {
        return
    }
    sh 'mkdir -p /tmp/.m2/repository'
    withDockerContainer(args: "-v /tmp/.m2/repository:/tmp/.m2/repository:rw,z", image: 'maven:3.6.3-openjdk-8') {
        configFileProvider([configFile(fileId: 'public_maven-setting', variable: 'MAVEN_SETTINGS')]) {
            for (path in paths) {
                if (fileExists("${path}/pom.xml")) {
                    dir(path) {
                        sh "mvn -s ${MAVEN_SETTINGS} ${mavenCmd} -Dmaven.test.skip=true"
                    }
                }
            }
        }
    }
}
posted @ 2022-10-21 15:11  liwenchao1995  阅读(97)  评论(0编辑  收藏  举报