Fetching The Latest Git Hash For A Branch With Ansible
I’m currently working on an Ansible role to deploy an application to OpenShift. My application template uses a BuildConfig with a dockerStrategy, so when a build is run it runs against a remote git repo and branch.
I wanted my role to ensure that the current deployed application is up to date with the git branch, so we can push changes to that branch and whenever ansible runs it will know to reprocess the template, triggering a new build and deployment. (alternatively here we could use webhooks to trigger the build for us I suspect)
OpenShift nicely records the git ref used for a build:
So if we can determine what the latest git reference is for a branch with Ansible, we could easily compare these two values and see if a rebuild/redeploy is needed.
I wanted to do this without doing a full git clone as our repo in this case is quite large. As it turns out, this is quite easy with Ansible:
The result looks like this: