Jenkins更改Parameterized Trigger Plugin以支持fail or abort结果

通常在Jenkins中,会使用 Build-timeout plugin给一个Jenkins Job设定一个timeout值,当超过这个值时,使这个build fail或abort。这个plugin 也会和Parameterized Trigger Plugin一起协作,在当前的build fail或abort时,来执行下游的Job,可能是资源清理,系统restore等工作。

在Jenkins core中是对build 的Result定义如下所示:

public static final Result SUCCESS = new Result("SUCCESS",BallColor.BLUE,0);
    /**
     * The build had some errors but they were not fatal.
     * For example, some tests failed.
     */
    public static final Result UNSTABLE = new Result("UNSTABLE",BallColor.YELLOW,1);
    /**
     * The build had a fatal error.
     */
    public static final Result FAILURE = new Result("FAILURE",BallColor.RED,2);
    /**
     * The module was not built.
     * 

* This status code is used in a multi-stage build (like maven2) * where a problem in earlier stage prevented later stages from building. */ public static final Result NOT_BUILT = new Result("NOT_BUILT",BallColor.NOTBUILT,3); /** * The build was manually aborted. * * If you are catching {@link InterruptedException} and interpreting it as {@link #ABORTED}, * you should check {@link Executor#abortResult()} instead (starting 1.417.) */ public static final Result ABORTED = new Result("ABORTED",BallColor.ABORTED,4);

那么在Parameterized Trigger Plugin中,可以看到各个配置对build result的匹配,

SUCCESS("Stable") {
		@Override
		boolean isMet(Result result) {
			return result == Result.SUCCESS;
		}
	},
	UNSTABLE("Unstable") {
		@Override
		boolean isMet(Result result) {
			return result == Result.UNSTABLE;
		}
	},
	UNSTABLE_OR_BETTER("Stable or unstable but not failed") {
		@Override
		boolean isMet(Result result) {
			return result.isBetterOrEqualTo(Result.UNSTABLE);
		}
	},
	UNSTABLE_OR_WORSE("Unstable or Failed but not stable") {
		@Override
		boolean isMet(Result result) {
			return result.isWorseOrEqualTo(Result.UNSTABLE);
		}
	},
	FAILED("Failed") {
		@Override
		boolean isMet(Result result) {
			return (result == Result.FAILURE);
		}
	},
	ALWAYS("Complete (always trigger)") {
		@Override
		boolean isMet(Result result) {
			return true;
		}
	};
可以看到没有支持fail or abort的build result。abort有两种可能,一是job本身超时,由Build-timeout plugin触发,这个我们可以在Build-timeout plugin配置中,勾上“Fail the build” checkbox,使得在这种情况下,让build fail。在这个情况下,两个plugin的协作,完全可以解决这个问题。

另一个abort的可能,是用户手动cancel一个build,在这种情况下,Build-timeout plugin不会起作用。

如果特定的业务逻辑是:当当前build的result是unstable或比unstable差,触发下游job,那么就可以用

UNSTABLE_OR_WORSE("Unstable or Failed but not stable") 触发器。
但如果业务逻辑是:仅当当前build的result是abort 或fail时,触发下游job。那么现有的Parameterized Trigger Plugin就不符合要求了。

解决方法,可以在Parameterized Trigger Plugin中支持这个新的trigger,也可以更加简单的重写

FAILED("Failed") trigger,

如下所示:

FAILED("Failed") {
		@Override
		boolean isMet(Result result) {
			return (result == Result.FAILURE || result == Result.ABORTED);
		}
	},



你可能感兴趣的:(Jenkins)