> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/open-source/use-harness-open-source/pipelines-1/conditions.md).

# Conditions

Conditions limit pipeline [step](/open-source/use-harness-open-source/pipelines-1/steps.md) execution at runtime. Harness Open Source sets [variables](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md) that can be used in conditions.

{% hint style="info" %}
Harness Open Source supports multiple pipelines per repository. Creating a pipeline per [trigger](/open-source/use-harness-open-source/pipelines-1/triggers.md) (push, pull request, tag) can reduce the need for conditions.
{% endhint %}

The following operators are supported:

| Type       | Operator                             |
| ---------- | ------------------------------------ |
| Comparison | `==`, `!=`                           |
| Logical    | `not`, `and`, `or`                   |
| Regex      | `matches`                            |
| String     | `contains`, `startsWith`, `endsWith` |

The following functions are supported:

| Type    | Syntax      |
| ------- | ----------- |
| Always  | `always()`  |
| Failure | `failure()` |

This pipeline runs the `test` step only for pull request [events](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md#buildevent) where the [target branch](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md#buildtarget) is `main`.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: test
        type: run
        when: |
          build.event == "pull_request"
          and
          build.target == "main"
        spec:
          container: ruby
          script: |-
            bundle install --jobs=3 --retry=3
            rake
```

This condition runs the step for pull request events where the target branch is not `main`.

```yaml
        when: |
          build.event == "pull_request"
          and
          build.target != "main"
```

This condition runs the step only when a pull request is created based on the [action](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md#buildaction).

```yaml
        when: build.action == "pullreq_created"
```

## Branch <a href="#branch" id="branch"></a>

Limit execution based on the [target branch](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md#buildtarget).

This pipeline runs the `build` step when the target branch is `main`, or starts with `feature/`.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: build
        type: run
        when: |
          build.target == "main"
          or
          build.target startsWith "feature/"
        spec:
          container: golang
          script: |-
            go build
            go test
```

This condition uses a regular expression to achieve the same behavior.

```yaml
        when: build.target matches "main|feature/.*"
```

## Event <a href="#event" id="event"></a>

Limit execution based on the [event](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md#buildevent).

This pipeline runs the `clean cache` step only for manually triggered pipelines.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: clean cache
        type: run
        when: build.event == "manual"
        spec:
          container: node:18
          script: |-
            npm cache clean --force
```

## Reference <a href="#reference" id="reference"></a>

Limit execution based on the git [reference](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/expression_variables.md#buildref).

This pipeline runs the `build` step for branch names that start with `feature-`, or for tags.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: build
        type: run
        when: |
          build.ref startsWith "refs/heads/feature-"
          or
          build.ref startsWith "refs/tags/"
        spec:
          container: golang
          script: |-
            go build
            go test
```

## Status <a href="#status" id="status"></a>

Limit execution based on the pipeline status.

This pipeline runs the `notify` step only when the `test` step fails.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: test
        type: run
        spec:
          container: gradle:jdk10
          script: |-
            gradle assemble
            gradle check

      - name: notify
        type: plugin
        when: failure()
        spec:
          name: slack
          inputs:
            webhook: ${{ secrets.get("slack_webhook") }}
```

This condition will always run the step.

```yaml
        when: always()
```
