Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I auto-increment an MVC 6 version number?

Previous versions of ASP.NET allowed you to auto-increment the version number via Project Properties. How can I do this in MVC 6?

like image 377
Stafford Williams Avatar asked Nov 18 '15 03:11

Stafford Williams


3 Answers

MVC 6 now uses project.json to track version and you can bump this number using gulp-bump.

Version Bumping

  1. Add gulp-bump to package.json > devDependencies

    gulp-bump": "1.0.0"

  2. Edit gulpfile.js

    • Add bump = require("gulp-bump") to the dependencies at the top
    • Add a task to bump the version number

      gulp.task("bump", function() {
        gulp.src("./project.json")
        .pipe(bump())
        .pipe(gulp.dest("./"));
      });
      
  3. Update project.json

    • By default the MVC template sets the version number to 1.0.0-*, change this to 1.0.0.
    • Add "gulp bump" to the bottom of "scripts" > "prepublish"

Now whenever you Publish, or dnu publish or run the gulp Task Runner the version number will bump.

Bonus

To display this version number in View add the following in the view;

@inject Microsoft.Extensions.PlatformAbstractions.IApplicationEnvironment appEnv
My version number is @(appEnv.ApplicationVersion)
like image 90
Stafford Williams Avatar answered Sep 21 '22 08:09

Stafford Williams


ASP.NET 5 (DNX) Answer

This is what the ASP.NET 5 team actually uses themselves. If you are using a continuous integration build server you can get your build server to set the DNX_BUILD_VERSION environment variable like so using PowerShell:

$env:DNX_BUILD_VERSION=$version

Your build machine then sets $version to 'build123' or something similar (It can't start with a number, has to be a character from the alphabet) Then, as long as your version number is set like so:

{
    "version": "1.0.0-*"
}

The star will be replaced with the value in the DNX_BUILD_VERSION environment variable. See the ASP.NET 5 GitHub page here for more info.

like image 29
Muhammad Rehan Saeed Avatar answered Sep 21 '22 08:09

Muhammad Rehan Saeed


For .NET Core (RTM) projects, you can use dotnet-bump. You can add it as a tool to your project, and call it from a postcompile script. http://github.com/BalassaMarton/dotnet-bump

like image 25
Márton Balassa Avatar answered Sep 18 '22 08:09

Márton Balassa