Skip to content

JPS upgrade hooks for live environments — onBeforeUpdate / onAfterUpdate

How update manifests differ from install manifests, what runs when, and how to write hooks that don't break a production environment during a patch.

You ship a new application version through your JPS update flow on Friday afternoon. The update applies. The site is 502 for nine minutes while Nginx tries to reload against a config that references a directory the new release doesn’t ship. The rollback is messy because you didn’t capture the previous state before the update started. The post-mortem comes around to the obvious gap: the manifest had onInstall actions copied to onAfterUpdate and nothing in onBeforeUpdate to capture what the environment looked like before you touched it.

What’s happening

A JPS update manifest is a separate document from the install manifest, with type: update at the top. The lifecycle is different:

  • Install runs once, provisions nodes from scratch, has onInstall and onAfterInstall actions.
  • Update runs against an already-existing environment, does not provision nodes (unless you call API actions that do so explicitly), and has onBeforeUpdate and onAfterUpdate actions plus the install-time hooks that the engine reuses.

The hook ordering during an update:

  1. onBeforeUpdate — fires first, against the existing environment. Use it to take backups, capture state, and stop services that would conflict with the update.
  2. The body of the update runs — file uploads, command execution, API calls.
  3. onAfterUpdate — fires after the update body succeeds. Use it to start services back up, run migrations, smoke-test, and signal the operator.

If onBeforeUpdate returns non-zero, the update aborts before any change is made — this is the right signal mechanism for “the environment isn’t healthy enough to update right now.” If the update body fails after onBeforeUpdate succeeded, you’re in a partially-applied state and need a rollback path. JPS does not auto-rollback updates the way it aborts a failed install. That’s by design — the engine has no way to know which side effects are reversible.

Two patterns matter for live environments:

  • Pre-update health check + state snapshot. onBeforeUpdate should refuse to proceed if anything is unhealthy, and should capture enough state to roll back if the update body fails halfway.
  • Drain, update, restore, verify. Live environments behind a load balancer should be drained from the LB before the runtime is touched, then re-added after the smoke test passes. The update manifest is the right place to encode that sequence.

How to verify

After a release of your update manifest to the private marketplace, walk it through a non-production environment first:

# 1. Apply the update to a test environment
jelastic marketplace.app.ExecAction \
  --envName acme-test \
  --appUniqueName acme-php-demo \
  --action update \
  --params '{"version":"1.5.0"}' \
  --session "$SESSION_ID"

# 2. Tail the update log
jelastic environment.control.GetEnvInfo \
  --envName acme-test --session "$SESSION_ID" \
  | jq -r '.env.log[] | select(.action=="update")'

# 3. Confirm the new version is what the smoke test sees
curl -s https://acme-test.<your-paas>.com/version

You’re looking for three signals:

  • onBeforeUpdate produced a state-snapshot artifact (a database dump, a tar of the webroot, whatever the rollback needs) and the path to it is in the log.
  • The update body’s actions are individually idempotent — applying the same update twice doesn’t double up files or re-run migrations against a schema that’s already current.
  • onAfterUpdate ran a smoke test against the actual application URL, not just against localhost on a node.

If any of those three is missing, fix the manifest before you point it at production.

The fix

A working update manifest skeleton for the PHP demo environment from the install article. Save as update.yml and publish to your private marketplace:

type: update
version: 1.6
jpsType: update
name: Acme PHP Demo update
id: acme-php-demo
description:
  text: Roll Acme PHP demo forward to a new application version.
baseUrl: https://assets.example.com/acme/jps/1.0

settings:
  fields:
    - name: targetVersion
      caption: Target version
      type: string
      required: true
      regex: "^[0-9]+\\.[0-9]+\\.[0-9]+$"
      regexText: Use semantic versioning, e.g. 1.5.0.

onBeforeUpdate:
  - log: "Pre-update checks for ${env.envName} -> ${settings.targetVersion}"

  # Refuse to update if the load balancer reports the backend unhealthy
  - cmd[cp]: |-
      code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1/health)
      if [ "$code" != "200" ]; then
        echo "Pre-update health check failed: $code" >&2
        exit 1
      fi
    user: root

  # Capture state: db dump + webroot tar
  - cmd[sqldb]: |-
      set -e
      mkdir -p /var/lib/jelastic/keys/rollback
      ts=$(date -u +%Y%m%dT%H%M%SZ)
      mysqldump --single-transaction --quick \
        -u${globals.DB_USER} -p${globals.DB_PASSWORD} \
        ${globals.DB_NAME} | gzip \
        > /var/lib/jelastic/keys/rollback/db-${ts}.sql.gz
      echo "Wrote db-${ts}.sql.gz"
    user: root

  - cmd[cp]: |-
      set -e
      mkdir -p /var/lib/jelastic/keys/rollback
      ts=$(date -u +%Y%m%dT%H%M%SZ)
      tar -C /var/www/webroot -czf \
        /var/lib/jelastic/keys/rollback/webroot-${ts}.tar.gz ROOT
      echo "Wrote webroot-${ts}.tar.gz"
    user: root

  # Mark this node out-of-rotation in the LB if the environment has one.
  # For a single-node cp this is a no-op; for a multi-node cp the LB drains.
  - api: env.balancer.SetNodeStatus
    nodeGroup: cp
    nodeType: nginxphp
    status: maintenance
    onFail: ignore

