โ05-11-2023 12:30 PM
I have a script in a repository on DataBricks. The script should log the current git commit ID of the repository. How can that be implemented?
I tried various command, for example:
result = subprocess.run(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, check=True)
but getting the following error:
Command '['git', 'rev-parse', 'HEAD']' returned non-zero exit status 128.
Any help with this will be appreciated ๐
โ05-12-2023 01:18 AM
I think this is because of the fact that the code and execution (clusters) are separated.
โ05-12-2023 01:18 AM
I think this is because of the fact that the code and execution (clusters) are separated.
โ05-14-2023 08:20 AM
Thanks for you reply, so this means its impossible to log the git commit id? There is no way to pass this information to the cluster?
โ05-15-2023 12:13 AM
I think that is indeed the case (not 100% sure as I do not know the innards of Databricks).
The on-demand cluster receives a program to be executed and I don't think a git history is passed.
โ02-20-2025 07:09 AM
As of February 2025 it is possible using the repos rest api (I use the sdk for simplicity) and dbutils. This code snippet assumes that the notebook it's being run in is in the root of the databricks repos folder that we're interested in.
โ03-06-2025 06:36 PM - edited โ03-06-2025 06:38 PM
This should be the accepted answer.
A bit shorter version:
import os
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
repo_info = next(w.repos.list(path_prefix=os.getcwd()))
print(repo_info.head_commit_id)
Wednesday
Here is a version of @vr 's solution that can be run from any folder within the rep. It uses regex to extract the root from the path in the form of \Repos\<username>\<some-repo:
import os
import re
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
repo_root = re.search(r'\/Repos\/[^\/]+\/[^\/]+', os.getcwd()).group(0)
repo_info = next(w.repos.list(path_prefix=repo_root))
print(repo_info.head_commit_id)
Passionate about hosting events and connecting people? Help us grow a vibrant local communityโsign up today to get started!
Sign Up Now