Gitlab Pipeline – allow_failure – What is allow_failure in GitLab CI/CD?

DevOps

MOTOSHARE 🚗🏍️
Turning Idle Vehicles into Shared Rides & Earnings

From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.

With Motoshare, every parked vehicle finds a purpose. Owners earn. Renters ride.
🚀 Everyone wins.

Start Your Journey with Motoshare

Okay, here’s a basic GitLab CI/CD YAML (.gitlab-ci.yml) example that demonstrates how the allow_failure configuration option is used, along with an explanation.

The allow_failure keyword in a GitLab CI/CD job definition specifies whether a pipeline should continue running and potentially be marked as successful even if that particular job fails. By default, if any job fails, the entire pipeline is marked as failed, and subsequent stages are typically not executed.

This is useful for:

  • Non-critical jobs like code quality checks, linters, or experimental tests whose failure shouldn’t block the main deployment flow.
  • Jobs that deploy to non-critical environments.
  • Informational scripts.

Example .gitlab-ci.yml with allow_failure:

YAML

# .gitlab-ci.yml

stages:
  - build
  - test
  - deploy

default:
  image: alpine:latest

# Job 1: A standard build job that should succeed
build_application:
  stage: build
  script:
    - echo "--- Building Application ---"
    - sleep 5
    - echo "Application build successful."
    - mkdir -p app_binaries
    - echo "my_app_v1" > app_binaries/app

# Job 2: A critical test job. If this fails, the pipeline should reflect a problem.
critical_unit_tests:
  stage: test
  script:
    - echo "--- Running Critical Unit Tests ---"
    - sleep 8
    - echo "Critical unit tests PASSED."
    # To see a pipeline failure, uncomment the next line:
    # - echo "Simulating critical test failure!"; exit 1

# Job 3: A non-critical or "flaky" test job that is allowed to fail
# without stopping the pipeline or marking the pipeline as failed overall (if other critical jobs pass).
non_critical_lint_check:
  stage: test
  allow_failure: true # This is the key configuration
  script:
    - echo "--- Running Non-Critical Lint Check ---"
    - echo "Checking code style..."
    - sleep 3
    - echo "Lint check found some non-critical style issues and will 'fail'."
    - exit 1 # Simulate a failure for this non-critical job

# Job 4: Another non-critical job (e.g., an optional test suite)
# This also uses allow_failure and will demonstrate that the pipeline continues.
experimental_feature_test:
  stage: test
  allow_failure: true
  script:
    - echo "--- Testing Experimental Feature (allowed to fail) ---"
    - sleep 4
    - echo "Experimental feature test completed (simulating success for this one, but could fail)."
    - exit 0

# Job 5: Deployment job.
# This job will run if 'build_application' and 'critical_unit_tests' succeed,
# even if 'non_critical_lint_check' failed, because it was allowed to fail.
deploy_to_staging:
  stage: deploy
  script:
    - echo "--- Deploying to Staging ---"
    - echo "Checking if critical tests passed before deploying..."
    - if [ -f "app_binaries/app" ]; then echo "App binary found."; else echo "App binary NOT found! Build might have issues."; exit 1; fi
    - echo "Deploying application to staging environment..."
    - sleep 10
    - echo "Deployment to staging successful."
  # This job depends on the success of prior stages' critical jobs.
  # If 'critical_unit_tests' failed (and didn't have allow_failure: true),
  # this 'deploy_to_staging' job would not run.

Code language: PHP (php)

Explanation:

  1. allow_failure: Keyword:
    • Used within a job’s definition.
    • It accepts a boolean value: true or false.
    • allow_failure: true: If this job fails, the job itself will be marked as “failed” (often with a warning icon like orange/yellow in the UI), but it will not cause the entire pipeline to be marked as “failed”. Subsequent jobs in later stages (or parallel jobs that don’t depend on it) can still proceed as if this job had succeeded for the purpose of pipeline progression.
    • allow_failure: false (or omitting the keyword): This is the default behavior. If the job fails, the pipeline is marked as “failed”, and typically, jobs in subsequent stages will not run.
  2. build_application Job:
    • A standard job that is expected to succeed. It does not use allow_failure, so if it failed, the pipeline would fail.
  3. critical_unit_tests Job:
    • This job also does not use allow_failure: true. If this job were to fail (e.g., by uncommenting exit 1), the deploy_to_staging job would not run, and the pipeline would be marked as failed. This represents a critical part of your CI.
  4. non_critical_lint_check Job:
    • allow_failure: true: This is the key demonstration.
    • The script is designed to exit 1, which means this job will fail.
    • However, because allow_failure is true:
      • The non_critical_lint_check job itself will show a failed status (often with a warning icon).
      • The overall pipeline can still proceed to the deploy stage and potentially be marked as “passed” or “passed with warnings” if all other critical jobs (like build_application and critical_unit_tests) succeed.
  5. experimental_feature_test Job:
    • Also uses allow_failure: true. This job is written to succeed in this example, but if it were to fail, it wouldn’t stop the pipeline. This is typical for tests of features that are under development and not yet stable.
  6. deploy_to_staging Job:
    • This job is in the deploy stage, which runs after the test stage.
    • It will execute if build_application and critical_unit_tests (the critical jobs in preceding stages) have passed. The failure of non_critical_lint_check will not prevent deploy_to_staging from running because of allow_failure: true on the lint check job.

Key Concepts and Behavior:

  • Pipeline Status: allow_failure: true primarily affects the overall pipeline status. The job that fails but is allowed to fail will still be marked as failed individually.
  • Visual Indication: In the GitLab UI, jobs that fail but have allow_failure: true are often displayed with a distinct icon (e.g., orange or yellow warning) rather than a red failure icon that would stop the pipeline. The pipeline itself might show as “passed with warnings.”
  • Use Cases:
    • Linters/Style Checks: Useful if you want to be informed of style issues but not block deployments for minor infractions.
    • Non-Critical Tests: For test suites that are known to be flaky or cover less critical aspects of the application.
    • Experimental Features: When testing new, unstable features.
    • Informational Scripts: Scripts that gather data or generate reports where failure isn’t catastrophic.
    • Manual Jobs: Often, manual jobs (like deployments to optional environments) might have allow_failure: true if their failure shouldn’t affect the primary outcome of the CI run.
  • Within rules:: The allow_failure attribute can also be used within a rules: block, allowing you to make the failure allowance conditional. YAMLmy_job_with_rules: script: - "exit 1" rules: - if: '$CI_COMMIT_BRANCH == "main"' allow_failure: false # Failure on main is critical - if: '$CI_COMMIT_BRANCH =~ /^feature\//' allow_failure: true # Allow failures on feature branches

Using allow_failure: true provides flexibility in defining which job failures are considered critical for your pipeline’s success and which are merely warnings or informational. It helps maintain a balance between ensuring quality and not being overly restrictive for non-essential checks.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x