1
1
import * as c from "ansi-colors" ;
2
- import { spawn } from "child_process" ;
2
+ import { spawn , execSync } from "child_process" ;
3
3
import * as deepExtend from "deep-extend" ;
4
4
import * as fs from "fs-extra" ;
5
5
import * as glob from "glob" ;
@@ -19,8 +19,6 @@ export class Job {
19
19
public readonly name : string ;
20
20
public readonly needs : string [ ] | null ;
21
21
public readonly stage : string ;
22
- public readonly allowFailure : boolean ;
23
- public readonly when : string ;
24
22
public readonly maxJobNameLength : number ;
25
23
public readonly stageIndex : number ;
26
24
@@ -32,6 +30,10 @@ export class Job {
32
30
private readonly scripts : string [ ] = [ ] ;
33
31
private readonly variables : { [ key : string ] : string } ;
34
32
private readonly predefinedVariables : { [ key : string ] : string } ;
33
+ private readonly rules : any ;
34
+
35
+ public allowFailure : boolean ;
36
+ public when : string ;
35
37
36
38
private prescriptsExitCode = 0 ;
37
39
private afterScriptsExitCode = 0 ;
@@ -86,6 +88,7 @@ export class Job {
86
88
this . allowFailure = jobData . allow_failure || false ;
87
89
this . variables = jobData . variables || { } ;
88
90
this . needs = jobData . needs || null ;
91
+ this . rules = jobData . rules || null ;
89
92
90
93
this . predefinedVariables = {
91
94
CI_COMMIT_SHORT_SHA : "a33bd89c" , // Changes
@@ -119,6 +122,27 @@ export class Job {
119
122
} ;
120
123
}
121
124
125
+ public async initRules ( ) {
126
+ if ( ! this . rules ) {
127
+ return
128
+ }
129
+ for ( const rule of this . rules ) {
130
+ try {
131
+ if ( rule [ 'if' ] ) {
132
+ const output = execSync ( `[ ${ rule [ 'if' ] } ] && exit 0 || exit 1` , { cwd : this . cwd , env : this . getEnvs ( ) , shell : 'bash' } ) ;
133
+ if ( output . length > 0 ) {
134
+ process . stderr . write ( `Rule output ${ output } ` ) ;
135
+ }
136
+ }
137
+ this . when = rule [ 'when' ] ? rule [ 'when' ] : this . when ;
138
+ this . allowFailure = rule [ 'allowFailure' ] ? rule [ 'allowFailure' ] : this . allowFailure ;
139
+ break ;
140
+ } catch ( e ) {
141
+ // By pass rule on exit 1
142
+ }
143
+ }
144
+ }
145
+
122
146
public getPrescriptsExitCode ( ) {
123
147
return this . prescriptsExitCode ;
124
148
}
0 commit comments