Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I place my Meteor apps version number in the UI?

Tags:

git

meteor

I have a meteor application that has different branches for different clients. This is because clients want special things. Of course right?

I would like to be able to place the git branch and tag/hash for the deployed version of the app for that client in the UI somewhere.

The question is how? Is there a way in Meteor to get this information and simply use it?

Thanks!

like image 205
DigiLord Avatar asked Sep 11 '14 20:09

DigiLord


4 Answers

You can use Meteor.gitCommitHash on both client and server.

like image 110
Nathan Schwarz Avatar answered Sep 19 '22 05:09

Nathan Schwarz


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.

like image 23
Mark Lynch Avatar answered Oct 07 '22 15:10

Mark Lynch


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");
  }
})
like image 11
Kuba Wyrobek Avatar answered Oct 07 '22 17:10

Kuba Wyrobek


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.

like image 5
johipsum Avatar answered Oct 07 '22 15:10

johipsum