Skip to content

Commit 70d2462

Browse files
authored
Fix allow_failure when combined with rules (#1283)
* test: Check that `allow_failure` does not get overwritten Currently `allow_failure` is overwritten by `rules` regardless of whether or not `rules` has an `allow_failure` setting itself. * fix: Do not overwrite `allow_failure` from `rules` unless set
1 parent c205080 commit 70d2462

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export class Job {
194194

195195
// Set {when, allowFailure} based on rules result
196196
if (this.rules) {
197-
const ruleResult = Utils.getRulesResult({cwd, rules: this.rules, variables: this._variables}, this.gitData, this.when);
197+
const ruleResult = Utils.getRulesResult({cwd, rules: this.rules, variables: this._variables}, this.gitData, this.when, this.allowFailure);
198198
this.when = ruleResult.when;
199199
this.allowFailure = ruleResult.allowFailure;
200200
this._variables = {...globalVariables, ...jobVariables, ...ruleResult.variables, ...matrixVariables, ...predefinedVariables, ...envMatchedVariables, ...argvVariables};

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ export class Utils {
180180
return envMatchedVariables;
181181
}
182182

183-
static getRulesResult (opt: RuleResultOpt, gitData: GitData, jobWhen: string = "on_success"): {when: string; allowFailure: boolean; variables?: {[name: string]: string}} {
183+
static getRulesResult (opt: RuleResultOpt, gitData: GitData, jobWhen: string = "on_success", jobAllowFailure: boolean | {exit_codes: number | number[]} = false): {when: string; allowFailure: boolean | {exit_codes: number | number[]}; variables?: {[name: string]: string}} {
184184
let when = "never";
185185

186186
// optional manual jobs allowFailure defaults to true https://docs.gitlab.com/ee/ci/jobs/job_control.html#types-of-manual-jobs
187-
let allowFailure = jobWhen === "manual";
187+
let allowFailure = jobWhen === "manual" ? true : jobAllowFailure;
188188
let ruleVariable: {[name: string]: string} | undefined;
189189

190190
for (const rule of opt.rules) {

tests/test-cases/script-failures/.gitlab-ci.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,18 @@ exit_code[number[]] not allowed:
6262

6363
rules:allow_failure precedence:
6464
image: nginx:alpine
65-
allow_failure: false
65+
allow_failure:
66+
exit_codes: 137
6667
rules:
6768
- if: $CI_DEFAULT_BRANCH == "main"
68-
allow_failure: true
69+
allow_failure: false
6970
script:
70-
- exit 1
71+
- exit 137
72+
73+
rules:without allow_failure:
74+
allow_failure:
75+
exit_codes: 137
76+
rules:
77+
- when: always
78+
script:
79+
- exit 137

tests/test-cases/script-failures/integration.script-failures.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ test("script-failures <rules:allow_failure precedence>", async () => {
141141
}, writeStreams);
142142

143143
expect(writeStreams.stdoutLines.join("\n")).toContain(
144-
chalk`{black.bgYellowBright WARN } {blueBright rules:allow_failure precedence}`,
144+
chalk`{black.bgRed FAIL } {blueBright rules:allow_failure precedence}`,
145+
);
146+
});
147+
148+
test("script-failures <rules:without allow_failure>", async () => {
149+
const writeStreams = new WriteStreamsMock();
150+
await handler({
151+
cwd: "tests/test-cases/script-failures",
152+
job: ["rules:without allow_failure"],
153+
}, writeStreams);
154+
155+
expect(writeStreams.stdoutLines.join("\n")).toContain(
156+
chalk`{black.bgYellowBright WARN } {blueBright rules:without allow_failure}`,
145157
);
146158
});

0 commit comments

Comments
 (0)