def cover = false //相同版本是否覆盖
def rootPath = "./sipsd-flowable-editor/src/packages/vue-bpmn-viewer"
def tgzName = "vue-bpmn-viewer"
def gitUrl = "https://git.dpark.com.cn/git/cbb/flowable_v4.git"
def branch = "master"
def credentialsId = "c078ac92-89dd-4842-8292-5e348e68db08"
def npm = "https://npm.dpark.com.cn/npm"
def robot = "flowable_publish"
def issuesUrl= 'http://issues.dpark.com.cn/git/issues/flowable_v4/-/issues'
def releasesUrl= 'https://npm.dpark.com.cn/npm/-/web/detail/bpmn-viewer-vue'
def docUrl= 'https://doc.dpark.com.cn/iplatform/geekdoc/docs/wow/wow-1df4mgf24hnuh'
def versionLocal
def packageName
def notice = false
pipeline {
    agent any
    stages {
        stage('拉取代码') {
            steps {
                git branch: "$branch", credentialsId: "$credentialsId", url: "$gitUrl"
            }
        }
        stage('项目构建'){
            steps {
                script {
                    versionLocal = sh(script: "node -p 'let path=\"$rootPath\";require(path+\"/package.json\").version'", returnStdout: true).trim()
                    packageName = sh(script: "node -p 'let path=\"$rootPath\";require(path+\"/package.json\").name'", returnStdout: true).trim()
                    sh "cd $rootPath && yarn install && yarn run build"
                }
            }
        }
        stage('发布npm包'){
            steps {
                script {
                    echo "获取包的本地版本和线上版本"
                    def versionOnline =  sh(script: "npm view $packageName version --registry='$npm'", returnStdout: true).trim()
                    if ("$versionOnline" == "$versionLocal" && cover){
                        echo "版本相同则撤销线上版本"
                        sh "npm unpublish $packageName@$versionLocal --force --registry='$npm'"
                        stage ('删除服务器中的包') {
                               def remote = [:]
                               remote.name = '25'
                               remote.host ='192.168.126.25'
                               remote.user = 'root'
                               remote.password ='kjjsyf#0512'
                               remote.allowAnyHosts= true
                               sshCommand remote: remote,command: "rm -rf '/root/.config/verdaccio/storage/${packageName}/${tgzName}-${versionLocal}.tgz'"

                        }
                        echo "发布新版本"
                        sh "cd $rootPath && npm publish --registry='$npm'"
                    }else if ("$versionOnline" != "$versionLocal"){
                        echo "发布新版本"
                        sh "cd $rootPath && npm publish --registry='$npm'"
                    }
                }
          }
        }
    }
    post {
        success {
            script {
                if(notice){
                    dingtalk(robot: "$robot",
                                type: 'ACTION_CARD',
                                at: [],
                                atAll: true,
                                title: "",
                                text: [
                                "<font color=#0089ff >${packageName}组件发版</font>",
                                "",
                                "----",
                                "",
                                "* 任务：#${BUILD_NUMBER}",
                                "* 组件：${packageName}",
                                "* 版本：${versionLocal}",
                                "* 状态：<font color=#00EE00 >发版成功</font>",
                                "* 查看版本：[点击查看](${releasesUrl})",
                                "* 查看issues：[点击查看](${issuesUrl})",
                                "* 在线文档：[点击查看](${docUrl})",
                                "* 持续时间：${currentBuild.durationString}".split("and counting")[0],
                                "* 执行人：${currentBuild.buildCauses.shortDescription}",
                                ],
                                messageUrl: '',
                                picUrl: '',
                                singleTitle: '',
                                btns: [],
                                btnLayout: 'H',
                                hideAvatar: false
                                )
                }
            }
        }
        failure {
            script {
                if(notice){
                    dingtalk(robot: "$robot",
                                type: 'ACTION_CARD',
                                at: [],
                                atAll: false,
                                title: "",
                                text: [
                                "<font color=#0089ff >${packageName}组件发版</font>",
                                "",
                                "----",
                                "",
                                "* 任务：#${BUILD_NUMBER}",
                                "* 组件：${packageName}",
                                "* 版本：${versionLocal}",
                                "* 状态：<font color=#EE0000 >发版失败</font>",
                                "* 持续时间：${currentBuild.durationString}".split("and counting")[0],
                                "* 执行人：${currentBuild.buildCauses.shortDescription}",
                                ],
                                messageUrl: '',
                                picUrl: '',
                                singleTitle: '',
                                btnLayout: 'H',
                                hideAvatar: false
                                )
                }
            }
        }
    }
}
