# Releasing compiled assets (#3598)

The Laravel Mix outputs (`public/js/main.js`, `public/css/main.css`, `public/css/nuhet.css`,
`public/js/agreement.js`, `public/js/nuhet/*`, `public/mix-manifest.json` and the rest) are
git-ignored. A deploy that only fast-forwards git therefore never changes them, and production
kept serving 2022 bundles until they were replaced by hand. Every release that changes a Mix
input, or any release when in doubt, ships a matching asset package built by
`tools/release/build-assets.sh` and installed by `tools/release/install-assets.sh`.

A few Mix outputs are committed (`public/medialibrary/main.js`, `public/css/tag-colors.css`,
`public/js/tag-colors.js`, `public/css/nuhet-pagination.css`, `public/css/nuhet/receiver-*.css`,
`public/css/legacy/sms.css`). Git delivers those; the package leaves them out, and the build
refuses to package when a fresh build differs from the committed copy (rebuild with
`npm run production`, commit, then package).

## 1. Build the package (local, not on production)

Build the exact commit that will be deployed:

```sh
cd /path/to/nuhet-checkout
tools/release/build-assets.sh --rev <deploy-commit> --editor-from <dir> --out /tmp/nuhet-asset-releases
```

The script exports the commit with `git archive` into a temporary directory, runs `npm ci` and
`mix --production` there, adds the newsletter editor build, and then:

- checks that every output in `webpack.mix.js` is in `mix-manifest.json`, every manifest entry
  has a non-empty file, and the version ids of the webpack bundles match the files;
- refuses a build that changed or removed a committed file;
- packs exactly the files the build added under `public/` into
  `nuhet-assets-<short>.tar.gz` (top folder `nuhet-assets-<short>/` with `public/...`,
  `SHA256SUMS` and `release-assets.json`), and writes `.tar.gz.sha256` and `.json` next to it;
- unpacks the tarball again and checks every file against `SHA256SUMS`.

The same commit and inputs give the same tarball bytes (fixed file order, owner and times).

**Newsletter editor.** `public/newsletter-editor/` has its own pinned build
(docs/ops/newsletter-editor-ui-3967.md), and a plain Mix build drops its five entries from
`mix-manifest.json`. Choose one:

- `--editor-from DIR`: DIR is a built `public/newsletter-editor` directory (all of it, not just
  the five entry files). If nothing under `tools/newsletter-editor/` changed since the editor
  build that production runs, reuse that build: the `public/newsletter-editor/` folder of the
  previous package, or a read-only copy of production's. Otherwise rebuild the editor first.
- `--without-editor`: no editor files and no editor manifest entries. `/newsletter-editor` then
  fails to load. Only for a server where the editor is not in use.

`resources/newsletter-editor/starter-previews.json` (written by `npm run production`) is
committed and arrives through git; the asset package does not carry it.

Keep the build log. Record the tarball sha256 from `nuhet-assets-<short>.tar.gz.sha256` in the
release record.

## 2. Transfer

Copy both `nuhet-assets-<short>.tar.gz` and `nuhet-assets-<short>.tar.gz.sha256` to the server
(native SFTP to `/tmp`, like the git bundle). The installer checks the sha256 itself; a
mismatch stops it before anything changes.

## 3. Install on production

Order: deploy the commit with the usual git bundle and `pull --ff-only` first, then install the
asset package built from that same commit. The installer refuses a package whose commit is not
the app's `git HEAD` (`--allow-other-commit` overrides it; there should be no reason to).

The installer is in the deployed checkout (`/var/www/html/tools/release/install-assets.sh`).
For the first release that adds it, copy it with the tarball and run that copy.

```sh
cd /var/www/html
stat -c '%U:%G %a' public public/js public/css    # who owns the asset folders
tools/release/install-assets.sh --app /var/www/html \
    --package /tmp/nuhet-assets-<short>.tar.gz --dry-run
tools/release/install-assets.sh --app /var/www/html \
    --package /tmp/nuhet-assets-<short>.tar.gz
```

What it does and why:

- **Permissions.** It runs with `umask 022` and writes every file `0644` (folders `0755`), so
  the SSH login's `umask 0002` cannot leave group-writable assets.
