- Include source code directly in your runtime: Often, this means building your code into a Docker image.
- Retrieve code from storage at runtime: The worker pulls code from a specified location before starting the flow run.
- Local filesystem
- Git-based storage (GitHub, GitLab, Bitbucket)
- Blob storage (AWS S3, Azure Blob Storage, GCP GCS)
Deployment creation options
As detailed in the Deployment overview, you can create a deployment in one of two main ways:-
Python code with the
flow.deploymethod-
When using
.deploy, specify a storage location for your flow with theflow.from_sourcemethod. -
The
sourceis either a URL to a git repository or a storage object. For example:- A local directory:
source=Path(__file__).parentorsource="/path/to/file" - A URL to a git repository:
source="https://github.com/org/my-repo.git" - A storage object:
source=GitRepository(url="https://github.com/org/my-repo.git")
- A local directory:
-
The
entrypointis either the path to the file and the function name separated by a colon (for example,my_flow.py:my_func), or a Python module path (for example,my_module.my_flow.my_func).
-
When using
-
YAML specification defined in a
prefect.yamlfile-
To create a
prefect.yamlfile interactively, runprefect deployfrom the CLI and follow the prompts. -
The
prefect.yamlfile may define apullsection that specifies the storage location for your flow. For example:- Set the working directory:
- Clone a git repository:
- Pull from blob storage:
-
To create a
from_source or prefect.yaml to specify the storage location for your flow code, the resulting deployment will have a set of pull steps that your worker will use to retrieve the flow code at runtime.
Store code locally
If using a Process work pool, you can use one of the remote code storage options shown above, or you can store your flow code in a local folder. Here is an example of how to create a deployment with flow code stored locally:Git-based storage
Git-based version control platforms provide redundancy, version control, and collaboration capabilities. Prefect supports: For a public repository, you can use the repository URL directly. If you are using a private repository and are authenticated in your environment at deployment creation and deployment execution, you can use the repository URL directly. Alternatively, for a private repository, you can create aSecret block or git-platform-specific credentials block to store your credentials:
Then you can reference this block in the Python deploy method or the prefect.yaml file pull step.
If using the Python deploy method with a private repository that references a block, provide a GitRepository object instead of a URL, as shown below.
- GitHub
- Bitbucket
- GitLab
Secret block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.If using a GitHubCredentials block to store your credentials, you can create it ahead of time and reference it at deployment creation.- Install
prefect-githubwithpip install -U prefect-github - Register all block types defined in
prefect-githubwithprefect block register -m prefect_github - Create a
GitHubCredentialsblock through code or the Prefect UI and reference it at deployment creation as shown above.
branch if creating a GitRepository object.
The default is "main".
Prefect Cloud GitHub App integration
If you use Prefect Cloud, you can install the Prefect Cloud GitHub App to authenticate to GitHub at runtime and pull private repository code without storing long-lived credentials. This approach is recommended for GitHub.com organizations that enforce SAML/SSO (GitHub Enterprise Cloud), because it removes the need for Personal Access Tokens (PATs) that must be individually authorized for SAML. Instead, an organization owner installs the GitHub App once, and every deployment in the organization can pull code through it.This integration works with GitHub.com and GitHub Enterprise Cloud. It is not compatible
with GitHub Enterprise Server (self-hosted) installations.
How it works
The Prefect Cloud GitHub App uses GitHub’s app installation token mechanism:- An organization owner installs the app and grants it access to selected repositories.
- At deployment runtime, the worker calls
prefect-cloud github tokento request a short-lived installation access token from Prefect Cloud. - Prefect Cloud exchanges its credentials with GitHub to generate the token, scoped only to the repositories the app can access.
- The worker uses this token to clone the repository, then the token expires.
Security details for InfoSec review
| Property | Detail |
|---|---|
| Token type | GitHub App installation access token |
| Token lifetime | Short-lived (expires within one hour) |
| Token scope | Read-only access to repository contents, limited to repositories selected during app installation |
| Credential storage | No customer credentials or long-lived tokens are stored in your deployment configuration. Prefect Cloud holds the GitHub App’s private key to mint installation tokens on your behalf. |
| SAML/SSO compatibility | The GitHub App is authorized at the organization level, bypassing the per-user SAML token authorization requirement |
| Revocation | An organization owner can uninstall the app or change repository access at any time from GitHub App settings |
Set up the GitHub App
Install the GitHub App
Navigate to the Prefect Cloud GitHub App installation page
and select the GitHub organization and repositories you want to grant access to.If your organization enforces SAML/SSO, an organization owner must approve the app
installation. The app is then authorized for SAML access automatically.
The
prefect-cloud github token command requires that the worker’s environment is
authenticated to Prefect Cloud. Ensure that PREFECT_API_KEY and PREFECT_API_URL are
set in the worker’s environment, or that the worker is logged in through prefect cloud login.Docker-based storage
Another popular flow code storage option is to include it in a Docker image. All work pool options except Process and Prefect Managed allow you to bake your code into a Docker image. To create a deployment with Docker-based flow code storage use the Pythondeploy method or create a prefect.yaml file.
If you use the Python
deploy method to store the flow code in a Docker image, you don’t need to use the from_source method.prefect.yaml file below was generated by running prefect deploy from the CLI (a few lines of metadata were excluded from the top of the file output for brevity).
Note that the build section is necessary if baking your flow code into a Docker image.
.deploy will build a Docker image that includes your flow code and any pip packages specified in a requirements.txt file.
In the examples above, we elected not to push the resulting image to a remote registry.
To push the image to a remote registry, pass push=True in the Python deploy method or add a push_docker_image step to the push section of the prefect.yaml file.
Custom Docker image
If animage is not specified by one of the methods above, deployment flow runs associated with a Docker work pool will use the base Prefect image (e.g. prefecthq/prefect:3-latest) when executing.
Alternatively, you can create a custom Docker image outside of Prefect by running docker build && docker push elsewhere (e.g. in your CI/CD pipeline) and then reference the resulting image in the job_variables section of your deployment definition, or set the image as a default directly on the work pool.
For more information, see this discussion of custom Docker images.
Blob storage
Another option for flow code storage is any fsspec-supported storage location, such as AWS S3, Azure Blob Storage, or GCP GCS. If the storage location is publicly available, or if you are authenticated in the environment where you are creating and running your deployment, you can reference the storage location directly. You don’t need to pass credentials explicitly. To pass credentials explicitly to authenticate to your storage location, you can use either of the following block types:- Prefect integration library storage blocks, such as the
prefect-awslibrary’sS3Bucketblock, which can use aAWSCredentialsblock when it is created. Secretblocks
If you use a storage block such as the
S3Bucket block, you need to have the prefect-aws library available in the environment where your flow code runs.You can do any of the following to make the library available:- Install the library into the execution environment directly
- Specify the library in the work pool’s Base Job Template in the Environment Variables section like this:
{"EXTRA_PIP_PACKAGES":"prefect-aws"} - Specify the library in the environment variables of the
deploymethod as shown in the examples below - Specify the library in a
requirements.txtfile and reference the file in thepullstep of theprefect.yamlfile like this:
prefect.yaml example includes an additional line to reference a credentials block if authenticating to a private storage location through that option.
We also include Python code that shows how to use an existing storage block and an example of that creates, but doesn’t save, a storage block that references an existing nested credentials block.
- AWS S3 bucket
- Azure Blob Storage container
- GCP GCS bucket
AwsCredentials block:- Install the prefect-aws library with
pip install -U prefect-aws - Register the blocks in prefect-aws with
prefect block register -m prefect_aws - Create a user with a role with read and write permissions to access the bucket. If using the UI, create an access key pair with IAM -> Users -> Security credentials -> Access keys -> Create access key. Choose Use case -> Other and then copy the Access key and Secret access key values.
- Create an
AWSCredentialsblock in code or the Prefect UI. In addition to the block name, most users will fill in the AWS Access Key ID and AWS Access Key Secret fields. - Reference the block as shown above.