如何阻止Jenkins中的CI构建意外发布到发布存储库? [英] How to stop CI builds in Jenkins from accidentally publishing to release repository?

查看:114
本文介绍了如何阻止Jenkins中的CI构建意外发布到发布存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,开发人员不小心在POM中签入了没有"SNAPSHOT"的版本.这将构建Maven项目并发布工件以发布存储库.如何避免这种情况?我只想发布构建工件以发布存储库,而不是CI构建.

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

  • 编写一个预先提交的钩子,以检查是否允许除发行版之外的管理员以外的任何其他人在没有SNAPSHOT的情况下签入版本;
  • 修改Jenkins作业,以查看该构建是否为CI构建;然后grep获取版本,如果该版本不是SNAPSHOT版本,则会出错.为此,我需要修改100个工作.

推荐答案

一个好的解决方案是利用 Maven Enforcer插件.

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

从1.4.2版开始(尚未发布,请参见增强功能请求 MENFORCER-204 ),其中有一个新的requireSnapshotVersion规则,该规则强制所构建的项目具有快照版本.

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>

编写自定义规则

在1.4.1版之前,如果当前项目是SNAPSHOT版本,则没有内置规则会失败,但是我们仍然可以使用

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.

为此,您可以在POM中添加以下内容:

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>

这是执行一个评估项目版本的 BeanShell 脚本.如果以-SNAPSHOT结尾,则规则通过,否则,规则失败,构建结束.确定版本是否为快照. (快照版本的严格规则是更复杂,但这应该涵盖所有用例).因此,这样的规则将验证正在构建的项目具有SNAPSHOT版本.

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.

以上两种配置都将Maven属性声明为

Both configurations above declares a Maven property as

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

在SNAPSHOT版本上运行mvn deploy时,它们会使您的构建失败,并确保没有将SNAPSHOT意外部署到发行版本库中.

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.

然后,执行发布时需要禁用规则.为此,我们可以定义一个release配置文件以禁用所定义的规则:

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"

这篇关于如何阻止Jenkins中的CI构建意外发布到发布存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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