How do I use spacectl commands in Spacelift hooks?
Last updated: November 25, 2026
You can run spacectl in hooks and tasks, for example to read another stack's outputs or to mount a file on another stack.
Install spacectl
spacectl isn't included in the default runner image. Install it in a hook, or add it to a custom runner image. For example, as a before_init hook (or before_perform for tasks):
mkdir -p /mnt/workspace/bin && cd /mnt/workspace/bin && wget -qO spacectl.zip https://github.com/spacelift-io/spacectl/releases/download/v1.30.0/spacectl_1.30.0_linux_amd64.zip && unzip -oq spacectl.zip && chmod +x spacectlThen call it as /mnt/workspace/bin/spacectl. On ARM workers, use linux_arm64 in the URL.
Authentication
Spacelift injects a SPACELIFT_API_TOKEN into every run and task, and spacectl uses it automatically. You don't need to log in.
Don't unset or override
SPACELIFT_API_TOKEN. spacectl needs it inside a run.Don't set
SPACELIFT_OIDC_TOKENyourself. Spacelift provides it in runs where OIDC is available.
Some commands need a logged-in user and don't work inside a run, for example spacectl whoami, spacectl stack show and spacectl stack environment list. They fail with unauthorized or not available for machine sessions.
Fix "denied" or "unauthorized" errors
Without a role, a stack can change only its own settings, and read stacks in its own space, or in parent spaces if its space inherits from them. Anything else fails with errors such as:
failed to query GraphQL API when checking if a stack exists: denied
unauthorized: You can re-login using `spacectl profile login`Ignore the "re-login" hint: there's no user to log in as inside a run. Instead, attach a role to the stack that runs the command:
Open the stack and go to Settings → Roles → Manage Roles.
Choose a role that allows the action. To change another stack, for example with
spacectl stack environment mount, use Space writer or higher. Space reader isn't enough.Set the space to the target stack's space, and click Add.
You can also attach roles with the spacelift_role_attachment resource. Use the narrowest role and space that work.
You can't attach a role on the root space to a stack outside root.
Sensitive outputs
spacectl stack outputs prints sensitive outputs in plain text in the run log. Avoid printing them in hooks and tasks.