Skip to content

Commit

Permalink
docs: Filter Condition Doc (#4224)
Browse files Browse the repository at this point in the history
* Filter Condition Doc

* Feedback Incorporated + Better Examples

* Feedback Incorporated
  • Loading branch information
ashokdevtron committed Nov 17, 2023
1 parent 606a16c commit e0d95a1
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions docs/user-guide/global-configurations/filter-condition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Filter Condition

[![](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/elements/EnterpriseTag.svg)](https://devtron.ai/pricing)

## Introduction

The [workflows](../creating-application/workflow/) you create in Devtron for managing the CI-CD of your application can be made flexible, for e.g., not all events (such as image builds) generated during the CI stage require progression to the CD stage. Therefore, instead of creating multiple pipelines that cater to complex requirements, Devtron provides you the option of creating filters to make your workflow efficient.

Using filter conditions, you can control the progression of events. Here are a few general examples:
* Images containing the label "test" should not be eligible for deployment in production environment
* Only images having tag versions greater than v0.7.4 should be eligible for deployment
* Images hosted on Docker Hub should be eligible but not the rest

---

## Steps to Create a Filter

{% hint style="info" %}
**Pre-requisite**: You must have application(s) with CI-CD workflow(s) configured
{% endhint %}

1. From the left sidebar, go to **Global Configurations****Filter Condition**.

2. Add a filter condition.

![Figure 1: Creating Our First Filter](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/add-filter.jpg)

3. In the **Define Filter condition** section, you get the following fields:
* **Filter For**: Choose the pipeline upon which the filter should apply. Currently, you can use filter conditions for CD pipelines only. Support for CI pipelines is underway.

![Figure 2: 'Define Filter Condition' section](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/define-filter-page.jpg)

* **Filter Name**: Give a name to the filter.
* **Description**: (Optional) Add a description to the filter, preferably explaining what it does.
* **Filter Condition**: You can specify either a pass condition, fail condition, or both the conditions:
* **Pass Condition**: Only those events that satisfy the condition will be directed through your pipeline.
* **Fail Condition**: Events that fail the condition will not be directed through your pipeline..
* **Use CEL Expression**: You can use `Common Expression Language` (CEL) to define the conditions. Currently, you can create conditions with the help of following variables:
* **containerImage**: Package that contains all the necessary files and instructions to run an application in a container, e.g., gcr.io/k8s-minikube/kicbase:v0.0.39. It returns a string value in the following format: `<registry>/<repository>:<tag>`
* **containerRepository**: Storage location for container images, e.g., kicbase
* **containerImageTag**: Versioning of image to indicate its release, e.g., v0.0.39
* **imageLabels**: The label(s) you assign to an image in the CD pipeline, e.g., ["PROD","Stage"]. It returns an array of strings.

Click **View filter criteria** to check the supported criteria. You get a copy button and a description of each criterion upon hovering. Moreover, you can go to **CEL expression** to learn more about the rules and supported syntax. Check [Examples](#examples) to know more.

![Figure 3: List of Supported Values](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/filter-criteria.jpg)

4. Click **Next**.

5. In the **Apply to** section, you get the following fields:
* **Application**: Choose one or more applications to which your filter condition must apply.

![Figure 4: Selecting Application(s)](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/app-selection.jpg)

* **Environment**: Choose one or more environments to which your filter condition must apply.

![Figure 5: Selecting Environment(s) from Cluster(s)](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/environment-selection.jpg)

{% hint style="info" %}
Since an application can have more than one environment, the filter conditions apply only to the environment you chose in the **Apply to** section. If you create a filter condition without choosing an application or environment, it will not apply to any of your pipelines.
{% endhint %}

6. Click **Save**. You have successfully created a filter.

![Figure 6: Success Toast](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/filter-created.jpg)

{% hint style="warning" %}
If you create filters using CEL expressions that result in a conflict (i.e., passing and failing of the same image), fail will have higher precedence
{% endhint %}

---

## Examples

Here's a sample pipeline we will be using for our explanation of [pass condition](#pass-condition) and [fail condition](#fail-condition).

![Figure 7: Sample Pipeline](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/sample-workflow.jpg)


### Pass Condition

Consider a scenario where you wish to make an image eligible for deployment only if its tag version is greater than `v0.0.7`

The CEL Expression should be `containerImageTag > "v0.0.7"`

![Figure 8: CEL Expression for Pass Condition](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/pass-condition-1.jpg)

Go to the **Build & Deploy** tab. The filter condition was created specifically for `test` environment, therefore the filter condition would be evaluated only at the relevant CD pipeline, i.e., `test`

![Figure 9: Build & Deploy tab](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/pass-env.jpg)

Click **Select Image** for the `test` CD pipeline. The first tab **Eligible images** shows the list and count of images that have satisfied the pass condition since their tag versions were greater than `v0.0.7`. Hence, they are marked eligible for deployment.

![Figure 10: List of Eligible Images](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/eligible-list-1.jpg)

The second tab **Latest images** shows the latest builds (up to 10 images) irrespective of whether they have satisfied the filter condition(s) or not. The ones that have not satisfied the filter conditions get marked as `Excluded`. In other words, they are not eligible for deployment.

![Figure 11: List of Latest Images](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/latest-list-1.jpg)

Clicking the filter icon at the top-left shows the filter condition(s) applied to the `test` CD pipeline.

![Figure 12a: Filter Icon](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/filter-icon-pass-1.jpg)

![Figure 12b: Conditions Applied](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/conditions-applied-1.jpg)


### Fail Condition

Consider a scenario where you wish to exclude an image from deployment if its tag starts with the word `trial` or ends with the word `testing`

The CEL Expression should be `containerImageTag.startsWith("trial") || containerImageTag.endsWith("testing")`

![Figure 13: CEL Expression for Fail Condition](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/test-filter-1.jpg)

Go to the **Build & Deploy** tab. The filter condition was created specifically for `devtron-demo` environment, therefore the filter condition would be evaluated only at the relevant CD pipeline, i.e., `devtron-demo`

![Figure 14: Build & Deploy tab](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/fail-filter.jpg)

Click **Select Image** for the `devtron-demo` CD pipeline. The first tab **Eligible images** shows the list and count of images that have not met the fail condition. Hence, they are marked eligible for deployment.

![Figure 15: List of Eligible Images](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/eligible-list-3.jpg)

The second tab **Latest images** shows the latest builds (up to 10 images) irrespective of whether they have satisfied the filter condition(s) or not. The ones that have satisfied the filter conditions get marked as `Excluded`. In other words, they are not eligible for deployment.

![Figure 16: List of Latest Images](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/latest-list-2.jpg)

Clicking the filter icon at the top-left shows the filter condition(s) applied to the `devtron-demo` CD pipeline.

![Figure 17a: Filter Menu Icon](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/doner-menu-fail-1.jpg)

![Figure 17b: Conditions Applied](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/global-configurations/filters/conditions-applied-3.jpg)

0 comments on commit e0d95a1

Please sign in to comment.