# Deployments and rollbacks

How Hangar deploys a service, how to trigger a deploy from your CI with a deploy hook, and how to roll back to an earlier release in seconds.

Every time a service is deployed, Hangar records a **deployment** with its build log, its status and the commit or image it came from. The service's **Deployments** tab lists them, newest first.

## What starts a deploy

- **A push**, when the repository is connected through a [Git provider](https://tryhangar.io/docs/git-providers) and **Deploy on push** is on. You can also choose to deploy only when you publish a tag.
- **The Deploy button**, for a manual release.
- **The deploy hook**, a URL you can call from anywhere.

A build that is still running can be cancelled from the same tab.

## When a service crashes

A service whose process exits is started again after 5 seconds. If it exits 10 times within 10 minutes, Hangar stops restarting it and it stays down until you deploy or restart it: a process that crashes on every start would only fill its logs. The service logs show why it exited.

## Deploy hook

The Deployments tab shows the service's deploy hook URL. Calling it starts a deploy, which is how you release from a CI pipeline after your tests pass, or from a registry when a new image is pushed.

- For a service that runs a [Docker image](https://tryhangar.io/docs/docker-image), a plain `POST` to the URL deploys the image again. If the request comes from a registry webhook that names the image and tag, Hangar checks that they match the service's before deploying.
- For a service built from Git, add the URL as a push webhook on your Git host. Hangar reads the branch from the push event and deploys only when it is the service's branch.

Deploy hooks only work while **Deploy on push** is on. Treat the URL as a secret: anyone who has it can start a deploy.

## Rollbacks

Rollbacks are off by default. Turn them on from the rollback settings in the service's **Deployments** tab; from then on, each successful deployment keeps the image it built in the platform registry.

To roll back, open **Deployments** and choose **Roll back** on an earlier deployment. The service goes back to that image and to the environment variables it was deployed with. Nothing is rebuilt, so it takes only the few seconds needed to pull the image.

Hangar keeps the last 10 deployments of each service, and with them their rollback images. For a service that runs a [Docker image](https://tryhangar.io/docs/docker-image), deploy a tag per version so that rolling back returns to the same build. Databases have [backups and point-in-time recovery](https://tryhangar.io/docs/backups) instead.

## Frequently asked questions

### How do I roll back a bad release on Hangar?

Turn on rollbacks for the service, then open its Deployments tab and choose Roll back on an earlier successful deployment. Hangar runs the image that deployment built, with the variables it had, without building again.

### Can I trigger a deploy from GitHub Actions or another CI?

Yes. Each service has a deploy hook URL. Calling it from your CI, or adding it as a webhook on your Git host or registry, starts a deploy.
