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");
}
})