How do I access my .git directory from within my app?
Platform.sh DevRel
I want to be able to access the commit hash and timestamp of the current state of my project, but I don’t see a .git repository when I SSH in to my application. How can I access the information stored in .git?
0
Comments
Platform.sh doesn’t provide direct access to your
.gitrepository, but there are still a few methods you can use to access that information from your application.Get the commit hash of your current work tree
Although the commit hash of the state of your project isn’t available to your application directly, it is still possible to figure it out using the
PLATFORM_TREE_IDenvironment variable that is provided to your application and our CLI tool.Configure dependencies
First, you’ll need to configure an environment variable to pass your API token to the build process. In your project settings (not the settings for any individual environment), add an environment variable to contain your API token:
Set the name to
env:PLATFORMSH_CLI_TOKEN, mark it as sensitive, and indicate that you want it to be available during the build process but not the runtime.This proof-of-concept method has two dependencies: the Platform.sh CLI and the
jqcommand line tool for parsing JSON objects. We can install both in the build hook by adding these lines:Talking with the API
Create a new file called
get-commit.shin the root directory of your application, and paste in the script below. Make sure to make the script executable withchmod +x get-commit.sh.The above script makes a call to the
activitiesAPI endpoint for the project in which this script is being run and extracts a commit hash from the results. Let’s break it down:.[] |– This command iterates over the array of results and pipes each object to the next command.select(.type == "environment.push") |– This selects the subset of activity entries that are Git push events.select(.log | (contains("tree: " + $tree))) |– This takes thelogkey from each event entry and checks to see if it has the stringtree: $treecontained within, where$treeis the first seven characters of$PLATFORM_TREE_ID. The[0] | .payload.commits | .[0].sha– This line takes the first matching event and returns the SHA hash of the most recent commit.Final build hook
Call
get-commit.shfrom your build hook and output the result to a file called.commit, like so:This method should work reliably because the
.commitfile is only updated during the build hook. Thus, if you have multiple apps in one project, only apps that have code changes with each commit will be rebuilt, making the contents of the.commitfile permanently associated with the correct commit.Without dependencies
As mentioned above, this example is only a proof-of-concept. It is also possible to query the Platform.sh API directly without the CLI tool, and you can process the JSON results with any tool or language that you prefer.
Adding a
.gitrepo for other tools to useSome tools, such as a static site generator like Hugo, can parse the information contained within the
.gitdirectory and use it to determine when pages were last updated, who has edited a page, etc. For this use case, you’ll need more than just the commit hash.If you want to have access to that information in your application, you can clone the repository that is being used to build the application and extract the
.gitrepo from there.In your
.platform.app.yamlfile:Now any tools you want to use can process this repository as if it were any other working tree containing a
.gitdirectory.Hi,
8 months have passed, is there an easy way to access our .git directory within an app, or we still should do this?
Thanks
There is actually a way to make changes to code
https://docs.platform.sh/configuration/app/source-operations.html#source-operations-usage-examples
Please sign in to leave a comment.