Skip to content

import-page-draft: fail fast on environment-like site names, clean up temp files on any exit - #296

Merged
jasperf merged 1 commit into
mainfrom
fix/import-page-draft-site-name-and-trap
Nov 17, 2026
Merged

jasperf merged 1 commit into
mainfrom
fix/import-page-draft-site-name-and-trap

Conversation

@jasperf

@jasperf jasperf commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

This PR hardens wp-cli/content-creation/import-page-draft.sh with two robustness fixes: an early argument-validation guard and comprehensive temp-file cleanup. When a site name of local, production, or both was passed in the site-name position (argument drift from older documented invocations), the script silently constructed /srv/www/<environment>/current as its path and only failed much later inside trellis vm shell with a confusing error. It also relied on inline rm -f calls after each success path, so under set -e any failed run left the stripped draft in /tmp, its copy in the Bedrock site root, and — after a failed production import — the scp'd copy on the remote server. Both issues are points 1 and 2 of #232 (point 3, header-comment stripping, landed earlier in #246). The change spans 2 files with 31 insertions and 4 deletions, and includes a corresponding CHANGELOG.md entry under the Unreleased Fixed section.

Input validation and fail-fast behavior:

  • Added a guard immediately after the draft-file check that rejects local, production, or both as the site name — these values belong in the environment argument position, and previously produced a nonsensical SERVER_PATH built from the environment string.
  • The script now exits with the full usage line ($0 <draft-file> <page-id> [site-name] [environment] [template]) before touching the filesystem or starting any remote operations, instead of failing deep inside trellis VM shell.
  • The check mirrors the existing environment validation that follows it, keeping the two guards consistent in style and error format.

Temp-file cleanup on all exit paths:

  • Introduced a cleanup() function registered with trap cleanup EXIT, which removes the stripped draft from /tmp and the copy from the site directory (SITE_DIR/$TMP_NAME) on any exit — success, failure, or error from set -e.
  • The cleanup also removes the remote copy at $SERVER_PATH/$TMP_NAME over SSH, gated behind a PROD_UPLOADED flag that flips to yes only after the scp succeeds, so the trap never attempts a remote removal for a run that never uploaded anything.
  • The remote rm -f inside cleanup is best-effort (2>/dev/null || true) so a transient SSH failure during cleanup cannot mask the original exit status or break the trap.
  • Removed the now-redundant inline rm -f calls from the stray-comment error path, the end of update_local(), the end of update_production(), and the script's final line, consolidating all removal logic in one place.

Documentation:

Files Changed:

… in import-page-draft

A site name of local, production or both is the environment argument in
the wrong position; the script built /srv/www/<environment>/current from
it and only failed inside trellis vm shell. Exit early with the usage
line instead.

set -e without a trap meant cleanup ran only on success, so a failed run
left the stripped draft in /tmp, its copy in the Bedrock site root and
the scp'd copy on the server after a failed production import. An EXIT
trap now removes all three on any exit; the remote remove is best-effort
and only runs once the scp happened.

Resolves #232 (points 1 and 2; point 3 landed in #246).
@jasperf
jasperf merged commit de45d26 into main Nov 17, 2026
2 checks passed
@jasperf
jasperf deleted the fix/import-page-draft-site-name-and-trap branch November 17, 2026 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant