简体   繁体   中英

Azure DevOps REST API: invalid build id/number when creating a release

I'm trying to create a release using DevOps REST APIs . The definition is correct since I can create it manually, but REST call fails with followin log:

##[Error 1]
Exception Message: 235 is not a valid Build ID or BuildNumber. Make sure that the build succeeded or partially succeeded and is not deleted. (type ReleaseManagementExternalServiceException)
Exception Stack Trace:    at Microsoft.VisualStudio.Services.ReleaseManagement.Artifact.Extensions.Build.BuildArtifact.GetBuildInternal(IVssRequestContext requestContext, Guid projectId, Int32 buildId) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\ArtifactType\Build\BuildArtifact.cs:line 3606
   at Microsoft.VisualStudio.Services.ReleaseManagement.Artifact.Extensions.Build.BuildArtifact.GetBuild(IVssRequestContext requestContext, Guid projectId, Int32 buildId) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\ArtifactType\Build\BuildArtifact.cs:line 391
   at Microsoft.VisualStudio.Services.ReleaseManagement.Artifact.Extensions.Build.BuildArtifact.GetArtifactConfigurationVariables(IVssRequestContext context, ArtifactSource artifactSource) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\ArtifactType\Build\BuildArtifact.cs:line 2744
   at Microsoft.VisualStudio.Services.ReleaseManagement.Server.Processors.DeployPhaseRunner.GetArtifactVariables(Release release, IVssRequestContext requestContext) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\Server\Processors\DeployPhaseRunner.cs:line 379
   at Microsoft.VisualStudio.Services.ReleaseManagement.Server.Processors.DeployPhaseRunner.GetMergedReleaseVariables(Release release, ReleaseEnvironment releaseEnvironment, ReleaseEnvironmentSnapshotDelta deploymentDelta, Boolean includeArtifactVariables) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\Server\Processors\DeployPhaseRunner.cs:line 582
   at Microsoft.VisualStudio.Services.ReleaseManagement.Server.Processors.DeployPhaseRunner.GetAutomationEngineInput(Release release, ReleaseEnvironment releaseEnvironment, ReleaseEnvironmentStep step, Int32 trialNumber, DeployPhaseSnapshot snapshotToProcess, ReleaseEnvironmentSnapshotDelta deploymentDelta) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\Server\Processors\DeployPhaseRunner.cs:line 287
   at Microsoft.VisualStudio.Services.ReleaseManagement.Server.Processors.DeployPhaseRunner.Run(Release release, ReleaseEnvironment releaseEnvironment, ReleaseEnvironmentStep deployStep, DeployPhaseSnapshot snapshotToProcess) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\Server\Processors\DeployPhaseRunner.cs:line 190

Inner Exception Details:

Exception Message: The requested build 235 could not be found.
 (type ReleaseManagementExternalServiceException)
Exception Stack Trace:    at Microsoft.VisualStudio.Services.ReleaseManagement.Common.Extensions.TaskExtension.GetResult[T](Task`1 task, CancellationToken token) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\Common\Extensions\TaskExtension.cs:line 43
   at Microsoft.VisualStudio.Services.ReleaseManagement.Artifact.Extensions.Build.BuildArtifact.GetBuildInternal(IVssRequestContext requestContext, Guid projectId, Int32 buildId) in D:\v2.0\P1\_work\7\s\ReleaseManagement\Service\ReleaseManagement2\ArtifactType\Build\BuildArtifact.cs:line 3600

Inner Exception Details:

Exception Message: The requested build 235 could not be found. (type BuildNotFoundException)
Exception Stack Trace:    at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<HandleResponseAsync>d__53.MoveNext() in D:\v2.0\P1\_work\7\s\Vssf\Client\WebApi\VssHttpClientBase.cs:line 942
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__51.MoveNext() in D:\v2.0\P1\_work\7\s\Vssf\Client\WebApi\VssHttpClientBase.cs:line 883
--- End of stack 

This is the PowerShell script I wrote:

using namespace System
using namespace System.Text

$pat = '****************************************************'

$credentials = [Convert]::ToBase64String([Encoding]::ASCII.GetBytes(":$pat"))
$organization = 'myorg'
$project = 'My%20Project'
$definitionId = 71
$buildId = 235 # this is the wrong value

$uri = "https://vsrm.dev.azure.com/$organization/$project/_apis/release/releases?api-version=5.0"

$json = @"
{
    "definitionId": $definitionId,
    "description": "Sample Release",
    "artifacts": [
      {
        "alias": "Source",
        "instanceReference": {
          "id": "$buildId",
          "name": null
        }
      }
    ],
    "isDraft": false,
    "reason": "none",
    "manualEnvironments": null
  }
"@

$response  = Invoke-RestMethod -Uri $uri -Method Post -ContentType 'application/json' -Body $json `
                                -Headers @{
                                    Authorization = "Basic $credentials" 
                                }

$response | ConvertTo-Json -Depth 100 | Out-File out.json

I set artifacts.instanceReference.id to the ID of the build pipeline. I know this is wrong, but from documentation I was unable to understand which value I need to set to properly create a new release.

What exactly is this value? And where I can get it with DevOps REST APIs?

Any help will be very appreciated!

Giacomo SS

Few minutes after I posted the question, I was able to find a solution based on this other post (found here).

Since I still don't know what is the required correct value , I now understand that artifacts content is optional.

So, this payload allows to create a new release without problems:

$json = @"
{
    "definitionId": $definitionId,
    "description": "Sample Release",
    "artifacts": [

    ],
    "isDraft": false,
    "reason": "none",
    "manualEnvironments": null
  }
"@

I'm grateful to people that posted the other question. Thank you!

I want to add I was able accomplish the same result using the Microsoft.VisualStudio.Services.Release.Client package in C# .

var pat = "****************************************************";
var organization = "myorg";
var project = "My%20Project";
var creds = new VssBasicCredential(string.Empty, pat);
var conn = new VssConnection(new Uri($"https://dev.azure.com/{organization}"), creds);
var client = conn.GetClient<ReleaseHttpClient>();
var release = client.CreateReleaseAsync(new ReleaseStartMetadata() {
    DefinitionId = 71,
    Description = "Sample Release",
    Reason = ReleaseReason.None
}, project).Result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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