How can I place my Meteor apps version number in the UI?
For the most part I liked Kuba's answer and I actually tried to use his node script but ran in to problems with the promise library. Anyway, I wrote my own post-commit bash script and I think it's cleaner and easier like this.
ver=$(git describe --abbrev=0)
complete=$(git describe)
branch=$(git rev-parse --abbrev-ref HEAD)
commit=$(git rev-parse HEAD)
timestamp=$(git log -1 --date=short --pretty=format:%cd)
cat > private/version.json << EOF
{
"basic": "$ver",
"complete": "$complete",
"branch": "$branch",
"commit": "$commit",
"timestamp": "$timestamp"
}
EOF
I agree with everything else in Kuba's answer on where to store the file and how to access it in the server.
I just wrote a package for that and published it on Atmosphere. The package comes with an Template-Helper to show your git commit hash, tag or branch just like this:
<div>
<p>short hash: {{gitRev 'short'}}</p>
<p>long hash: {{gitRev 'long'}}</p>
<p>tag: {{gitRev 'tag'}}</p>
<p>branch: {{gitRev 'branch'}}</p>
</div>
See atmospherejs.com/johdirr/meteor-git-rev for details.
In my production apps I solved this issue in this way:
Files
App/.git/hooks/post.commit
App/MeteorApp/hooks/post-commit-version
App structure:
App
.git
hooks
post-commit (file)
MeteorApp
client
server
both
private
version.json
hooks
post-commit-version (file)
Whenever developer commits code .git/hooks/post-commit
is executed which executes nodejs script
stored in App/MeteorApp/hooks/post-commit-version
.
Script post-commit-version
generates version.json
in App/MeteorApp/private
dir in format:
{
"timestamp": "29-08-2014 23:16",
"branch": "master",
"commit": "3332f6dcbde57105a8dc353e5e878651cab89856"
}
Everything stored in private
is accessible to server on production.
How to display version.json
in app ?
App/MeteorApp/both/collections/Version.js:
Version = new Meteor.Collection('version');
App/MeteorApp/server/startup.js
Meteor.startup(function(){
if (Version.find().count() > 0){
Version.remove({});
}
Version.insert(JSON.parse(Assets.getText("version.json")));
})
After application is deployed it will fire startup
callbacks and version will be inserted to collection Version
.
App/MeteorApp/server/publish/version.js:
Meteor.publish('version', function () {
return Version.find();
});
App/MeteorApp/client/startup.js:
Meteor.startup(function(){
Meteor.subscribe("version");
})
And then somewhere in template simply create helper:
Template.template_name.helpers({
version:function(){
return Version.findOne();
}
})
In template_name you display version using {{version.commit}}
{{version.branch}}
{{version.timestamp}}
.
Side note 1
Script post-commit-version
don't have js
extension, because I don't want meteor to include it in bundle or reload app in development every time I change this file.
However it is possible to use post-commit-version.js
when that file is stored in .dir
(like App/MeteorApp/.hooks
) as directories having .
as first character are not processed by meteor.
Side note 2
Another possibility would be load version.json
on server side Meteor.startup
, parse json and attach to global variable like App.version
. Later use it with Meteor.method
:
Meteor.methods({
getVersion:function(){
return App.version;
}
})
On client you simply call method:
Meteor.call("getVersion", function(error,version){
if(error) {
throw new Error("Cannot get version");
return;
}
Session.set("version",version)
})
Some template's helper could use it :
Template.template_name.helpers({
version:function(){
return Session.get("version");
}
})