@Library('jenkins-share-library@master') def qyWechatRobot = new org.devops.notification.QYWechatRobot() pipeline { agent { node { label 'master' } } // tools { node '' } // parameters {} environment { _fullDisplayName = "幼享乐-服务端" _qyWechatRobotToken = "a47cb467-8e94-4877-b275-832cc1f0d9af" _remote = "root@47.97.230.53" _remoteB = "root@8.134.14.62" _buildTime = new Date().format('yyyyMMddHHmmss') // 生成当前时间戳 _git_tag = sh(returnStdout: true, script: 'git describe --tags --always').trim() _git_branch = sh(returnStdout: true, script: 'echo ${GIT_BRANCH#*/}').trim() _git_commit_email = sh(returnStdout: true, script: 'git --no-pager show -s --format=\'%ae\'').trim() _git_commit_message = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim() _productFileName = "yxl-backend_${_git_branch}_${_buildTime}" _productPath = '/app/yxl/yxl-backend' // 产物启动目录 _productBackupPath = '/app/backup/yxl/yxl-backend' // 产物备份目录 } triggers { // cron('0 0 * * *') // cron计划任务定期执行构建 // pollSCM('H/1 * * * *') // pollSCM与cron定义类似,但由Jenkins定期检测源码变化 // upstream( // upstream接受逗号分割的工作字符串和阀值。当字符串中的任何作业以最小阀值结束时,流水线被重新触发。 // upstreamProjects: 'job1, job2', // threshold: hudson.model.Result.SUCCESS // ) gitlab( triggerOnPush: true, triggerOnMergeRequest: true, triggerOnNoteRequest: true, branchFilterType: 'All', secretToken: 'asdfghjkl' ) } options { timestamps() // 添加日志打印时间 // checkoutToSubdirectory('sub') // Jenkins默认拉取源码至工作空间的根目录中,此选项可以指定检出到工作空间的子目录中 disableConcurrentBuilds() // 同一个pipeline,Jenkins默认是可以同时执行多次的,此选项为了禁止pipeline同时执行 // skipDefaultCheckout() // 删除隐式checkuot scm语句 retry(1) // 当发生失败时进行重试(包括第1次失败) buildDiscarder( logRotator( daysToKeepStr: '30', // 构建记录将保存的天数 numToKeepStr: '10', // 最多此数目的构建记录将被保存 artifactDaysToKeepStr: '30', // 比此早的发布包将被删除,但构建的日志、操作历史、报告等将被保留 artifactNumToKeepStr: '10' // 最多此数目的构建将保留他们的发布包 ) ) timeout( // 流水线超时设置 time: 15, // 整型 unit: 'MINUTES', // 时间单位,默认分钟。支持NANOSECONDS,MICROSECONDS,MILLISECONDS,SECONDS,MINUTES(默认),HOURS,DAYS activity: true // 布尔类型,true时 只有当日志没活动才算真正的超时 ) gitLabConnection('gitlab') // 连接gitlab服务(需要在Jenkins中设置Jenkins -> Configure System) } post { always { // 不论当前完成状态是什么,都执行 // pom.xml中增加plugin -> maven-pmd-plugin // Jenkins PMD插件[PMD](https://plugins.jenkins.io/pmd) // pmd(canRunOnFailed: true, pattern: '**/target/pmd.xml') cleanWs() // 清理工作空间插件[Workspace Cleanup Plugin](https://plugins.jenkins.io/ws-cleanup) } success { // 成功后执行 updateGitlabCommitStatus name: 'build', state: 'success' script { qyWechatRobot.endBuild('构建成功😀', _qyWechatRobotToken) } } failure { // 失败后执行 updateGitlabCommitStatus name: 'build', state: 'failed' script { qyWechatRobot.endBuild('构建失败🤬', _qyWechatRobotToken) } } aborted { // 取消后执行 script { qyWechatRobot.endBuild('构建取消🤯', _qyWechatRobotToken) } } } stages { stage('Start Notification') { steps { script { qyWechatRobot.startBuild(_qyWechatRobotToken) } } } stage('Env & Param') { parallel { stage('Env') { steps { sh 'printenv' echo "系统当前用户 [${env.USER}]" echo "JENKINS_URL [${env.JENKINS_URL}]" echo "WORKSPACE [${env.WORKSPACE}]" } } stage('Job') { steps { echo "-------------------- --------------------" echo "JOB_NAME [${env.JOB_NAME}]" echo "BUILD_NUMBER [${env.BUILD_NUMBER}]" echo "BUILD_URL [${env.BUILD_URL}]" echo "BUILD_NUMBER [${env.BUILD_NUMBER}]" echo "-------------------- --------------------" echo "BRANCH_NAME [${env.BRANCH_NAME}]" echo "GIT_BRANCH [${env.GIT_BRANCH}]" echo "GIT_COMMIT [${env.GIT_COMMIT}]" echo "GIT_COMMIT [${env.commit}]" echo "_git_tag [${_git_tag}]" echo "_git_branch [${_git_branch}]" echo "-------------------- --------------------" echo "产物名称 [${_productFileName}]" sh "java -version" sh "mvn -v" } } } } stage('clone code') { steps { timeout(time: 1, unit: 'MINUTES') { script { println('clone code') } } } } stage('Build nx-framework-parent') { steps { dir("./nx-framework-parent") { sh 'mvn clean install -Dautoconfig.skip=true -Dmaven.test.skip=true' } } } stage('Build nx-service-parent') { steps { dir("./nx-service-parent") { sh 'mvn clean install -Dautoconfig.skip=true -Dmaven.test.skip=true' } } } stage('Build services') { failFast true // 当其中一个进程失败时,强制所以当parallel阶段都被终止 parallel { stage('service') { steps { dir("./yxl") { sh 'mvn clean package -Dautoconfig.skip=true -Dmaven.test.skip=true' } } } } } stage('Static check') { parallel { stage('service') { steps { echo "service" // sh 'mvn pmd:pmd' // sh "./mvnw clean test jacoco:report" // withSonarQubeEnv( installationName: 'sonar_server') { // sh './mvnw sonar:sonar' // } // timeout(time: 1, unit: 'HOURS') { // waitForQualityGate abortPipeline: true // } } } } } stage('Product') { parallel { stage('service') { steps { dir("./yxl/target") { sh "cp ${env.WORKSPACE}/yxl/target/*.jar ${env.WORKSPACE}/${_productFileName}.jar" sh "cp -rv ${env.WORKSPACE}/yxl/target/lib ${env.WORKSPACE}/lib" sh "cp -rv ${env.WORKSPACE}/yxl/target/resources ${env.WORKSPACE}/resources" } } } } } stage('archiveArtifacts') { steps { archiveArtifacts( artifacts: '*.jar', // 字符串类型,需要归档的文件路径,使用的是Ant风格路径表达式。 // excludes: '', // 字符串类型,需要排除的文件路径,使用的也是Ant风格路径表达式。 caseSensitive: true, // 布尔类型,对路径大小写是否敏感。 fingerprint: true, // 布尔类型,是否对归档的文件进行签名。 onlyIfSuccessful: true // 布尔类型,只在构建成功时进行归档。 ) } } stage('Pre-Release') { steps { script { echo "当前分支为:[${_git_branch}], 准备发布ing..." // if ("${_git_branch}" == 'master') { // echo "当前分支为:[${_git_branch}], 准备发布ing..." // } else { // error("当前分支为:[${_git_branch}], 终止当前发布操作...") // } } } } stage('开发环境(47.97.230.53)') { steps { script { echo "当前分支为:[${_git_branch}], 准备发布ing..." sh """ ssh ${_remote} " source /etc/profile mkdir -pv ${_productBackupPath} mkdir -pv ${_productPath} rm -rf ${_productPath}/resources " """ // rm -rf ${_productPath}/lib sh "scp -rv ${env.WORKSPACE}/${_productFileName}.jar ${_remote}:${_productBackupPath}/" // sh "scp -rv ${env.WORKSPACE}/lib ${_remote}:${_productPath}/lib" sh "scp -rv ${env.WORKSPACE}/resources ${_remote}:${_productPath}/resources" sh """ ssh ${_remote} " source /etc/profile rm -rf ${_productPath}/*.jar cp ${_productBackupPath}/${_productFileName}.jar ${_productPath}/${_productFileName}.jar cd ${_productPath} sudo bash ./bin/start.sh ${_productFileName}.jar " """ } } } stage('生产环境(8.134.14.62)') { when { branch 'master' // 仅在多分支Pipeline有效 } steps { script { echo "当前分支为:[${_git_branch}], 准备发布ing..." sh """ ssh ${_remoteB} " source /etc/profile mkdir -pv ${_productBackupPath} mkdir -pv ${_productPath} rm -rf ${_productPath}/resources " """ // rm -rf ${_productPath}/lib sh "scp -rv ${env.WORKSPACE}/${_productFileName}.jar ${_remoteB}:${_productBackupPath}/" // sh "scp -rv ${env.WORKSPACE}/lib ${_remoteB}:${_productPath}/lib" sh "scp -rv ${env.WORKSPACE}/resources ${_remoteB}:${_productPath}/resources" sh """ ssh ${_remoteB} " source /etc/profile rm -rf ${_productPath}/*.jar rm -rf ${_productPath}/*.log cp ${_productBackupPath}/${_productFileName}.jar ${_productPath}/yxl-backend-1.jar cp ${_productBackupPath}/${_productFileName}.jar ${_productPath}/yxl-backend-2.jar cd ${_productPath} touch ${_buildTime}.log sudo bash ./bin-1/start.sh yxl-backend-1.jar sudo bash ./bin-2/start.sh yxl-backend-2.jar " """ } } } } }