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

Modified on Wed, 21 Jun 2023 at 04:38 PM

The fail section in a pipeline as code allows you to define actions that should be taken when a job fails during its execution. It provides a way to handle failures and take appropriate actions based on the specific failure conditions encountered by a job.


To use the fail section in your pipeline as code, follow these steps:


Define the job you want to handle failures for in your pipeline YAML file. For example:


stages:
  - build

job1:
  stage: build
  script:
    - echo "Running the job..."
  fail:
    message: "This job failed."
    when: on_failure


Specify the fail section within the job definition and configure its properties. In the example above, the fail section is set with a failure message that will be displayed when the job fails.


Optionally, you can specify the when condition under the fail section to control when the failure message should be displayed. In this case, the failure message will be displayed only when the job fails.


Here's an example of a complete pipeline YAML file that includes the fail section:


stages:
  - build

job1:
  stage: build
  script:
    - echo "Running the job..."
  fail:
    message: "This job failed."
  when: always


In this example, the job1 will display the failure message defined in the fail section when it fails during its execution. The failure message helps provide relevant information about the failure and can be used to communicate the reason for the failure or prompt appropriate actions.


By using the fail section, you can handle failures in your pipeline and take appropriate actions based on the specific failure conditions encountered by a job. This allows you to address failures proactively and ensure the smooth execution of your pipeline.

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