How to get the deployed Git commit hash in Portainer CE

When Portainer Community Edition deploys a stack from a git repository, it clones the repo, runs docker compose up, and then deletes the .git directory. This means the usual git rev-parse HEAD trick doesn’t work inside the running container – there’s no git history left.
But Portainer does remember the commit it deployed. The stack API endpoint exposes it in GitConfig.ConfigHash:
bashCopy jsonCopy This is the exact commit SHA that Portainer cloned when it last (re)deployed the stack (here’s how the update mechanism works). You can query it at container startup to bake the version into your app. Here’s a minimal snippet that writes it to a file (using only Python stdlib, no extra dependencies needed in a slim image):
评论
?
参与讨论