TFS API:克隆构建定义环境

时间:2017-12-28 23:43:37

标签: powershell tfs tfsbuild azure-devops-rest-api

我想克隆构建定义中的现有环境。

这可以通过TFS GUI实现,但似乎API本身不支持它。

到目前为止,我已经尝试了以下(在PowerShell中):

下载构建定义并反序列化

class OrderLineImport(models.TransientModel):
    _name = 'order.line.import'

    customer_name = fields.Char(
        string='Customer',
    )

    product_code = fields.Char(
        string='Product Code',
    )

    @api.model
    def create(self, vals):
        product_code = vals.get('product_code', False)
        customer_name = vals.get('customer_name', False)

        product_id = self._get_product_id(product_code)
        customer_id = self._get_customer_id(customer_name)

        order_line = {
            'product_id': product_id,
            'customer_id': customer_id
        }
        self.env['amgl.order_line'].create(order_line)   # the important create

        # You can create records of the model order.line.import as well, but they are going to disappear
        # because it is a transient model. I think that this is good to keep your database clean

        return super(OrderLineImport, self).create(vals)

    def _get_product_id(self, product_code):
        if product_code:
            product = self.env['amgl.products'].search([
                ('product_code', '=', product_code)
            ])
            if len(product) == 1:
                return product.id
            elif len(product) == 0:
                raise Warning(_('Product code not found: %s') % product_code)
            else:
                raise Warning(_('More than one product_code found: %s') % product_code)
        else:
            return False

    def _get_customer_id(self, customer_name):
        if customer_name:
            customer = self.env['amgl.customer'].search([
                ('name', '=', customer_name)
            ])
            if len(customer) == 1:
                return customer.id
            elif len(product) == 0:
                raise Warning(_('Customer not found: %s') % customer_name)
            else:
                raise Warning(_('More than one customer found: %s') % customer_name)
        else:
            return False

表示环境的重复JSON块,更改唯一属性

[void][System.Reflection.Assembly]::LoadFile("$pwd\Newtonsoft.Json.dll")

$TargetDefinitionName = "Name"

$TargetDefinitionID = ($DefinitionsOverview | Where-Object { $_.name -eq $TargetDefinitionName } | Select-Object -First 1).id

$TargetDefinitionURL = "$TfsUri/$TargetDefinitionID"

$TargetDefinitionJSON = Invoke-WebRequest -Uri "$TargetDefinitionURL" -UseDefaultCredentials

$DeserializedBuildDefinition = [Newtonsoft.Json.JsonConvert]::DeserializeObject($TargetDefinitionJSON.Content)

$DeserializedBuildDefinition.ToString()

序列化JSON并回发(使用新环境)

$NewEnvironmentString = $DeserializedBuildDefinition.environments[4].ToString()

$DeserializedBuildDefinition.environments.Add(5)

$DeserializedBuildDefinition.environments[5] = $NewEnvironmentString 

$DeserializedBuildDefinition.environments[5].name.value = "NewEnvironment"

$DeserializedBuildDefinition.environments[5].rank.value = "6"

$DeserializedBuildDefinition.environments[5].id.value = "1665"

$DeserializedBuildDefinition.revision.value = "20"

问题:$ Response.StatusDescription给出“OK”,但构建定义中没有出现新环境。

有一种想法是,除了“名称”,“ID”和“排名”之外,还有其他值需要在每个我缺少的环境中都是唯一的。

我还尝试手动克隆环境,保存定义的JSON表示,删除环境以及发回JSON。新环境仍然没有出现。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

未来:基于Yaml的构建定义作为代码正在顺利进行,至少对VSTS而言。这不仅无法满足眼前的需求,但可能会提供更好,更轻便的未来方法。

答案 1 :(得分:1)

复制环境时,需要更改“id”,“name”和“rank”。 并且“id”需要更改为“0”。另外,请尝试使用api-version = 2.3-preview.1

然后使用api of update a release definition更新定义:

PUT http://tfs2015:8080/tfs/teamprojectcollection/teamProject/_apis/release/definitions?api-version=2.3-preview.1
{
  ……
}

我已经使用TFS 2015 Update 4进行了测试.Api可以成功克隆环境。

答案 2 :(得分:0)

我创建了一个用于克隆TFS构建的工具,因为(至少使用TFS 2015更新1)web“克隆”功能不会克隆“存储库”或“触发器”选项卡。我们的一些构建步骤有点复杂,所以我还编写了一些简单的转换来根据目标分支自动设置值。

我使用BuildHttpClient程序集中的Microsoft.TeamFoundation.Build2.WebApi对象来获取原始构建定义(BuildDefinition对象,使用GetDefinitionAsync()方法),转换任何构建步骤值,然后调用CreateDefinitionAsync() BuildHttpClient创建新版本。

非常可靠地运行,并且它似乎使用TFS REST API。

如果有任何兴趣,我很乐意为此创建一个更深入的教程。

相关问题