onInstall:
  - log: "Applying release ${settings.targetVersion}"

  - upload:
      - nodeGroup: cp
        sourcePath: ${baseUrl}/releases/acme-${settings.targetVersion}.tar.gz
        destPath: /tmp/app-new.tar.gz

  # Idempotent extract: stage to a versioned directory, then atomically swap
  - cmd[cp]: |-
      set -e
      target=/var/www/webroot/ROOT-${settings.targetVersion}
      if [ -d "$target" ]; then
        echo "Target ${target} already exists — assuming idempotent re-run"
      else
        mkdir -p "$target"
        tar -xzf /tmp/app-new.tar.gz -C "$target" --strip-components=1
        chown -R nginx:nginx "$target"
      fi
      rm -f /tmp/app-new.tar.gz
    user: root

  # Preserve env.php across the swap
  - cmd[cp]: |-
      cp /var/www/webroot/ROOT/config/env.php \
         /var/www/webroot/ROOT-${settings.targetVersion}/config/env.php
      chown nginx:nginx /var/www/webroot/ROOT-${settings.targetVersion}/config/env.php
    user: root

  # Atomic symlink swap
  - cmd[cp]: |-
      set -e
      ln -sfn /var/www/webroot/ROOT-${settings.targetVersion} \
              /var/www/webroot/ROOT.next
      mv -Tf /var/www/webroot/ROOT.next /var/www/webroot/ROOT
    user: root

  # Run forward-migrations from the new release if a migrate script exists
  - cmd[cp]: |-
      if [ -x /var/www/webroot/ROOT/bin/migrate.sh ]; then
        /var/www/webroot/ROOT/bin/migrate.sh \
          --db-host ${nodes.sqldb.address} \
          --db-name ${globals.DB_NAME} \
          --db-user ${globals.DB_USER} \
          --db-password ${globals.DB_PASSWORD}
      else
        echo "No migrate.sh in this release — skipping"
      fi
    user: root

onAfterUpdate:
  # Reload nginx — but ONLY if -t passes
  - cmd[cp]: |-
      nginx -t
      systemctl reload nginx
    user: root

  # Smoke test
  - cmd[cp]: |-
      for i in $(seq 1 20); do
        code=$(curl -s -o /dev/null -w '%{http_code}' \
          http://127.0.0.1/version)
        body=$(curl -s http://127.0.0.1/version)
        if [ "$code" = "200" ] && \
           echo "$body" | grep -q "${settings.targetVersion}"; then
          echo "Smoke test OK — version reports ${settings.targetVersion}"
          exit 0
        fi
        sleep 3
      done
      echo "Smoke test never saw ${settings.targetVersion}" >&2
      exit 1
    user: root

  # Bring the node back into rotation
  - api: env.balancer.SetNodeStatus
    nodeGroup: cp
    nodeType: nginxphp
    status: active
    onFail: ignore

  - log: "Update to ${settings.targetVersion} complete"

success:
  text: |-
    Acme demo is now running ${settings.targetVersion}.
    Rollback artifacts are on the database node under
    /var/lib/jelastic/keys/rollback/ (db dump + webroot tar).
    Clean them up once you've confirmed the new version is healthy.

The patterns that keep this safe in production:

  • Versioned webroot directories with an atomic symlink swap. mv -Tf is atomic on the same filesystem; the old version stays on disk for fast rollback. Don’t rm -rf the old release in the update — leave that to a separate cleanup manifest you run after a soak period.
  • Configuration carried across the swap. env.php is in the old release directory; copy it into the new one before the symlink moves. If you regenerate it inside the update, you’re rotating database passwords by accident.
  • Migrations are idempotent and live in the release tarball, not in the manifest. A migration that’s safe to re-run is a migration you can apply twice if the update aborts and you retry. The manifest’s job is to invoke the migration script; the script’s job is to know whether each step is already applied.
  • onAfterUpdate smoke test verifies the new version actually reports the new version. Curl-ing /health and getting 200 doesn’t tell you the swap worked; curl-ing /version and getting the target version string does.
  • onFail: ignore on LB drain/restore. A single-node environment has no LB and these API calls return errors that aren’t worth aborting on. For multi-node environments where the LB is real, drop onFail: ignore and let the failure abort the update at the right moment.

If the update body fails after onBeforeUpdate succeeded, you have rollback artifacts and a symlink to flip. The dedicated partial-install recovery article in this series covers the recovery procedure end-to-end.

Stack Harbor authors and tests update manifests for customers running production workloads on managed PaaS — pre-update health checks, drain/restore around the LB, and the staging pipeline that verifies the update before it touches customer traffic. If you’re shipping JPS updates by hand on Friday afternoons, our deployment management team takes that off your plate.