123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- pipeline {
- agent {
- node {
- label '112.74.105.17'
- }
- }
- // tools { node '' }
- // parameters {}
- environment {
- _productFileName = buildProductFileName() // 产物文件名
- _remote = "root@47.97.230.53"
- _productBackupPath = '/app/csair/backup' // 产物备份目录
- }
- triggers {
- // cron('0 0 * * *') // 周期任务
- // pollSCM('H/1 * * * *') // 轮询代码仓库(每分钟判断一次代码是否有变化)
- gitlab(
- triggerOnPush: true,
- triggerOnMergeRequest: true,
- triggerOnNoteRequest: true,
- branchFilterType: 'All',
- secretToken: 'asdfghjkl'
- )
- }
- options {
- buildDiscarder(logRotator(numToKeepStr: '10')) // 保存最近历史构建记录的数量
- disableConcurrentBuilds() // 同一个pipeline,Jenkins默认是可以同时执行多次的,此选项为了禁止pipeline同时执行
- // checkoutToSubdirectory('sub') // Jenkins默认拉取源码至工作空间的根目录中,此选项可以指定检出到工作空间的子目录中
- retry(1) // 当发生失败时进行重试(包括第1次失败)
- timestamps() // 添加日志打印时间
- timeout(time: 15, unit: 'MINUTES') // 如果pipeline执行时间过长,超出了设置的timeout时间,Jenkins将中止pipeline(SECONDS、MINUTES、HOURS)
- gitLabConnection('gitlab') // 连接gitlab服务(需要在Jenkins中设置Jenkins -> Configure System)
- }
- post {
- always { // 不论当前完成状态是什么,都执行
- cleanWs() // 清理工作空间插件[Workspace Cleanup Plugin](https://plugins.jenkins.io/ws-cleanup)
- }
- failure {
- updateGitlabCommitStatus name: 'build', state: 'failed'
- }
- success {
- updateGitlabCommitStatus name: 'build', state: 'success'
- }
- }
- stages {
- stage('Env & Param') {
- parallel {
- stage('Env') {
- steps {
- sh 'printenv'
- echo "系统当前用户 [${env.USER}]"
- echo "WORKSPACE [${env.WORKSPACE}]"
- echo "JENKINS_URL [${env.JENKINS_URL}]"
- echo "${_productFileName}"
- }
- }
- stage('Job') {
- steps {
- echo "Running [${env.BUILD_NUMBER}] on [${env.BUILD_URL}]"
- echo "BRANCH_NAME [${env.BRANCH_NAME}] GIT_BRANCH [${env.GIT_BRANCH}]"
- }
- }
- }
- }
- stage('NPM Install') {
- steps {
- sh 'npm install'
- }
- }
- stage('Static check') {
- parallel {
- stage('eslint') {
- steps {
- echo 'eslint'
- }
- }
- }
- }
- stage('NPM build') {
- steps {
- sh 'npm run build'
- }
- }
- stage('Product') {
- steps {
- dir("${env.WORKSPACE}/dist") {
- sh "tar -zcvf ${env.WORKSPACE}/${_productFileName} ./"
- }
- archiveArtifacts(artifacts: '*.tar.gz', caseSensitive: true, fingerprint: true)
- }
- }
- stage('Release') {
- parallel {
- stage('Master') {
- when {
- branch 'master'
- }
- steps {
- script {
- sh "scp -r ${WORKSPACE}/${_productFileName} ${_remote}:${_productBackupPath}"
- sh """
- ssh ${_remote} "
- source /etc/profile
- rm -rf /app/csair/admin/*
- tar -zxvf ${_productBackupPath}/${_productFileName} -C /app/csair/admin
- "
- """
- }
- }
- }
- }
- }
- }
- }
- // 生成产物的文件名
- def buildProductFileName() {
- String projectName = "${env.JOB_NAME}".tokenize('//')[0]
- String branchName = "${env.JOB_NAME}".tokenize('//')[1]
- String date = new Date().format('yyyyMMddHHmmss')
- return "${projectName}-${branchName}-${date}.tar.gz"
- }
|