# Link and deploy

Source: https://openpanel.sh/docs/cli/deploy

Tie a folder to a project and service with openpanel link, then deploy, follow and roll back from that folder.

`openpanel link` ties a folder, usually your app's repository, to a project and one of its services. From then on, commands run in that folder or below it need no `--project` or `--service`.

## Link a folder [#link-a-folder]

```sh
cd ~/code/shop
openpanel link --project shop --service web
```

The CLI checks that both exist and writes `.openpanel.json` in the folder:

```json
{
  "projectId": "01a2332b-243d-7006-9138-bd2ba39d4170",
  "serviceId": "01a2332b-7e96-7004-b727-8f80a8d5cb49"
}
```

The file holds only ids, no secrets. Commit it, and your teammates and your [CI](/docs/cli/ci) use the same service without naming it. Each person still signs in as themselves, and each sees the project only with their own access.

`--project` or `--service` on a command still wins over the link, and `openpanel link` again replaces it. Without `--service`, the folder is linked to the project only, and commands that work on a service ask for `--service`.

## Deploy [#deploy]

```sh
openpanel deploy --wait
```

A deploy takes the service as it is set now, and makes a deployment of it:

- **From an image**, it runs the image and tag the service names. The deployment starts at once.
- **From Git**, it builds the newest commit of the service's branch on one of your servers first. `--commit <sha>` builds that commit instead.

Settings and variables you change take effect at the next deploy, never before. A typical change is two commands:

```sh
openpanel services update --image nginx:1.28
openpanel deploy --wait
```

Without `--wait`, the CLI prints the new deployment's id and status, and returns. With it, the CLI prints each new status and returns when the deployment runs, fails, is cancelled or is replaced. For a service built from Git:

```text
Deploying web: 01a2332b-9525-7007-abb7-7d833b311635 (queued)
  building
  deploying
  running
```

| Status | What it means |
|---|---|
| `queued` | Waiting for its server to start the build |
| `building` | The image is being built from Git |
| `deploying` | The new containers are starting. The deployment before keeps serving until they are ready, except for a service with a volume, which stops first |
| `running` | Every copy runs and passed its health check, if it has one |
| `failed` | The build or the rollout failed; the service goes back to the deployment that ran before |
| `superseded` | It ran, and a newer deployment replaced it |
| `cancelled` | It was stopped before it finished |

A failed deployment makes `openpanel deploy --wait` exit with `1` and print why. A build that takes more than 60 minutes fails, and so does a rollout that is not ready after 15 minutes. See [Deployments](/docs/concepts/deployments).

## Follow what happened [#follow-what-happened]

```sh
openpanel deployments
openpanel logs --follow
```

`deployments` lists the service's deployments, newest first, and marks the current one with `*`. `logs` prints what the service's running containers print, read live from your servers, newest last; `--follow` asks again every 2 seconds until you stop it. `--deployment <id>` prints that deployment's build log and deploy log instead (the image being pulled, containers starting, a health check failing), following it until it runs or fails.

## Roll back [#roll-back]

```sh
openpanel rollback
```

Without an id, this goes back to the newest deployment before the current one that has an image and did not fail or get cancelled. It asks you to confirm, then makes a new deployment that runs that image again with the service's settings of today. To go back further, name the deployment:

```sh
openpanel rollback 01a2332b-95e0-7006-a28a-5aecb9679687
```

See [Rollbacks and history](/docs/deploy/rollbacks).

## From nothing to a deployed service [#from-nothing-to-a-deployed-service]

With a [server connected](/docs/servers), in your app's folder:

```sh
openpanel projects create shop
openpanel services create web --project shop --image nginx:1.27 --port http:80 --public --server my-server
openpanel link --project shop --service web
openpanel deploy --wait
openpanel domains
```

A public service with an HTTP port gets an [opnl.app address](/docs/domains/opnl-app) as soon as it is made, such as `web-shop-x7k2.opnl.app`; `openpanel domains` shows it. To build from a repository instead of an image, connect a [Git account](/docs/git) in the console, then use `--repo owner/app --git-account <id>` with the id `openpanel git` shows. See [Add a service](/docs/projects/add-service).