# Deploying the Strapi to production

Everything runs from your Mac's terminal. The server (`3.7.0.49`, Ubuntu, pm2) runs out of memory during a build, so the build happens here and only the result (`build`) is shipped.

```bash
git push                  # only pushed commits can be deployed
npm run build:prod        # tests + production build + package, stamped with the commit
npm run deploy:prod       # upload -> server switches to that commit -> old build kept -> swap -> pm2 restart -> health check
npm run deploy:rollback   # swap back to the previous release (run again to return)
```

## What deploy:prod does on the server
1. Refuses to run if code was edited on the server, or the commit is not on GitLab.
2. Moves the server's code to exactly the commit that was built (`git merge --ff-only`). Runs `npm ci` only if `package-lock.json` changed.
3. Unpacks the new build next to the live one (`build-new`), then swaps with two renames: `build` → `build-bkp`, `build-new` → `build`. Visitors never see a half-copied folder.
4. Runs `pm2 restart` and waits for the app to answer. If it does not, the old code and build are put back automatically.

The `pm2 restart` itself means a few seconds of downtime.

`npm test` checks the blog HTML cleanup (`tests/`). build:prod runs it first.

## One-time setup on a new Mac
Add this to `~/.ssh/config` (the key must be `chmod 400`):

```
Host wedigtech-prod
  HostName 3.7.0.49
  User ubuntu
  IdentityFile ~/Downloads/wedigtech_light_keys.pem
  IdentitiesOnly yes
```

Test it with `ssh wedigtech-prod echo ok`.

## Troubleshooting
- **"'pm2' is not available to non-interactive SSH":** node was installed in a way a non-login shell cannot see. On the server, run `which pm2 node` and add their folder to the `PATH` line in `scripts/remote-release.sh`.
- **"not answering":** the release was already undone. Look at `pm2 logs <name> --lines 100` on the server.
- **Testing these scripts without production:** set `DEPLOY_HOST=local DEPLOY_LOCAL_DIR=/path/to/a/clone`.
