【问题标题】:TFS 2015 release management via Java SDK?通过 Java SDK 进行 TFS 2015 发布管理?
【发布时间】:2017-11-22 12:19:28
【问题描述】:

借助本地 TFS 2015,我们正在尝试使用我们用 Java 编写的命令行工具来自动化发布管理。该工具已使用 TFS Java SDK 执行许多其他操作。

我们需要它:

  • 创建新版本
  • 将发布部署到指定环境

当通过 Web GUI 完成时,这两个都可以正常工作。但是,根据 Java SDK 中的类列表,这些功能似乎在 SDK 中不可用。这是正确的,还是有办法自动执行这些部署步骤?

如果做不到这一点,是否有 REST API?我们找到了一个,但它似乎需要比 TFS 2015 支持的 API 版本 4.0 更新...

【问题讨论】:

    标签: java tfs


    【解决方案1】:

    您可以使用 REST API 来实现这一点(使用 API 版本 api-version=2.3-preview.1):

    我使用以下 PS 脚本在 TFS 2015 Update 4 (Version 14.114.26403.0) 上进行了测试,它按预期工作。

    以下 PowerShell 脚本供您参考:

    创建新版本:(参见 REST API here

    Param(
       [string]$collectionurl = "http://server:8080/tfs/Collection",
       [string]$projectName = "ProjectName",
       [string]$keepForever = "true",
       [string]$user = "username",
       [string]$token = "token",
       [string]$releasedDefinitionId = "2" 
    )
    
    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    function CreateJsonBody
    {
    
        $value = @"
    {
     "definitionId":$releasedDefinitionId,
      "description":"",
       "artifacts":[
             {
               "alias":"VSTEST",
               "instanceReference":{
                         "name":"20171123.1",
                         "id":"1256",
                         "sourceBranch":"$/0418Scrum/web0418"}
            }],
        "isDraft":false,
        "manualEnvironments":[]
    }
    
    "@
    
     return $value
    }
    
    $json = CreateJsonBody
    
    $uri = "$($collectionurl)/$($projectName)/_apis/Release/releases?api-version=2.3-preview.1"
    $result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    $ReleaseID = $result.id
    
    Write-Host "ReleaseID:" $ReleaseID
    

    将发布部署到指定环境:(部署本示例中的第三个环境,根据您的要求进行更改即可。)

    Param(
       [string]$baseurl = "http://server:8080/tfs/CollectionLC",
       [string]$projectName = "ProjectName",
       [string]$keepForever = "true",
       [string]$user = "username",
       [string]$token = "token",
       [string]$ReleaseID = "3" 
    )
    
    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    #Get releaseresponse
    $Releaseurl= "$baseurl/$projectName/_apis/Release/releases/$ReleaseID" 
    
    $releaseresponse = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType application/json -Uri $Releaseurl
    
    #Get all of the environment IDs from the release response:
    $environmentIDs = $releaseresponse.environments.ForEach("id")
    
    #Get the specific environment ID by grabbing the element in the environment IDs array:
    $firstEnvironment = $environmentIDs[0]
    $secondEnvironment = $environmentIDs[1]
    $thirdEnvironment = $environmentIDs[2] # ...
    
    #Create the JSON body for the deployment:
    $deploymentbody = @" 
    {"status": "inprogress"} 
    "@
    
    #Invoke the REST method to redeploy the release:
    $DeployUrl = "$baseurl/$projectName/_apis/release/releases/$releaseid/environments/"+$thirdEnvironment+"?api-version=2.3-preview.1" # Change the envrionment ID accordingly based on your requirement.
    
    $DeployRelease = Invoke-RestMethod -Method Patch -ContentType application/json -Uri $DeployUrl -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $deploymentbody
    
    write-host "environmentIDs:" $environmentIDs
    

    您还可以将它们放在一起,创建一个新版本并在一个脚本中部署特定环境。在another thread 中查看我的答案。

    【讨论】:

    • 谢谢,非常有帮助,这正是我们所需要的!
    猜你喜欢
    • 2016-10-21
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2016-04-10
    • 2017-06-27
    • 2016-12-28
    • 2019-11-19
    • 2016-12-09
    相关资源
    最近更新 更多