123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- pipeline {
- agent any
- // tools { maven '' }
- // parameters {}
- environment {
- _productFileName = buildProductFileName() // 产物文件名
- _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(2) // 当发生失败时进行重试(包括第1次失败)
- timestamps() // 添加日志打印时间
- timeout(time: 15, unit: 'MINUTES') // 如果pipeline执行时间过长,超出了设置的timeout时间,Jenkins将中止pipeline(SECONDS、MINUTES、HOURS)
- 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)
- }
- 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('Build') {
- steps {
- sh 'mvn clean package -Dautoconfig.skip=true -Dmaven.test.skip=true'
- }
- }
- stage('Static check') {
- parallel {
- stage('pmd') {
- steps {
- sh 'mvn pmd:pmd'
- }
- }
- }
- }
- stage('Product') {
- steps {
- dir("${env.WORKSPACE}/target") {
- sh "mv ${env.WORKSPACE}/target/*.jar ${env.WORKSPACE}/target/${_productFileName}"
- }
- archiveArtifacts(artifacts: '**/target/*.jar', caseSensitive: true, fingerprint: true)
- }
- }
- stage('Release') {
- parallel {
- stage('Test') {
- when {
- branch 'test'
- }
- steps {
- echo 'Test环境'
- }
- }
- stage('Master') {
- when {
- branch 'master'
- }
- steps {
- script {
- // 判断产物备份目录是否存在,否则创建文件夹
- if(!fileExists("${_productBackupPath}")) {
- sh "mkdir ${_productBackupPath}"
- }
- }
- sh "scp -r ${WORKSPACE}/target/${_productFileName} ${_productBackupPath}"
- }
- }
- stage('Tag') {
- when {
- tag "v*.*.*"
- }
- steps {
- script {
- // 判断产物备份目录是否存在,否则创建文件夹
- if(!fileExists("${_productBackupPath}")) {
- sh "mkdir ${_productBackupPath}"
- }
- }
- sh "scp -r ${WORKSPACE}/target/${_productFileName} ${_productBackupPath}"
- }
- }
- }
- }
- }
- }
- // 生成产物的文件名
- 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}.jar"
- }
|