Friday, November 18, 2011

CFExecute with Multiple Bash Commands Inline && Getting the latest GIT short hash && custom CSS/JS caching solution - OH MY!

This one was hard. I knew it was possible so it was only a matter of time, googling, and testing. I got it.

Here is the code. Here I'm using the command "git rev-parse --short HEAD" to get the short hash of the latest GIT commit. But, in order for that to work we need to get into the right directory and CFExecute *starts* in the user directory of whatever user the Coldfusion server is running in. If you haven't set one the it's probably "/root".

So we first need to change directories to the current web root, then we grab the short GIT log hash, like this:

<cfexecute name="/bin/bash" arguments="-c 'cd -P #ExpandPath('/')#; git rev-parse --short HEAD'" variable="variables.test1" timeout="1" />


But a note on the results. If you output the above <cfoutput>[#ariables.test1#]</cfoutput> with brackets around it you will see there is an extra space on the end. That annoys me but I couldn't shake it off. My only solution was to trim itself afterwards with a <cfset variables.test1 = TRIM(variables.test1) />. Lame.

So, why am I wanting the latest short GIT log hash? I'm glad you asked.

I'm using it in my CSS and Javascript file tag query strings to prevent improper caching of old CSS and Javascript files so that users will always have the latest changes without having to clear their cache.


<link href="/css/global.css?v=<cfoutput>#server.codeversion#</cfoutput>" type="text/css" rel="stylesheet" />
<script src="/javascript/<cfoutput>#server.codeversion#</cfoutput>"></script>


No comments: