-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployment.py
83 lines (65 loc) · 2.74 KB
/
deployment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Deployment script for Prefect2 (Kubernetes Job block + Prefect Deployment)."""
import sys
import os
from os import environ, path
from prefect.deployments import Deployment
from main_flow import hello_world
from prefect.filesystems import GitHub
# sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), "../src")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# Verify Environment Variables
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print("Verifying environment variables...")
print(environ.get("WORK_POOL"))
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
# retrieve dynamic variables from environment
TIER_ENVIRONMENT = environ.get("WORK_POOL", "dev").upper()
WORK_POOL = environ.get("WORK_POOL", "dev")
GCP_PROJECT_ID = environ.get("GCP_PROJECT_ID", "fallback")
GCP_RESULTS_BUCKET = environ.get("GCP_RESULTS_BUCKET", f"{GCP_PROJECT_ID}-prefect-results")
PYTHON_VERSION = ".".join(environ.get("PYTHON_VERSION", "3.10").split(".")[:2])
PREFECT_VERSION = environ.get("PREFECT_VERSION", "2.14.3")
DEPLOYMENT_NAME = f"{hello_world.__name__}-{TIER_ENVIRONMENT}".replace('_', '-')
def main():
"""Main function."""
create_deployment()
def create_block():
BRANCH_REF = os.environ.get("BRANCH_REF", "branchrefnotset").split('/')[-1]
BLOCK_NAME = f'{hello_world.__name__}-{BRANCH_REF}'.replace('_', '-')
print(f"Creating block {BLOCK_NAME}")
print(f"BRANCH_REF: {BRANCH_REF}")
block = GitHub(
reference=BRANCH_REF,
repository="https://github.com/prefectcboyd/prefectcicd.git"
)
# block.get_directory("folder-in-repo") # specify a subfolder of repo
block.save(BLOCK_NAME, overwrite=True)
return block
def create_deployment():
"""Create deployment."""
print(f"Creating deployment: {DEPLOYMENT_NAME}")
environment = {
"WORK_POOL": WORK_POOL,
"GCP_PROJECT_ID": GCP_PROJECT_ID,
"GCP_RESULTS_BUCKET": GCP_RESULTS_BUCKET,
"PREFECT_VERSION": PREFECT_VERSION,
"PYTHON_VERSION": PYTHON_VERSION,
}
infra_overrides = {
"env": environment
}
deployment = Deployment.build_from_flow(
flow=hello_world,
name=DEPLOYMENT_NAME,
work_pool_name=WORK_POOL,
work_queue_name="default",
infra_overrides=infra_overrides,
path="/",
storage=create_block(),
entrypoint=f"flow.py:hello_world",
)
uuid = deployment.apply()
print(f"Saved deployment {DEPLOYMENT_NAME}: {uuid}")
if __name__ == "__main__":
main()