简体   繁体   English

package.json 版本和 teamcity

[英]package.json versions and teamcity

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.我们使用 TeamCity 进行构建和部署,作为其中的一部分,我们希望 TeamCity 在 package.json 文件中定义版本号。 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.我们过去使用gulp-bump会增加版本号,但是 TeamCity 没有将其内部版本号基于 package.json 文件中的详细信息。

So is there a simple way to get TC to update the package.json with a specific version number?那么有没有一种简单的方法可以让 TC 用特定的版本号更新 package.json 呢? 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.我知道我可以以某种方式将 var 传递给 gulp 并让它以这种方式设置 package.json 版本,但我希望有一种更“自动化”的方式来做到这一点。

You can easily change this command in the build step section package.json using this command您可以使用此命令在构建步骤部分package.json轻松更改此命令

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

--allow-same-version This is for a duplicate version --allow-same-version这是用于重复版本

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. TeamCity中没有构建功能,无法使用Teamcity 9更新各种gulp / bower等.json文件。

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. 像您一样,我正在使用gulp-bump设置版本号,但是我发现了一种使用teamcity版本号在package.json文件中设置版本号的方法。

FYI, I am using var $ = require('gulp-load-plugins')({ lazy: true }); 仅供参考,我正在使用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 我也在用yargs从命令行读取

That is the gulp task that i am using, and I have set it as a dependency of my mail build task. 那就是我正在使用的gulp任务,我已将其设置为我的邮件构建任务的依赖项。

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. 我检查以确保通过了版本,因为在开发机上执行gulpfile时,无需设置版本号,因为我只关心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. 如果通过require语句使用package.json,则该值将被缓存,并且使用该文件时可能无法获得正确的版本。 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. 使用require语句给我带来了麻烦,因为我最初难以确定为什么使用以前的内部版本号而不是新的内部版本号。

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. 在TeamCity构建中,我有一个使用Gulp运行器类型的构建步骤。 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. 这对我来说在package.json中设置版本。 If you are using bower etc, then just add the additional json files to config.packages 如果您使用Bower等,则只需将其他json文件添加到config.packages

I just added a command line build step to teamcity with the following: 我刚刚向teamcity添加了以下命令行构建步骤:

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 它将调整您的补丁号以适应团队城市的构建号

In TeamCity I found it much easier (after trying various techniques) just to do a find and replace on the package.json using Powershell. 在TeamCity中,我发现(使用各种技巧后)使用Powershell进行查找和替换package.json变得容易得多。

Add a Powershell build step with: 通过以下步骤添加Powershell构建步骤:

$packageFile = "D:\PathToPackageFile\package.json"
(Get-Content $packageFile).replace('"version": "0.0.0"', '"version": "%build.number%"') 
| Set-Content $packageFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM