Search code examples
gitversionpushincrement

Updating project version number on git push


I have a project hosted on Github, and I would like to set it up such that the project has a version number, and the version number is only updated when the master branch is updated, either directly by a push, or via a merged Pull Request.

Is there any way to let git/Bitbucket update a specific number in a specific file? It is OK if there is just a dedicated file for this purpose, containing a single number. I assume I will be able to write code that upon asking what version my project is using, will simply read that file. My project is a C# Web API project, although I'm not sure that matters much.


Solution

  • Is there any way to let git/Bitbucket update a specific number in a specific file?

    Not a file tracked in the repo, since it would be part of another commit.
    As explained in "How do I enable ident string for Git repos?", that is a task (generating build info) better left to a build system, rather than some kind of hook/content filter.

    Any repo hosting services can have webhooks (GitHub, BitBucket) allowing you to attach some kind of process to an event (like a git push), but those process would be executed on the client side (a client listening to the JSON payload generated by said webhook): nothing is executed on GitHub/BitBucket servers (except maybe BitBucket brokers, mainly to communicate with other third-party services).

    One way which could work though is a post-commit hook (executed on the client, before pushing) using:

    That way, each commit would update a git notes on said commit, with the content of a git describe.

    The idea with git notes is they don't change the SHA1 associated to the repo, meaning you can add those after a commit.
    You can then push those notes to GitHub and see them.

    git push origin refs/notes/*
    

    See more with "Git Notes and GitHub " by Matthew McCullough.