How do I use the fail section in a pipeline as code?

Modified on Wed, 21 Jun 2023 at 02:53 PM

To use the fail section in a GitLab pipeline as code, you can define a step that intentionally fails the pipeline under certain conditions. This can be useful for scenarios where you want to explicitly mark a pipeline as failed based on specific criteria.


Here's an example of how you can use the fail section:


stages:
  - build
  - test

build:
  stage: build
  script:
    - echo "Building..."

test:
  stage: test
  script:
    - echo "Running tests..."

  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'
      fail:
        reason: "Pipeline failed due to running tests on the master branch"


In the above example:


The pipeline consists of two stages: build and test.

The build stage simply echoes "Building..." as an example.

The test stage runs tests using the script keyword.

The rules section is used to define a condition for the test stage.

The if condition checks if the current branch is master.

If the condition is met, the fail section is triggered.

The reason field within the fail section provides a description of why the pipeline failed.

By using the fail section, you can explicitly mark a pipeline as failed when certain conditions are met. This can be useful for enforcing specific rules or preventing unintended actions on certain branches or scenarios.


If you have further questions or need assistance with implementing the fail section or optimizing your pipelines, feel free to reach out to Cloud-Runner. We specialize in helping developers maximize the efficiency and performance of their CI/CD workflows. Visit cloud-runner.com to learn more about our services and how we can support your pipeline automation needs.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article