<@ULVA73B9P> I'd like to keep all blocks under sou...
# marvin-in-the-wild
d
@Marvin I'd like to keep all blocks under source control. Is that plausible? If so, how can I go about doing so?
m
Yes, it is possible to keep all Prefect blocks under source control. Here are the steps to do so: 1. Create a Custom Block: Create the block definition in your code. You can create Prefect block classes in Python scripts and version control those scripts. Example:
Copy code
from prefect.blocks.core import Block
   from pydantic import BaseModel

   class MyCustomBlock(Block, BaseModel):
       my_field: str
2. Register the Block: Register the block with Prefect so that it can be used in deployments. Ensure this command is run whenever the block class changes.
Copy code
prefect block register -m path.to.block.module
3. Store Configuration in Code: Store block instances or configurations as JSON or YAML files in your repository. Example YAML configuration:
Copy code
my_block:
     my_field: "value"
4. Instantiate Blocks Programmatically: Use the stored configuration to instantiate blocks in your Prefect scripts. Example:
Copy code
from my_module import MyCustomBlock
   import yaml

   with open("block_configuration.yaml", "r") as file:
       config = yaml.safe_load(file)

   block_instance = MyCustomBlock(**config['my_block'])
   block_instance.save('my-block-name')  # Save the block to Prefect API
5. Using the Block: You can now reference the block in your flows or deployments by name. Example:
Copy code
from prefect.deployments import Deployment
   from my_flow import my_flow
   
   deployment = Deployment.build_from_flow(
       flow=my_flow,
       name="my-deployment",
       parameters={"param": "value"},
       storage="my_block_name"
   )
   deployment.apply()
Make sure you push any updates or changes to your version control system manually. For further details, visit the Prefect documentation on block storage and deployment. If you encounter issues with specific Prefect features or configurations, it might be helpful to check Prefect's GitHub issues for known bugs or feature requests.
n
marvin started off fine here but went a little off the rails @Del do you mean you have a bunch of custom blocks types that you want to keep in github someplace and then load in and use elsewhere?
d
Hey @Nate we have a bunch of blocks defined via the UI in general. I'd like to place and keep all blocks that are currently defined under source control.
n
I see, yeah generally you can just write them in some script and do
my_block.save('name', overwrite=True)
after making whatever content changes you need is there a problem with this approach you've hit?
d
I haven't tried this approach. I'm mostly looking for best practices to keep our blocks portable and versioned.
n
yep if you want a programmatic/declarative way to make sure your server / workspace has a set of blocks with certain values, I'd write a file like
save_blocks.py
Copy code
from prefect.blocks.system import Secret

def save_the_blocks():
  Secret(value=os.getenv('FOO')).save('your-block-name', overwrite=True)
  ... for all the blocks you want

if __name__ == "__main__":
  save_the_blocks()
and so on and if you have many workspaces / servers, you can wrap
save_the_blocks
in
with temporary_settings({PREFECT_API_URL: "<http://whatever>"}):
to easily register them to different workspaces
d
Got it, thanks