0

I have setup and configured by gulp process locally and now I am trying to get it working with Teamcity.

I have already installed node, and the gulp plug in.

One of the gulp packages I am using is gulp-bump so that I can use the %build-number% variable and use it to set the version of my package.json file. I am using yargs so that I can read arguments from the command line

This is the gulp task that I am using

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

To get this to work, from the command line i call

gulp build --version=2.3.4

build is the gulp task that I want to run, and --version is the value i want to read from the command line.

The task setVersion is a dependency of the build task. There are other gulp tasks that use the package.json file to be included at the op of all .css and js files that are outputted but these are called as part of the build task using the package 'run-sequence'

When I run this from the command line, everything works as expected.

Within my teamcity build I have two steps defined.

Step 1 is Node.js NPM runner, which i can see is correctly pulling down all the relevant packages .

Step 2 is using the Gulp runner. Within this task, I have set the gulp task as "build", and the additional command line parameters as --version=%build.number%

But it does not appear that the version number is being set correctly as even though in the build log it is saying

"Setting version to 2.3.4"

It is not setting the version correctly as when I review the css and js files after the build, they are not using the correct version.

However I know that teamcity is using %build.number% correctly as the next task is to produce a nuget file using NuGet Pack, and the version is set to %build.number%, and the file name of the nuget file produced does correctly include the version number.

I just cannot get the version within package.json set correctly as part of the build process.

What else can I do to correctly pass in the build number and to correctly set the version of package.json

Update

After the comments received, I have discovered that the error was with the way in which I was reading package.json

My gulpfile.js had the following:

var pkg = require('./package.json'); 
var banner = ['/**',
    ' * <%= pkg.name %>',
    ' * @version v<%= pkg.version %>',
    ' */',
    ''].join('\n');

I was using package gulp-header to then insert the banner at the top of the page.

$.header(banner, { pkg: pkg })

Looking at the answer to this question : Javascript: get package.json data in gulpfile.js it was mentioned that when you use require to load package.json the file is cached, and if any process modifies the package.json file, gulp will not update the version that it has already cached.

So I modified the routine to read the package.json file on demand:

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

So in my processes that where appending the header, I changed to call getPackageJson. Now when i run the process in teamcity, it is correctly setting the correct version

e.g.

var pkg = getPackageJson();
return gulp
    .src(config.src.sass)
    .pipe($.sass())
    .pipe($.header(banner, { pkg: pkg }))
Community
  • 1
  • 1
Darren Guy
  • 1,123
  • 2
  • 13
  • 39
  • may be `config.dest.root is not the location being used by the later processes` so the package.config being picked is different that package.config being changed – harishr Apr 11 '16 at 17:01
  • @entre my config.dest.root is defined as './' and later processes which use the package.json are referring to a pkg variable which is defined as require('./package.json'); so I am pretty confident that they are all referencing the same file. – Darren Guy Apr 12 '16 at 09:11
  • @entre Your comment made me think and I have found this question: http://stackoverflow.com/questions/22645779/javascript-get-package-json-data-in-gulpfile-js I am reading the package file at the start of the process as a require, and the file contents are being cached. When i then bump the variable in packages.json, the file in cache is not being updated. Looks like I have to change how I am reading the contents of this file – Darren Guy Apr 12 '16 at 09:22

1 Answers1

0

just check if below works

update package.json

 gulp.task('bump-version', function () {
  return gulp.src(['./bower.json', './package.json'])
    .pipe(plugins.if(!bumpOpt.type, plugins.prompt.prompt(bumpPrompt, function(res){ bumpOpt.type = res.bump; })))
    .pipe(plugins.bump(bumpOpt).on('error', plugins.util.log))
    .pipe(gulp.dest('./'));
});

and then read the version using

function getPackageJsonVersion () {
//We parse the json file instead of using require because require caches multiple calls so the version number won't be updated
return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
};

and use this function like

gulp.task('create-new-tag', function (cb) {
  var version = getPackageJsonVersion();
  plugins.git.tag(version, 'Created Tag for version: ' + version, function (error) {
    if (error) { return cb(error); }
    plugins.git.push('origin', 'master', {args: '--tags'}, cb);
  });
});
harishr
  • 17,807
  • 9
  • 78
  • 125