# Environments

> An environment is a place an app runs, in a tree under the app, with its servers, its configuration and how it deploys.

**An environment is a place an app runs: a tree under the app, such as `MyService/Prod/eu`, where each one has its servers, its configuration and how it deploys.**

Its root: the app, named after its package
Its levels: up to six, named as you like
Inherits: its parent's configuration and settings

## The tree {#tree}

```text
MyService
├── Beta
└── Prod
    ├── eu
    └── us
```

The levels mean what you make them mean. Each environment takes its parent's configuration and settings, and can change or unset any of them; only its servers and its sidecars are its own.

## What an environment holds {#cards}

| Card | What it holds |
|---|---|
| **Configuration** | variables the service gets. A **Secret**'s value is never shown again |
| **Instance** | its region, environment and stage: what picks the lines of the app's [configuration files](../reference/configuration.md#runtime) |
| **Deploy settings** | how a deployment checks health and stops the service |
| **Targets** | the hosts or groups of hosts it deploys to, in order |
| **Sidecars** | the [sidecars](sidecars.md) it runs beside it, a copy on each of its hosts |

## Two ways to configure {#configuration}

- **In the environment**: variables, such as a database password as a secret. They change without a release.
- **In the release**: `configuration/MyService.config`, written with the code, with a line for each region, environment and stage, such as `*.Prod.*.http.timeout = 10s`. They change with a release, and are reviewed like code.

## What runs where {#where}

An environment's page lists its deployments, newest first, with the release each one put there. **Deploy** puts a release of the group's main package there by hand, as a new deployment.

## Locking it {#locks}

To have an environment to yourself for a while, say to try a fix on `Beta` before it reaches main, open it and press **Lock…**, saying why. Everyone sees who holds it, since when and why: on its page, in the environments list, and in the pipelines that wait for it. Until someone releases it:

- **Pipelines wait.** A stage that deploys there looks at the lock first and waits, without deploying. A newer candidate takes the place of the one waiting, so once it's released, the newest deploys.
- **Hosts wait.** A host that joins its targets gets nothing until then, and the current release within a minute after. A host that leaves keeps what it runs until then.
- **Only its holder deploys.** **Deploy**, **Restart** and **Roll back to this** are offered to the holder alone, and **Delete** to nobody. A deployment the holder makes meanwhile is marked *under the lock*.

A lock is something whatever deploys checks first, never a wall: what was already queued or running when it was taken goes on, and a script that deploys checks it too, with an [API token](../guides/api-tokens.md):

```sh
$ curl -H "X-Plinto-Key: $PLINTO_TOKEN" https://environments.plinto.build/environments-api/acme/environments/MyService/Beta/-/lock
```

It answers the lock, or `204` when there is none. A deployment someone else makes meanwhile is marked *during ana's lock*.

Anyone can press **Release…**. It can put back what ran before the lock was taken, ahead of anything else; otherwise the pipeline's next candidate replaces what runs. [Try a fix on an environment](../guides/try-a-fix-on-an-environment.md) walks through it.

## Taking it off its hosts {#delete}

What plinto put on a host comes off again when the host no longer runs the environment:

- **A host leaving its targets**, out of a group it targets or a target taken off: within a minute, a deployment takes the environment's service and its sidecars' copies off that host. It never becomes current, and a host joining again gets the current release, as any new host does.
- **Deleting the environment**: it is *being deleted* first, while a deployment takes it off every host it ran on, and nothing else deploys to it; once that's done, it's gone. A host it can't reach keeps it *being deleted*: **Delete again** tries again once the host is back, and **Forget what's on its hosts** deletes it anyway, leaving the host as it is.

Taking off stops the service, with its stop hooks, removes its systemd unit and deletes its folder. A server listed by hand is taken off when its environment is deleted; one taken out of the list keeps what it ran.

- [Deployments](deployments.md): what happens on the servers
- [Sidecars](sidecars.md): programs it runs beside its service
- [Configuration files](../reference/configuration.md): the lines a release carries
- [Pipelines](pipelines.md): stages that deploy to environments
- [Try a fix on an environment](../guides/try-a-fix-on-an-environment.md): lock it, deploy, release
