CI/CD resources for working with Prefect.
.deploy
flow method or a prefect.yaml
configuration file, are both designed for building and pushing images to a Docker registry.
main
branch. This workflow builds and pushes a Docker image containing your flow code to Docker Hub, then deploys the flow to Prefect Cloud.
PREFECT_API_URL
and
PREFECT_API_KEY
as secrets in your repository’s settings. This allows them to be accessed in your CI/CD runner’s environment without exposing them in any scripts or configuration files.
In this scenario, deploying flows involves building and pushing Docker images, so add DOCKER_USERNAME
and DOCKER_PASSWORD
as secrets to your repository as well.
Create secrets for GitHub Actions in your repository under
Settings -> Secrets and variables -> Actions -> New repository secret:
.github/workflows/
directory in the root of your repository. In their simplest form,
GitHub workflow files are made up of triggers and jobs.
The on:
trigger is set to run the workflow each time a push occurs on the main
branch of the repository.
The deploy
job is comprised of four steps
:
Checkout
clones your repository into the GitHub Actions runner so you can reference files or
run scripts from your repository in later steps.Log in to Docker Hub
authenticates to DockerHub so your image can be pushed to the Docker
registry in your DockerHub account. docker/login-action is an
existing GitHub action maintained by Docker. with:
passes values into the Action, similar to passing
parameters to a function.Setup Python
installs your selected version of Python.Prefect Deploy
installs the dependencies used in your flow, then deploys your flow. env:
makes
the PREFECT_API_KEY
and PREFECT_API_URL
secrets from your repository available as environment variables during this step’s execution.flow.py
.github/workflows/deploy-prefect-flow.yaml
Prefect Deploy
step includes output about
your image build and push, and the creation/update of your deployment.
branches:
- which branch has changed. This ultimately selects which Prefect workspace a
deployment is created or updated in. In this example, changes on the stg
branch deploy flows to a
staging workspace, and changes on the main
branch deploy flows to a production workspace.paths:
- which project folders’ files have changed. Since each project folder contains its own flows,
dependencies, and prefect.yaml
, it represents a complete set of logic and configuration that can deploy
independently. Each project in this repository gets its own GitHub Actions workflow YAML file.prefect.yaml
file in each project folder depends on environment variables dictated by the
selected job in each CI/CD workflow; enabling external code storage for Prefect deployments that is clearly
separated across projects and environments.
setup-python
action offers caching options
so Python packages do not have to be downloaded on repeat workflow runs.
build-push-action
for building Docker images also offers
caching options for GitHub Actions.
If you are not using GitHub, other remote cache backends
are available as well.
prefect.yaml
,
especially in cases where a repository contains flows used in multiple deployments across multiple
Prefect Cloud workspaces.
Here’s an example of integrating these actions into the workflow above:
docker/login-action
GitHub Action supports pushing images to a wide variety of image registries.
For example, if you are storing Docker images in AWS Elastic Container Registry, you can add your ECR
registry URL to the registry
key in the with:
part of the action and use an AWS_ACCESS_KEY_ID
and
AWS_SECRET_ACCESS_KEY
as your username
and password
.