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

查看:27
本文介绍了詹金斯管道中的 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') {}
   }
 }

另一方面,我有运行的 maven

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

但部署"包括来自

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

所以我看到了很多类似的管道示例

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.

所以我的问题是 - 在 jenkins 管道阶段和 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

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

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