-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2840 from PrefectHQ/file_based_storage
File-based flow storage
- Loading branch information
Showing
23 changed files
with
661 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
feature: | ||
- "Flows can now be stored and executed using file-based storage - [#2840](https://github.com/PrefectHQ/prefect/pull/2840)" | ||
|
||
enhancement: | ||
- "Add GitHub storage for storing flows as files in a GitHub repo - [#2840](https://github.com/PrefectHQ/prefect/pull/2840)" | ||
- "Add `prefect register flow` CLI command for registering flows from files - [#2840](https://github.com/PrefectHQ/prefect/pull/2840)" | ||
- "Add default `GITHUB_ACCESS_TOKEN` secret - [#2840](https://github.com/PrefectHQ/prefect/pull/2840)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Using file based flow storage | ||
|
||
Prefect version `0.12.1` began to implement support for storing flows as paths to files. This means that flow code can change in between (or even during) runs without needing to be reregistered. As long as the structure of the flow itself does not change, only the task content, then a Prefect API backend will be able to execute the flow. This is a useful storage mechanism especially for testing, debugging, CI/CD processes, and more! | ||
|
||
### Example file based workflow | ||
|
||
In this example we will walk through a potential workflow you may use when registering flows with [GitHub](/api/latest/environments/storage.html#github) storage. This example takes place in a GitHub repository with the following structure: | ||
|
||
``` | ||
repo | ||
README.md | ||
flows/ | ||
my_flow.py | ||
``` | ||
|
||
First, compose your flow file and give the flow `GitHub` storage: | ||
|
||
```python | ||
# flows/my_flow.py | ||
|
||
from prefect import task, Flow | ||
from prefect.environments.storage import GitHub | ||
|
||
@task | ||
def get_data(): | ||
return [1, 2, 3, 4, 5] | ||
|
||
@task | ||
def print_data(data): | ||
print(data) | ||
|
||
with Flow("file-based-flow") as flow: | ||
data = get_data() | ||
print_data(data) | ||
|
||
flow.storage = GitHub( | ||
repo="org/repo", # name of repo | ||
path="flows/my_flow.py", # location of flow file in repo | ||
secrets=["GITHUB_ACCESS_TOKEN"] # name of personal access token secret | ||
) | ||
``` | ||
|
||
Here's a breakdown of the three kwargs set on the `GitHub` storage: | ||
|
||
- `repo`: the name of the repo that this code will live in | ||
- `path`: the location of the flow file in the repo. This must be an exact match to the path of the file. | ||
- `secrets`: the name of a [default Prefect secret](/core/concepts/secrets.html#default-secrets) which is a GitHub [personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). This is set so that when the flow is executed it has the proper permissions to pull the file from the repo. | ||
|
||
Push this code to the repository: | ||
|
||
```bash | ||
git add . | ||
git commit -m 'Add my flow' | ||
git push | ||
``` | ||
|
||
Now that the file exists on the repo the flow needs to be registered with a Prefect API backend (either Core's server or Prefect Cloud). | ||
|
||
```bash | ||
prefect register -f flows/my_flow.py | ||
Result check: OK | ||
Flow: http://localhost:8080/flow/9f5f7bea-186e-44d1-a746-417239663614 | ||
``` | ||
|
||
The flow is ready to run! Every time you need to change the code inside your flow's respective tasks all you need to do is commit that code to the same location in the repository and each subsequent run will use that code. | ||
|
||
::: warning Flow Structure | ||
If you change any of the structure of your flow such as task names, rearrange task order, etc. then you will need to reregister that flow. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
|
||
import click | ||
|
||
import prefect | ||
from prefect.utilities.storage import extract_flow_from_file | ||
|
||
|
||
@click.group(hidden=True) | ||
def register(): | ||
""" | ||
Register flows | ||
""" | ||
|
||
|
||
@register.command( | ||
hidden=True, | ||
context_settings=dict(ignore_unknown_options=True, allow_extra_args=True), | ||
) | ||
@click.option( | ||
"--file", | ||
"-f", | ||
required=True, | ||
help="A file that contains a flow", | ||
hidden=True, | ||
default=None, | ||
type=click.Path(exists=True), | ||
) | ||
@click.option( | ||
"--name", | ||
"-n", | ||
required=False, | ||
help="The `flow.name` to pull out of the file provided.", | ||
hidden=True, | ||
default=None, | ||
) | ||
@click.option( | ||
"--project", | ||
"-p", | ||
required=False, | ||
help="The name of a Prefect Cloud project to register this flow.", | ||
hidden=True, | ||
default=None, | ||
) | ||
def flow(file, name, project): | ||
""" | ||
Register a flow from a file. This call will pull a Flow object out of a `.py` file | ||
and call `flow.register` on it. | ||
\b | ||
Options: | ||
--file, -f TEXT The path to a local file which contains a flow [required] | ||
--name, -n TEXT The `flow.name` to pull out of the file provided. If a name | ||
is not provided then the first flow object found will be registered. | ||
--project TEXT The name of a Prefect Cloud project to register this flow | ||
\b | ||
Examples: | ||
$ prefect register flow --file my_flow.py --name My-Flow | ||
""" | ||
|
||
# Don't run extra `run` and `register` functions inside file | ||
with prefect.context({"loading_flow": True}): | ||
file_path = os.path.abspath(file) | ||
flow_obj = extract_flow_from_file(file_path=file_path, flow_name=name) | ||
|
||
flow_obj.register(project_name=project) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.