Jenkins管道中的Maven生命周期-如何最好地分离职责? [英] Maven lifecycle within jenkins pipeline - how to best separate responsibilities?

查看:189
本文介绍了Jenkins管道中的Maven生命周期-如何最好地分离职责?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用jenkins 2(声明性)管道和maven时,我总是在如何组织管道中的内容以使其可重用和灵活方面遇到问题.

When working with jenkins 2 (declarative) pipelines and maven I always have a problem with how to organize things within the pipeline to make it resusable and flexible.

一方面,我想将管道分成逻辑阶段,例如:

On the one side I would like to seperate the pipepline into logical stages like:

pipeline
 {
  stages
   {
    stage('Clean') {}
    stage('Build') {}
    stage('Test') {}
    stage('Sanity check') {}
    stage('Documentation') {}
    stage('Deploy - Test') {}
    stage('Selenium tests') {}
    stage('Deploy - Production') {}
    stage('Deliver') {}
   }
 }

另一方面,我有运行的行家

On the other hand I have maven which runs with

mvn clean deploy site

我可以将Maven拆分为

Simply I could split up maven to

mvn clean
mvn deploy
mvn site

但是'部署'包含了所有生命周期阶段,

But the 'deploy' includes all lifecycle phases from

  • 验证
  • 编译
  • 测试
  • 包裹
  • 验证
  • 安装
  • 部署

所以我看到了很多类似这样的pipline示例

So I saw a lot of pipline examples which do things like

sh 'mvn clean compile'

sh 'mvn test'

这将导致第二次重复验证和编译步骤,并以此方式浪费时间/资源". 这可以通过执行

which results in repeating the validate and compile step a second time and waste "time/resources" in this way. This could be resolved with doing a

sh 'mvn surefire:test'

而不是再次运行整个生命周期.

instead of running the whole lifecycle again.

所以我的问题是-在詹金斯油耗阶段和Maven生命周期之间取得最佳平衡的最佳方法是什么? 对我来说,我看到两种方式:

So my question is - which is the best way to get a good balance between the jenkins pipline stages and the maven lifecycle? For me I see two ways:

  1. 将Maven生命周期划分为尽可能多的流水线阶段-这将产生更好的jenkins用户反馈(查看哪个阶段失败等)
  2. 让maven做所有事情,仅使用jenkins管道处理maven的结果(即分析单元测试结果等)

还是在CI/CD练习中误解了某些内容?

Or did I missunderstand something in the CI/CD practice?

推荐答案

两个月后,我认为我有一个平衡良好的Jenkins管道脚本,该脚本虽然不完整,但在Windows和Linux上都能稳定运行.它避免了我看到的其他示例的陷阱.

Two month later I think I have a well balanced Jenkins pipeline script that is not complete, but works stable on windows and linux. It avoids pitfalls of other examples I have seen.

Jenkinsfile

pipeline
 {
  agent any

  tools
   {
    maven 'Maven3'
    jdk 'JDK8'
   }

  options
   {
    buildDiscarder(logRotator(numToKeepStr: '4'))
    skipStagesAfterUnstable()
    disableConcurrentBuilds()
   }


  triggers
   {
    // MINUTE HOUR DOM MONTH DOW
    pollSCM('H 6-18/4 * * 1-5')
   }


  stages
   {
    stage('Clean')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode clean'
           }
          else
           {
            bat 'mvn --batch-mode clean'
           }
         }
       }
     }

    stage('Build')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode compile'
           }
          else
           {
            bat 'mvn --batch-mode compile'
           }
         }
       }
     }

    stage('UnitTests')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode resources:testResources compiler:testCompile surefire:test'
           }
          else
           {
            bat 'mvn --batch-mode resources:testResources compiler:testCompile surefire:test'
           }
         }
       }
      post
       {
        always
         {
          junit testResults: 'target/surefire-reports/*.xml'
         }
       }
     }

    stage('Sanity check')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode checkstyle:checkstyle pmd:pmd pmd:cpd com.github.spotbugs:spotbugs-maven-plugin:spotbugs'
           }
          else
           {
            bat 'mvn --batch-mode checkstyle:checkstyle pmd:pmd pmd:cpd com.github.spotbugs:spotbugs-maven-plugin:spotbugs'
           }
         }
       }
     }

    stage('Packaging')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode jar:jar'
           }
          else
           {
            bat 'mvn --batch-mode jar:jar'
           }
         }
       }
     }

    stage('install local')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode jar:jar source:jar install:install'
           }
          else
           {
            bat 'mvn --batch-mode jar:jar source:jar install:install' // maven-jar-plugin falseCreation default is false, so no doubled jar construction here, but required for maven-install-plugin internal data
           }
         }
       }
     }

    stage('Documentation')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode site'
           }
          else
           {
            bat 'mvn --batch-mode site'
           }
         }
       }
      post
       {
        always
         {
          publishHTML(target: [reportName: 'Site', reportDir: 'target/site', reportFiles: 'index.html', keepAll: false])
         }
       }
     }

    stage('Deploy test')
     {
      steps
       {      
        script
         {
          if (isUnix()) 
           {
            // todo
           }
          else
           {
            bat returnStatus: true, script: 'sc stop Tomcat8'
            sleep(time:30, unit:"SECONDS")
            bat returnStatus: true, script: 'C:\\scripts\\clean.bat'
            bat returnStatus: true, script: 'robocopy "target" "C:\\Program Files\\Apache Software Foundation\\Tomcat 9.0\\webapps" Test.war'
            bat 'sc start Tomcat8'
            sleep(time:30, unit:"SECONDS")
           }
         }
       }
     }

    stage('Integration tests')
     {
      steps
       {
        script
         {
          if (isUnix()) 
           {
            sh 'mvn --batch-mode failsafe:integration-test failsafe:verify'
           }
          else
           {
            bat 'mvn --batch-mode failsafe:integration-test failsafe:verify'
           }
         }
       }
     }

   }

 }

希望这对外面的其他开发人员很有趣.

Hopefully this is interesting for other developers outside there.

随着时间的推移我将对其进行重大改进时,我将在此处进行更新.

I will update this here when I significantly improve it over time.

对于那些也希望看到Maven pom和Jenkinsfile的人,请在github上查看我的小示例项目:

For those who also wish to see a maven pom along with a Jenkinsfile please have a look at my small example project at github: TemplateEngine

这篇关于Jenkins管道中的Maven生命周期-如何最好地分离职责?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