3

We are using TeamCity for our builds and deployments, and as part of this we are wanting to have TeamCity define the version number in the package.json file. We historically were using gulp-bump which would bump the version number however TeamCity wasn't basing its build number upon the details within the package.json file.

So is there a simple way to get TC to update the package.json with a specific version number? I know I could pass a var to gulp somehow and get it to set the package.json version that way but I was hoping there was a more "automated" way of doing it.

Grofit
  • 17,693
  • 24
  • 96
  • 176

4 Answers4

4

You can easily change this command in the build step section package.json using this command

npm version "1.0.%build.number%" --allow-same-version

--allow-same-version This is for a duplicate version

Yaser Darzi
  • 1,480
  • 12
  • 24
  • when I try to do this command, npm tries to commit changes into git repository and fails with this error: npm ERR! error: object directory /TeamCity/buildAgent/work/ae09b07f727ad1af/.git/objects/C:\TeamCity\buildAgent\system\git\git-39B46704.git\objects does not exist; check .git/objects/info/alternates npm ERR! fatal: could not parse HEAD – Noy Oliel Aug 15 '21 at 13:03
2

I just added a command line build step to teamcity with the following:

call npm install -g json
call json -I -f package.json -e "this.version='1.0.%build.number%'"

Which will adjust your patch number to whatever the team city build number is

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
Shannon McRae
  • 231
  • 3
  • 10
1

It depends on your definition of automated. There is no Build Feature within TeamCity to update the various .json files that gulp / bower etc using Teamcity 9.

Like you I am using gulp-bump to set the version number, but I have found a way to set the version number inside the package.json file using the teamcity version number.

FYI, I am using var $ = require('gulp-load-plugins')({ lazy: true }); so if you see $ sign that's is why

I am also using yargs to read from the command line

That is the gulp task that i am using, and I have set it as a dependency of my mail build task.

var args = require('yargs').argv;
var fs = require('fs');
gulp.task('setVersion', function () {
    var msg = 'Setting version';
    var version = args.version;
    var options = {};
    if (version) {
        options.version = version;
        msg += ' to ' + version;
    }
    log(msg);
    return gulp
        .src(config.packages)
        .pipe($.print())
        .pipe(version ? $.bump(options) : $.util.noop())
        .pipe(gulp.dest(config.dest.root));
});

This is my config. It is abbreviated to only show whats needed

module.exports = function () {
    var root = './';
    var config = {
        dest: {
            root: root,
        },
        packages: [
            './package.json'
        ]
    };

    return config;
};

I check to make sure that a version is passed because when the gulpfile is being executed on dev machines, there is no need to set the version number because I only care about the build number on TeamCity.

so If you called it from the node command line it would be

gulp build --version=2.3.4

There is a gotcha. If you use the package.json via via a require statement, then the value will be cached and you may not get the correct version when you use the file. Using the require statement caused me problems as I initially struggled to determine why it was using the previous build number and not the new build number.

Because of this I created a new function to load the file when I needed to read the contents

function getPackageJson() {
    return JSON.parse(fs.readFileSync('./package.json'));
}

Within my TeamCity build, I have a build step using the Gulp runner type. For the Commands I only have "build"

And I have the following for Additional command line parameters

--version=%build.number%

This for me sets the version in package.json. If you are using bower etc, then just add the additional json files to config.packages

Darren Guy
  • 1,123
  • 2
  • 13
  • 39
1

In TeamCity I found it much easier (after trying various techniques) just to do a find and replace on the package.json using Powershell.

Add a Powershell build step with:

$packageFile = "D:\PathToPackageFile\package.json"
(Get-Content $packageFile).replace('"version": "0.0.0"', '"version": "%build.number%"') 
| Set-Content $packageFile
bigtv
  • 2,611
  • 5
  • 29
  • 42