@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: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:Install web stack | Install nginx
Deploy Arguments
Deploys accept the same global arguments as operations:Error Handling
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 functionsadd_deploy()- Add deploy to state (API mode only)_wrap_deploy()- Internal function that wraps deploy logic
