Skip to main content

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

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:

- build
- test

stage: build
- echo "Building..."

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

- if: '$CI_COMMIT_REF_NAME == "master"'
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 to learn more about our services and how we can support your pipeline automation needs.