Maven lifecycle within jenkins pipeline - how to best separate responsibilities?

前端 未结 2 1486
孤城傲影
孤城傲影 2021-02-08 06:01

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.

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 06:18

    I think there is no right answer, but the following example worked for us.

    stage('Build and Unit Test') {
        mvn clean deploy -> with unit tests, without integration tests, deploy local
    
        deploy local:
        You can define in a maven profile the distributionManagement like:
        
            
                localFile
                file:target/repository/
            
            
                localFile
                file:target/repository/
            
        
    }   
    
    stage('Pre Integration Tests') {
        The binaries are now in target/repository.
        From there you can use the binaries as you like.
        Copy them to a server, deploy them on an application server, etc.
    }
    
    stage('Integration Tests') {
        maven failsafe:integration-test failsafe:verify
        Already all tests are compiled, just execute them and verify the result.
    }
    
    stage('Deploy to Binary Repository (Nexus, Artifactory, etc)') {
        Now if everything is ok, finally upload the Binaries.
        For that we use wagon-maven-plugin
        So from target/repository the files are uploaded to the Binary Repository.
    }
    

    So to wrap this up:

    • Fail fast. If a unit test has errors -> fail the build.
    • Only build once. Use the same binaries for test, deployment/integration test, upload to repository, etc.
    • With that the stages are logical units, which gives you enough feedback where to look for errors.

提交回复
热议问题