Track Git commit for a flow run

If you want to track the Git commit that is associated with a flow run, you can save the SHA of the commit as the flow version:

import git
from prefect import flow 

@flow(
    version=git.Repo(search_parent_directories=True).head.commit.hexsha,
)
def my_flow():
    return 1

To get the commit from the SHA, type:

git show YOUR-SHA

You can also log the URL to the commit based on the SHA of the commit:

import git
from prefect import flow, get_run_logger

@flow
def my_flow():
    logger = get_run_logger()
    logger.info(f"URL for your code: https://github.com/khuyentran1401/prefect-dvc/commit/{git.Repo(search_parent_directories=True).head.commit.hexsha}")

Output:

...
16:04:36.699 | INFO    | prefect.flow_runs - URL for your    
code:                                                        
https://github.com/khuyentran1401/prefect-dvc/commit/bc75ccce
ec2cd92ebd5a1756ea7163895120a971                             
...
2 Likes