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

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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 can I avoid this situation? I only want to publish build artifacts to release repository and not a CI build.

I thought about the following- but none of them is an easy one step solution

  • Writing a pre-commit hook to check if version was checked in without SNAPSHOT by any one other than admin who is allowed to do a release build;
  • Modify the Jenkins job to see if the build is a CI build; then grep for version and error out if the version is a not a SNAPSHOT version. For this I need to modify 100s of jobs.

回答1:

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.

<plugin>   <artifactId>maven-enforcer-plugin</artifactId>   <version>1.4.2</version>   <executions>     <execution>       <id>enforce-snapshot</id>       <goals>         <goal>enforce</goal>       </goals>       <configuration>         <rules>           <requireSnapshotVersion/>         </rules>         <fail>${fail.if.release}</fail>       </configuration>     </execution>   </executions> </plugin> 

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:

<plugin>   <artifactId>maven-enforcer-plugin</artifactId>   <version>1.4.1</version>   <executions>     <execution>       <id>enforce-beanshell</id>       <goals>         <goal>enforce</goal>       </goals>       <configuration>         <rules>           <evaluateBeanshell>             <condition>"${project.version}".endsWith("-SNAPSHOT")</condition>           </evaluateBeanshell>         </rules>         <fail>${fail.if.release}</fail>       </configuration>     </execution>   </executions> </plugin> 

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

<property>   <fail.if.release>true</fail.if.release> </property> 

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:

<profile>   <id>release</id>   <properties>     <fail.if.release>false</fail.if.release>   </properties> </profile> 

and activate that profile on release with

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!