如何阻止 Jenkins 中的 CI 构建意外发布到发布存储库?
How to stop CI builds in Jenkins from accidentally publishing to release repository?
有时,开发人员会不小心签入 POM 中没有 "SNAPSHOT" 的版本。这将构建 Maven 项目并将工件发布到发布存储库。我怎样才能避免这种情况?我只想发布构建工件以发布存储库而不是 CI 构建。
我考虑了以下 - 但其中 none 是一个简单的一步解决方案
- 正在编写预提交挂钩以检查版本是否被允许执行发布构建的管理员以外的任何人在没有 SNAPSHOT 的情况下签入;
- 修改 Jenkins 作业以查看构建是否为 CI 构建;然后 grep 获取版本,如果版本不是 SNAPSHOT 版本则出错。为此,我需要修改 100 个作业。
一个很好的解决方案是利用 Maven Enforcer Plugin.
更新到 1.4.2
从 1.4.2 版本开始(尚未发布,请参阅增强请求 MENFORCER-204),有一个新的 requireSnapshotVersion
规则,它强制正在构建的项目具有快照版本。
<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>
编写自定义规则
到1.4.1版本,没有内置规则如果当前项目是SNAPSHOT版本会失败,但是我们仍然可以使用evaluateBeanshell
规则。
这个想法是让构建失败是默认版本不是快照版本。当当前项目处于发布状态时,禁用该规则。
为此,您可以在 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>
它的作用是执行 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 但这应该涵盖所有用例)。因此,这样的规则将验证正在构建的项目是否具有 SNAPSHOT 版本。
以上两种配置都将 Maven 属性 声明为
<property>
<fail.if.release>true</fail.if.release>
</property>
当 mvn deploy
在 SNAPSHOT 版本上 运行 时,它们会使您的构建失败,确保没有 SNAPSHOT 被意外部署到发布存储库。
然后,执行发布时需要禁用该规则。为此,我们可以定义一个 release
配置文件来禁用定义的规则:
<profile>
<id>release</id>
<properties>
<fail.if.release>false</fail.if.release>
</properties>
</profile>
并在发布时激活该配置文件
mvn release:prepare release:perform -Darguments="-Prelease"
有时,开发人员会不小心签入 POM 中没有 "SNAPSHOT" 的版本。这将构建 Maven 项目并将工件发布到发布存储库。我怎样才能避免这种情况?我只想发布构建工件以发布存储库而不是 CI 构建。
我考虑了以下 - 但其中 none 是一个简单的一步解决方案
- 正在编写预提交挂钩以检查版本是否被允许执行发布构建的管理员以外的任何人在没有 SNAPSHOT 的情况下签入;
- 修改 Jenkins 作业以查看构建是否为 CI 构建;然后 grep 获取版本,如果版本不是 SNAPSHOT 版本则出错。为此,我需要修改 100 个作业。
一个很好的解决方案是利用 Maven Enforcer Plugin.
更新到 1.4.2
从 1.4.2 版本开始(尚未发布,请参阅增强请求 MENFORCER-204),有一个新的 requireSnapshotVersion
规则,它强制正在构建的项目具有快照版本。
<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>
编写自定义规则
到1.4.1版本,没有内置规则如果当前项目是SNAPSHOT版本会失败,但是我们仍然可以使用evaluateBeanshell
规则。
这个想法是让构建失败是默认版本不是快照版本。当当前项目处于发布状态时,禁用该规则。
为此,您可以在 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>
它的作用是执行 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 但这应该涵盖所有用例)。因此,这样的规则将验证正在构建的项目是否具有 SNAPSHOT 版本。
以上两种配置都将 Maven 属性 声明为
<property>
<fail.if.release>true</fail.if.release>
</property>
当 mvn deploy
在 SNAPSHOT 版本上 运行 时,它们会使您的构建失败,确保没有 SNAPSHOT 被意外部署到发布存储库。
然后,执行发布时需要禁用该规则。为此,我们可以定义一个 release
配置文件来禁用定义的规则:
<profile>
<id>release</id>
<properties>
<fail.if.release>false</fail.if.release>
</properties>
</profile>
并在发布时激活该配置文件
mvn release:prepare release:perform -Darguments="-Prelease"