1

I am trying to add a new variable to an existing Devops ReleaseEnvironment and redeploy this existing releaseEnvironment. I'm using Microsoft.VisualStudio.Services.Release.Client version 16.199.0-preview.

var connection = new VssConnection("someUrl", new VssBasicCredential(string.Empty, "somePAT"));
var client = connection.GetClient<ReleaseHttpClient>();

var projectHttpClient = connection.GetClient<ProjectHttpClient>();
var project = await projectHttpClient.GetProject("xxx");
var metadata = new ReleaseEnvironmentUpdateMetadata
{
    Status = EnvironmentStatus.InProgress
};

metadata.Variables.Add("someVariable",
    new ConfigurationVariableValue()
    {
        Value = "xxx",
        AllowOverride = true,
        IsSecret = true
    });


await client.UpdateReleaseEnvironmentAsync(metadata, project.Id, 999,999);

However, cause this variable does not exist yet, I'm getting the error: Unhandled exception. Microsoft.VisualStudio.Services.Common.VssServiceException: Variable(s) someVariable do not exist in the release environment at scope: PRD. New variables cannot be added while creating deployment.

However it looks like it's possible to do it via the UI. So I was wondering how I can automate this. Does anyone have an idea?

Thx in advance.

Best regards, JeffVN

2
  • I'm not sure this would be the same as the UI nor do I think you can change anything about the variables during or after because they get captured. For example, if I reran a completed pipeline after updating a variable or adding a new one, it would not use them. Can you give the steps you were able to use to get it to work on the UI? Commented Jan 11, 2022 at 17:38
  • 1
    You can open an existing release pipeline. Then click on the edit button > choose Edit Release. Then you'll see a notification: You can edit approvals, tasks, and variables of this release. Edits will be saved only to this release. If you then add a pipeline variable you for example also used for as a variable in a group it will override the variable in the group. Do make sure you select the correct Scope. This has to be the name of the Environment and not "Release" (that didn't seem to work). Save this and redeploy the Release for that environment. Hope this helps Commented Jan 11, 2022 at 20:38

1 Answer 1

1

Found the solution.

You'll just need to fetch the Release and update the variables in the Environment:

var connection = new VssConnection("someUrl", new VssBasicCredential(string.Empty, "somePAT"));
var client = connection.GetClient<ReleaseHttpClient>();
var project = await projectHttpClient.GetProject("xxx");
var release = await client.GetReleaseAsync(project.Id, 999);
if (release == null)
{
    Console.WriteLine("Release 999 not found");
    return;
}
var releaseEnv = release.Environments.FirstOrDefault(x => x.Id == 999);
if (releaseEnv == null)
{
    Console.WriteLine("ReleaseEnvironment 999 not found");
    return;
}

if (!releaseEnv.Variables.ContainsKey("someVariable"))
{
    releaseEnv.Variables.Add("someVariable", new ConfigurationVariableValue()
        {
            Value = "xxx",
            AllowOverride = true,
            IsSecret = true
        });
}
else
{
    releaseEnv.Variables["someVariable"].Value = "xxx";
}

await client.UpdateReleaseAsync(release, project.Id, 999);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.