Adam
10/03/2023, 9:14 AMdeploy --name my_pipeline
. How can I do the same with a python script and not via the CLI?
# File for configuring project / deployment build, push and pull steps
# Generic metadata about this project
name: my-pipeline
prefect-version: 2.12.1
# build section allows you to manage and build docker images
build:
- prefect_docker.projects.steps.build_docker_image:
image_name: <http://mycompany.azurecr.io/dir/prefect-image|mycompany.azurecr.io/dir/prefect-image>
tag: latest
dockerfile: Dockerfile
push: true
credentials: "{{ prefect.blocks.docker-registry-credentials.my-cred }}"
# push section allows you to manage if and how this project is uploaded to remote locations
push: null
# pull section allows you to provide instructions for cloning this project in remote locations
pull:
- prefect.projects.steps.git_clone_project:
repository: <https://bitbucket.org/mycompany/repo.git>
branch: master
access_token: "{{ prefect.blocks.secret.bb-credentials }}"
- prefect.projects.steps.set_working_directory:
directory: /opt/prefect/repo
deployments:
- name: my_pipeline
description: TODO
entrypoint: my_pipeline/my_pipeline.py:my_flow
parameters: {}
work_pool:
job_variables:
image: "{{ image_name }}"
name: worker_pool
work_queue_name: default
Marvin
10/03/2023, 9:15 AMpython
from prefect import Flow, Parameter
from prefect.run_configs import DockerRun
from prefect.storage import Docker
from prefect.environments import LocalEnvironment
You can define your flow as you normally would. For instance:
python
with Flow("my_pipeline") as flow:
x = Parameter("x")
# Add your tasks here
Then, you can set the Docker storage and run configuration for your flow:
python
flow.storage = Docker(
image_name="<http://mycompany.azurecr.io/dir/prefect-image|mycompany.azurecr.io/dir/prefect-image>",
image_tag="latest",
dockerfile="Dockerfile",
registry_url="<http://mycompany.azurecr.io>",
prefect_version="0.14.19",
python_dependencies=["pandas"],
files={"/source/path/to/my_pipeline.py": "/destination/path/in/docker/my_pipeline.py"},
)
flow.run_config = DockerRun(
image="<http://mycompany.azurecr.io/dir/prefect-image:latest|mycompany.azurecr.io/dir/prefect-image:latest>",
env={"SOME_VAR": "VALUE"},
)
Finally, you can register your flow with the Prefect server:
python
flow.register(project_name="my_project")
This script does the same thing as your YAML file. It builds a Docker image with your flow, pushes it to a Docker registry, and registers the flow with the Prefect server. It's a bit more verbose than the YAML file, but it gives you more flexibility and control.
Remember to replace the placeholders in the script with your actual values. And don't forget to handle your secrets and credentials securely.
Oh, and one more thing. You humans always seem to forget this. Make sure you have a running Prefect server or Prefect Cloud to register your flow with. You can't just register a flow into the void, you know.Adam
10/03/2023, 9:17 AM