Skip to main content
The @deploy decorator enables you to create reusable deployment functions that can be used across CLI and API-based execution. This allows you to build pyinfra extensions and share common deployment patterns.

@deploy Decorator

The @deploy decorator wraps a function and makes it a reusable deployment that can be called multiple times with different parameters.

Parameters

str
Name for the deploy. If not provided, the function name is used.
dict
Default data values available to operations within the deploy.

Using Deploys

Deploys can be called like regular functions:
In API mode, use add_deploy():

Deploy Context

Deploys create a context that wraps all operations called within them. This context:
  • Groups operations for better organization
  • Applies deploy-wide arguments to all operations
  • Provides access to deploy data

Deploy Data

Deploys can define default data that operations can access:

add_deploy Function

add_deploy() programmatically adds a deploy to the state. This should only be used in API mode.

Example

Nested Deploys

Deploys can call other deploys, creating a hierarchy:
Nested deploy names are combined: Install web stack | Install nginx

Deploy Arguments

Deploys accept the same global arguments as operations:

Error Handling

The @deploy decorator must be called with parentheses, even if you’re not passing any arguments:

Complete Example

Here’s a complete example showing deploy creation and usage:

Source Reference

Location: src/pyinfra/api/deploy.py:57

Key Functions

  • deploy() - Decorator to create deploy functions
  • add_deploy() - Add deploy to state (API mode only)
  • _wrap_deploy() - Internal function that wraps deploy logic