# On environment event

Environment event triggers run a pipeline when an environment is created or deleted. Filter by environment ID pattern, scope, and tags to provision or tear down resources automatically.

Environment event triggers let you run a pipeline when an [environment](/docs/environments/introduction.md) is created or deleted. Buddy starts all pipelines whose trigger matches the environment - for example, provision a database and DNS entry for every new `preview-*` environment, or remove them when the environment is deleted.

There are two environment event types:

- **Environments: Created** - triggered when a new environment is created.
- **Environments: Deleted** - triggered when an environment is deleted.

## Configure trigger

1. Open your pipeline and go to the **Workflow** tab.
2. Click **+** in the **Triggers** section and select **Created** or **Deleted** in the **Environments** group.

   ![Environments group in the trigger dropdown](/docs/pipelines/triggering-pipelines/triggering-pipelines-artifacts-dropdown.png 640x485)

3. Pick the environments that should start the pipeline, or tick **All environments**.
4. Click **Apply**.

The trigger appears in the **Triggers** section with the selected environments and matching filters:

![Artifact and environment triggers in the pipeline workflow](/docs/pipelines/triggering-pipelines/triggering-pipelines-artifacts-environments-triggers.png 640x408)

## Filters

The trigger fires when the environment matches at least one of the entries in the popup.

- **Environments** - existing environments in the project. Tick the ones that should start the pipeline.
- **All environments** - reacts to every environment. In YAML this is stored as `'*'`.
- **Add or manage matching filters** - opens a modal where you define patterns instead of picking specific environments:
  - **ID** - an environment identifier pattern with wildcards, e.g. `preview-*`.
  - **Scope** - where the environment lives: **Any**, **Workspace**, or **Project**.
  - **Tags** - tags the environment must have.

Specific environments, **All environments**, and matching filters can be combined in a single trigger. **Created** and **Deleted** are separate triggers with their own filters.

## YAML

The event types are `ENVIRONMENT_CREATE` and `ENVIRONMENT_DELETE`. The `environments` list accepts `'*'` for all environments, specific environment identifiers, and objects with `environment`, `scope`, and `tags`:

```yaml
- pipeline: "Provision preview"
  refs:
    - "refs/heads/master"
  events:
    - type: "ENVIRONMENT_CREATE"
      environments:
        - environment: "preview-*"
          scope: "PROJECT"
  actions:
    - action: "Create database"
      type: "BUILD"
      docker_image_name: "library/ubuntu"
      docker_image_tag: "22.04"
      commands: |-
        ./scripts/create-preview-db.sh
```

`scope` takes `ANY`, `WORKSPACE`, or `PROJECT`. This pipeline tears down resources for any deleted environment tagged `preview`:

```yaml
- pipeline: "Tear down preview"
  refs:
    - "*"
  events:
    - type: "ENVIRONMENT_DELETE"
      environments:
        - environment: "*"
          tags:
            - "preview"
  actions:
    - action: "Drop database"
      type: "BUILD"
      docker_image_name: "library/ubuntu"
      docker_image_tag: "22.04"
      commands: |-
        ./scripts/drop-preview-db.sh
```

<Hint type="info">
Pair the **Created** trigger with a matching **Deleted** trigger in a second pipeline so that everything provisioned for an environment is removed when the environment goes away.
</Hint>

## See also

- [Environments](/docs/environments/introduction.md)
- [Managing environments](/docs/environments/manage-environments.md)
- [On artifact event](/docs/pipelines/introduction/triggering-pipelines/on-artifact-event.md)
- [On sandbox event](/docs/pipelines/introduction/triggering-pipelines/on-sandbox-event.md)


---
Original source: https://buddy.works/docs/pipelines/introduction/triggering-pipelines/on-environment-event