123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- @Library('jenkins-share-library@master')
- def qyWechatRobot = new org.devops.notification.QYWechatRobot()
- pipeline {
- // agent any
- agent {
- node {
- label 'master' // 指定运行节点的标签或者名称
- // customWorkspace "${workspace}" // 指定运行工作目录
- }
- }
- // tools { node '' }
- // parameters {}
- environment {
- _fullDisplayName = "430平台-管理后台"
- _qyWechatRobotToken = "a47cb467-8e94-4877-b275-832cc1f0d9af"
- _remote = "root@47.97.230.53"
- _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 = "430-platform-web-admin-frontend_${_git_branch}_${_buildTime}.tar.gz" // 产物文件名
- _productPath = '/app/430-platform-/back-end-ui' // 产物启动目录
- _productBackupPath = '/app/backup/430-platform-/back-end-ui' // 产物备份目录
- }
- 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 { // 不论当前完成状态是什么,都执行
- 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 "node -v"
- sh "npm -v"
- }
- }
- }
- }
- stage('app') {
- parallel {
- stage('admin') {
- steps {
- dir("${env.WORKSPACE}") {
- sh 'npm install'
- sh 'npm run build'
- }
- dir("${env.WORKSPACE}/dist") {
- sh "tar -zcvf ${env.WORKSPACE}/430-platform-web-admin-frontend_${_git_branch}_${_buildTime}.tar.gz ./"
- }
- }
- }
- }
- }
- stage('Product') {
- steps {
- archiveArtifacts(
- artifacts: '*.tar.gz', // 字符串类型,需要归档的文件路径,使用的是Ant风格路径表达式。
- // excludes: '', // 字符串类型,需要排除的文件路径,使用的也是Ant风格路径表达式。
- caseSensitive: true, // 布尔类型,对路径大小写是否敏感。
- fingerprint: true, // 布尔类型,是否对归档的文件进行签名。
- onlyIfSuccessful: true // 布尔类型,只在构建成功时进行归档。
- )
- }
- }
- stage('Pre-Release') {
- steps {
- script {
- if ("${_git_branch}" == 'master') {
- echo "当前分支为:[${_git_branch}], 准备发布ing..."
- } else {
- error("当前分支为:[${_git_branch}], 终止当前发布操作...")
- }
- }
- }
- }
- stage('Release') {
- parallel {
- stage('master分支') {
- steps {
- script {
- echo "当前分支为:[${_git_branch}], 准备发布ing..."
- sh """
- ssh ${_remote} "
- source /etc/profile
- mkdir -pv ${_productBackupPath}
- mkdir -pv ${_productPath}
- "
- """
- sh "scp -r ${WORKSPACE}/${_productFileName} ${_remote}:${_productBackupPath}"
- sh """
- ssh ${_remote} "
- source /etc/profile
- rm -rf ${_productPath}/*
- touch ${_productPath}/${_git_branch}_${_buildTime}.log
- tar -zxvf ${_productBackupPath}/${_productFileName} -C ${_productPath}
- "
- """
- }
- }
- }
- }
- }
- }
- }
|