Gitlab Rules if + allow_failure syntax error

So I want to allow a job to fail only if a variable has a certain value. I’m setting using a script.
I’m using the following:

jobName:
  script:   
    - export VAR_NAME=... [script here]
  rules:
    - if: 'VAR_NAME=="value"'
      allow_failure: true

The pipeline does not start, due to the following error:
jobs:jobName:rules:rule config contains unknown keys: allow_failure

When I check my .gitlab-ci.yml in CI Lint it says that the syntax is correct.

I’m running on self-managed gitlab server(GitLab Community Edition 12.10.3, gitlab-runner 12.10.2)

I checked the https://docs.gitlab.com/ee/ci/yaml/#rulesallow_failure docs without any success.

Any suggestions would be greatly appreciated.

This looks like indentation errors (very important when working with yaml files)

rules and script are separate job keywords. They should not be nested within one another and none should have a - before them. allow_failure is also a keyword and therefore should not use an = but a :.

Should be

jobName:
  script:
    - [script code]
  rules:
    - if: 'VAR_NAME=="value"'
      allow_failure: true

I also believe that the rules section is evaluated during pipeline/job startup.

This means export ing the variable you are acting on within the script will not cause the rule to be executed. That variable would have to be passed in via CI/CD variables, or within the variables: section of that job (or defaults).

I recommend looking into a dynamic pipeline, parent-child pipelines, or manual (API) triggering of a pipeline where you can pass the variable with its value into the job.

1 Like

I entered the yml incorrectly. I edited the original post now with the correct version. Either way, I would have expected something else than a syntax error for the allow_failure part. I could use any predefined variable there and still have the error pop up.
Thanks for the reply.

I may have edited my post after you saw it originally. The syntax error is due to the equals sign after allow_failure. That is a keyword and should have a colon instead of an equal sign.

Yeah, sorry. Edited that as well, but still no change. The same error. Thanks

If what you edited in the original post is completely accurate, I’m wondering if the parsing during execution has an issue with

’VAR_NAME==“value”’

There should be a $ before VAR_NAME. It’s possible the runner is having issues evaluating that line but says the syntax error is the following line.

I tried adding a when: always instead of the allow_failure part and it worked. Also tried having them both (if, when and then allow_failure) and had the same error related to the allow_failure part.

1 Like

Same problem here.

myjob:
  script: ...
  rules:
    - if: "$CI_COMMIT_REF_NAME =~ /^release.*$/"
      when: manual
      allow_failure: true
    - when: never

fails with

jobs:myjob:rules:rule config contains unknown keys: allow_failure

Even though the documentation mentions very similar formula…

job:
  script: "echo Hello, Rules!"
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: manual
      allow_failure: true
    - if: '$CI_PIPELINE_SOURCE == "schedule"'

Works fine when allow_failure is applied to the whole job:

myjob:
  script: ...
  allow_failure: true
  rules:
    - if: "$CI_COMMIT_REF_NAME =~ /^release.*$/"
      when: manual
    - when: never