【问题标题】:VSTS / TFS Build only run NPM task when Git tag is committed and pushedVSTS / TFS Build 仅在提交和推送 Git 标签时运行 NPM 任务
【发布时间】:2018-01-29 12:32:09
【问题描述】:

Microsoft docs 上,它声明您可以为特定的构建任务设置条件。例如,在定义的 Git 分支的特定版本上运行构建。

但是是否也可以仅在提交包含 git-tag 的情况下使 VSTS 构建 NPM 包的条件?


更新 1: 命令$ git describe (source) 似乎是解决方案的一个可能部分。此命令获取最后一个提交哈希并匹配它以查看是否有注释标签。但是,它并不等于我们在 VSTS 的自定义条件中需要的布尔值。

更新 2: 使用 npm git-describe 你可以返回最新的提交是否有注释标签。 gulp 任务示例:

/** Example of a gulp task using git describe */
    var gulp                  = require('gulp');
    var {gitDescribeSync}     = require('git-describe');

    /**
     * @function
     * @name checkGitTag
     * @description Returns wether or not the latest commit has a tag
     * @returns {(String|null)} Git tag or null if no git tag exists on the commit
     */
    var checkGitTag = function() {

        var gitInfo = gitDescribeSync();

        // output the result to debug
        console.log(gitInfo.tag); 

        // gitInfo.tag seems to contain the logic needed
    };

    gulp.task('checkGitTag', checkGitTag);
    module.exports = checkGitTag;

也许通过在构建服务器上安装 NPM 包并使用类似的功能可以工作。要去测试一下。

【问题讨论】:

    标签: node.js git continuous-integration azure-devops


    【解决方案1】:

    是的,当构建提交包含标签时,可以有条件地运行任务。

    但是由于没有这样的预定义变量来记录构建提交的标签$(BUILD.SOURCEVERSION),所以您应该添加步骤(添加变量和 PowerShell 任务)以检查构建上是否有标签犯罪。详细步骤如下:

    1.添加一个默认值为0的变量(如result)。该变量用于检查构建提交是否包含标签。

    2.然后在要条件运行的任务前添加PowerShell任务,powershell脚本如下:

    $tag=$(git tag --contains $(BUILD.SOURCEVERSION))
    if($tag)
    {
      Write-Host "##vso[task.setvariable variable=result]1"
      echo "The build version $(BUILD.SOURCEVERSION) contains tag(s)"
    }
    else
    {
      Write-Host "##vso[task.setvariable variable=result]0"
      echo "The build version $(BUILD.SOURCEVERSION) does not contain any tags"
    }
    

    3. 最后为要运行的任务设置自定义条件,仅当提交包含如下标签时:

    and(succeeded(), eq(variables['result'], '1'))
    

    【讨论】:

    • 像魅力一样工作
    猜你喜欢
    • 2011-04-14
    • 2016-09-02
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    相关资源
    最近更新 更多