- **Ownership.** Files get the owner of `public/` (or `--owner USER:GROUP`). Run it as that
  user, or with sudo so it can `chown`; under sudo every folder it creates gets that owner
  too, and git is told to trust the checkout (`safe.directory`) for its `HEAD` check. It stops
  if it cannot give files the right owner. It never runs PHP, so the www-data rule in
  docs/ops/storage-permissions.md is not affected.
- **Writable folders.** Before the backup, and also in `--dry-run`, it checks that it can write
  the folder of every file it will write or remove (for a folder that does not exist yet, the
  nearest existing parent), the app root (for `asset-release.json`) and the backup folder. If
  one of them belongs to someone else, for example a `public/newsletter-editor/` owned by root
  while running as the deploy user, it lists them and stops before changing anything.
- **Atomic swap.** Each file is copied next to its target as a hidden temporary file and
  renamed over it, so a request gets either the old or the new file, never a partial one.
  All bundles are switched before `mix-manifest.json`, which goes last; pages pick up the new
  `?id=` versions from that moment.
- **Backup.** Before changing anything it copies the current version of every file it will
  touch to `$HOME/nuhet-asset-backups/<UTC time>-<short>/` (with `SHA256SUMS`, an `ABSENT`
  list of files that did not exist yet, and `INFO`), and checks the copy.
  The backup goes under `$HOME` of the user that runs it: with sudo that is normally
  `/root/nuhet-asset-backups/`, not the deploy user's home. Pass `--backup-root DIR` to choose.
- **Check.** Afterwards it compares every installed file with the package, writes
  `asset-release.json` (see below) and prints the exact rollback command.
- **Failure part way.** If anything fails after the first file was switched, it prints the
  exact `--rollback` command for the backup it just made before it exits.

`--dry-run` checks the tarball, its commit, ownership and folder permissions and lists new
and changed files without writing anything.

## 4. Verify

From any machine, compare what the site serves with the package:

```sh
cd nuhet-assets-<short>      # the unpacked tarball
while read -r sum path; do
  got=$(curl -fsS "https://post.nuhet.se/${path#public/}" | sha256sum | cut -d' ' -f1)
  [ "$got" = "$sum" ] && echo "ok  $path" || echo "BAD $path"
done < SHA256SUMS
```

Then open an ordinary legacy page and a v2 page and check that their CSS and JS load
(no 404 or `MixFileNotFoundException`), and, if the editor is in use, `/newsletter-editor`.

## 5. Roll back

Run the command the installer printed (it is also the `undo` value in `asset-release.json`;
under sudo the backup is in `/root/nuhet-asset-backups/`):

```sh
tools/release/install-assets.sh --app /var/www/html --rollback ~/nuhet-asset-backups/<UTC time>-<short>
```

It restores the saved files the same atomic way (manifest last), removes the files that did
not exist before, and makes its own backup first (a folder named
`<UTC time>-rollback-<undone backup>`), so a rollback can be undone too. Rolling back
code with git does not roll back assets; when a release is reverted, roll back its asset
install as well, or install the package of the commit you return to.

## Checking what production serves

Every install and rollback writes `asset-release.json` in the app root (`/var/www/html`, next
to `public/`, so the web server does not serve it; git-ignored; same owner as the assets):

```json
{
    "action": "install",
    "commit": "<the package's commit>",
    "package": "nuhet-assets-<short>.tar.gz",
    "package_sha256": "...",
    "time": "...",
    "by": "root (sudo from nuhetftp)",
    "backup": "/root/nuhet-asset-backups/<UTC time>-<short>",
    "undo": "<the exact rollback command>"
}
```

After a rollback, `action` is `rollback`, `commit` is the build that was recorded before the
undone install (`null` if none was recorded, for example the hand-copied assets from before
this script), and `undid` and `restored_from` name the backup it put back. Read this file
rather than the backup folders: the newest backup folder only shows the last run, which after
a rollback is a `-rollback-` folder holding the files the rollback replaced, and backups land
in the home of whoever ran the script (`/root` under sudo, the deploy user's home otherwise).

Each backup folder still has `INFO` and, when there was one, `previous-asset-release.json` (the
record it replaced); an install's backup also has `installed-release-assets.json` (commit and
file hashes of the package). To check freshness without a record, compare production's
`public/mix-manifest.json` and file hashes with a package built from production's `HEAD`
(read-only: `sha256sum` over the packaged paths on the server).
