How to stop CI builds in Jenkins from accidentally publishing to release repository?

后端 未结 1 1475
栀梦
栀梦 2021-01-21 18:35

Sometimes, the developers accidentally check in a version in POM without \"SNAPSHOT\" in it. This builds the Maven project and publishes the artifacts to release repository. How

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-21 19:30

    A good solution around this is to leverage the Maven Enforcer Plugin.

    Update to 1.4.2

    Starting with version 1.4.2 (not released yet, see the enhancement request MENFORCER-204), there is a new requireSnapshotVersion rule, which enforces that the project being built has a snapshot version.

    
      maven-enforcer-plugin
      1.4.2
      
        
          enforce-snapshot
          
            enforce
          
          
            
              
            
            ${fail.if.release}
          
        
      
    
    

    Write a custom rule

    Up to version 1.4.1, there is no built-in rule to fail if the current project is a SNAPSHOT version, but we can still use the evaluateBeanshell rule.

    The idea is to make the build fail is the version is not a snapshot version by default. And when the current project is in a release, disable that rule.

    For that, you can have the following in your POM:

    
      maven-enforcer-plugin
      1.4.1
      
        
          enforce-beanshell
          
            enforce
          
          
            
              
                "${project.version}".endsWith("-SNAPSHOT")
              
            
            ${fail.if.release}
          
        
      
    
    

    What this does is executing a BeanShell script that evaluates the project's version. If it ends with -SNAPSHOT then the rule passes, otherwise, the rule fails and the build ends. Determining whether a version is a snapshot. (The strict rule for snapshots versions are more complicated but this should cover all use cases). Therefore, such a rule will validate that the project being build has a SNAPSHOT version.


    Both configurations above declares a Maven property as

    
      true
    
    

    They will make your build fails when mvn deploy is run on a SNAPSHOT version, making sure no SNAPSHOT are accidently deployed to the release repository.

    Then, the rule need to be disabled when a release is performed. For that, we can define a release profile to disable the defined rule:

    
      release
      
        false
      
    
    

    and activate that profile on release with

    mvn release:prepare release:perform -Darguments="-Prelease"
    

    0 讨论(0)
提交回复
热议问题