# Deploy
mmo.admin: deploy.json, targets, a release, the health check and the rollback.
- **You will have:** your project's servers and targets declared, checked from your machine, and the few commands
that release it, check it and take it back.
- **You need:** **Tests and verify** done, and the project committed: a release is built from git, never from
your working tree.
## 1. What a deployment is
A **target** is a folder on a server that one project is served from. Deploying is not copying files into it: it
is building one artifact, putting it beside what is running, then moving a single link.
```text
{folder}/
releases/1.0.1/ one release, extracted, never edited in place
releases/1.0.0/ the previous one: where a rollback goes back to
current -> releases/1.0.1 what the web server serves: moving this link IS the deployment
shared/ what every release shares (uploads...), linked into each
deploy/ the journal of the deployments, and the lock
```
Moving a link is atomic: the site serves the old release, then the new one, never half of each. And a rollback is
moving it back, in a second.
The project came with everything this takes, from mmo.admin: `deploy.json`, `scripts/admin.php`, `.gitattributes`
and `db/patches/`.
## 2. What a release contains
A release is a `git archive` of one commit, so `.gitattributes` decides what stays out of it. The sources are not
shipped: the release runs on the `.phar` packages mmo.admin builds from them.
```text from=.gitattributes
# sources: the release ships the built .phar packages, not what they were built from
/src/ export-ignore
/modules/ export-ignore
/scripts/ export-ignore
/tests/ export-ignore
```
`deploy.json`, `configs/dev.json`, `configs/local.json` and the files for AI agents are left out the same way.
This is the "no `src/`" of **What MMOFramework is**: a release runs in prod mode because `src/` is not there.
## 3. Servers and targets: deploy.json
`deploy.json` is committed and reviewed: it decides where releases go. It holds **no secret**, and mmo.admin
refuses to read it if it does. Every key is written in it with a comment saying what it does.
A **server** says how to reach a machine, never with what password:
```json from=deploy.json
"sshAlias": "web1",
```
```json from=deploy.json
"auth": "agent",
```
`sshAlias` names an entry of `deploy.ssh.config`, an ordinary ssh config kept in the project, ignored by git, each
developer's own. `"auth": "agent"` means your ssh agent holds the key: no file holds a secret at all. With a key
file or a password instead, its path or value goes in `deploy.credentials.json`, ignored by git and readable by you
alone.
A **target** says which server, which git branch, which folder, which url:
```json from=deploy.json
// the git branch a release is built from: what is committed on it, never a working tree
"ref": "develop",
// its folder on the server, holding releases/, current and deploy/. Relative: under the server's `root`,
// /var/www/demo. An absolute path works too
"folder": "{name}",
// its public url, checked once a release is switched on: if the site stops answering, the release is rolled
// back. null: no check
"url": "https://demo.example.com",
```
Each target says everything it needs: nothing is inherited from another, so reading one is reading what it is.
`{name}` is the target's own name.
Rather than editing it by hand, `setup` asks for each server and target, explains each question, and shows every
change before writing it:
```bash
php scripts/admin.php setup
```
## 4. Try it on your machine first
A server can be this machine, reached with no ssh: a `local` shell. Add one, with a target, to try the pipeline
before any server:
```json
"servers": {
"here": {"shell": "local", "root": "/home/you/dev/targets"}
},
"targets": {
"local": {"server": "here", "ref": "master", "folder": "{name}", "url": null, "database": null, "shared": [], "reload": null, "healthTimeout": 0, "tags": ["staging"]}
}
```
Then the two commands that read and change nothing:
```bash
php scripts/admin.php targets
php scripts/admin.php doctor --targets=local
```
`targets` lists them; `doctor` checks each one can be reached and has what deploying needs:
```text
targets
○ local reachable, not installed yet (run `install`)
● tar tar
● sha256 sha256sum
● curl curl
```
On the scaffold's `demo` target, `doctor` says what is still missing instead: the ssh alias, the server's host key.
## 5. Release
Commit first: a release is built from the target's branch as it is committed, and refused from a working tree with
uncommitted changes. Then look before you do:
```bash
php scripts/admin.php plan --targets=demo
php scripts/admin.php release --targets=demo
```
`plan` prints every step a release would take and changes nothing. `release` takes them:
1. **install**: creates the target's folders, the first time;
2. **build**: archives the commit, builds the `.phar` packages, and refuses to ship an artifact holding a secret or
a development file;
3. **ship**: uploads it, checks its checksum on the server, extracts it beside the running release;
4. **migrate**: applies the database patches this target has not run, after a snapshot;
5. **activate**: moves the link, runs the target's `reload`, then asks the site whether it answers;
6. **prune**: deletes the oldest releases, never the live one nor the one before it.
If a step fails, every step already taken is undone, the database included, and the command fails: the site stays
on the release it had. Before building, `release` raises the version of what changed since the last one and commits
it (`--no-version` does not).
Nothing is ever implicit: a command that changes a target needs `--targets`, `--tags` or `--all`, or asks.
## 6. Check it, take it back
```bash
php scripts/admin.php status --targets=demo
php scripts/admin.php history --targets=demo
php scripts/admin.php rollback --targets=demo
```
`status` asks the target what is live, and whether someone holds its lock; `history` reads its journal;
`rollback` moves the link back to the previous release.
**Make the health check prove something.** After the switch, the release is checked by asking the target's `url`.
A status 200 alone proves little: a static page answers it while PHP never ran. `healthExpect` names a text the
answer must hold; `{checksum}` is the stamp of the release just activated, which only it can echo:
```json from=deploy.json
"healthExpect": null,
```
Set it on every target that has a route echoing the release's `.release` file.
## 7. Production
A target tagged `prod` is protected: changing it takes typing its name. Released to several protected targets at
once, mmo.admin deploys to one first and stops, so a bad release reaches one site rather than all of them:
```json from=deploy.json
"tags": ["prod"],
```
From a script or a CI job, where nobody can type, the confirmation is `--i-understand`. `--yes` does not count: it
answers another question.
## 8. The database: patches
The tables of **Entities and a database** are created by `install()` the first time; a later change of a table is a
**patch**: a file of `db/patches/`, named after the version that brings it (`1.2.0.js`). `release` applies the
patches a target has not run yet, once each, after a snapshot; `migrate` runs that step alone. Write a patch so that
it can run twice without harm, and never edit one that has run somewhere: write a new one. `db/patches/README.md`
explains why.
## 9. Shared hosting
On a shared webspace (IONOS, OVH, o2switch...), ssh works but sudo and the web server's configuration do not.
Declare the server `"managed": true`: nothing is reloaded, no vhost is written, and the check after the switch runs
from your machine. Point the domain, in the host's panel, at the target's `web` folder, which `install` links onto
the live release.
## Common mistakes
- **A secret in `deploy.json`.** It is refused. Use the ssh agent, or `deploy.credentials.json`.
- **Releasing work that is not committed.** It is not in the release: the build uses the commit.
- **A committed file in `shared`.** The first release seeds it in `shared/`, and no later commit ever reaches the
server. `shared` is for what git does not have: uploads, keys.
- **No `reload` on a server of yours.** PHP's opcache keeps the old release's files: make `reload` restart php-fpm
or Apache.
- **The domain pointed at the target's folder.** Every release becomes downloadable. Point it at
`current/www`, or at `web` on shared hosting.
## Going further
- Next: **Capstone: a projects board**, the whole track on one real feature.
- mmo.admin's [deploy](https://www.mmoframework.fr/en/documentation/mmo.admin/deploy.md): every key, every command,
shared hosting in detail. Its [backups](https://www.mmoframework.fr/en/documentation/mmo.admin/backups.md): backup
sets, restore, encryption.