Skip to main content

Add lifecycle scripts to a pack

Add lifecycle hooks only when managed files cannot complete the operation. Scripts run with the consumer's permissions and may have effects LunaPack cannot roll back.

Run these authoring commands against the synthetic example-documentation-standard pack created by Create a first pack.

Choose a hook

Supported hooks are preInstall, postInstall, preUpdate, postUpdate, preUninstall, and postUninstall. Use a direct command when no script file is needed:

luna pack add hook script command postInstall dotnet tool restore `
--description "Restore repository-local .NET tools."

Use a packed script with an explicit runner when logic belongs in the pack:

luna pack add hook script file preUpdate scripts/check.ps1 pwsh `
-NoProfile `
--description "Check update prerequisites."

The script path is relative to pack.yml. Each trailing value becomes one literal process argument. LunaPack does not invoke an implicit shell. Add --replace when changing an existing hook.

Pass parameter values

Script arguments are Scriban templates, but the command, runner, and file are literal:

hooks:
preInstall:
- type: script
file: scripts/setup.ps1
runner: pwsh
arguments:
- -ProjectType
- '{{ projectType }}'
description: Configure project tooling.

Rendered arguments are shown before authorization and remain separate argv values.

Run hooks conditionally

Set condition to use the same parameter expression grammar as managed files. False conditions omit scripts before authorization and omit instructions before their files are loaded:

hooks:
postInstall:
- type: instruction
file: instructions/customize-security.md
condition: isDefault(securityUrl)

The luna pack add hook script command, script file, and instruction commands accept --condition or -c.

Inspect and test hooks

luna pack hooks
luna pack validate
luna install [email protected] --scripts skip --dry-run
luna install [email protected] --scripts prompt

Prompt mode asks consumers to approve untrusted hooks. --scripts run permits all non-disabled hooks for that invocation; --scripts skip runs none. Test approved hooks only in a disposable repository. Keep hooks deterministic, non-interactive, cross-platform where practical, and explicit about network or filesystem effects.

Composite packs can suppress dependency hooks with --disable-hook:

luna pack add reference csharpier 1.0.0 `
--disable-hook postInstall