Passing values for prompt variables with Octopus.Client

usability
(Mahwag) #1

Hi,

Can you help me to pass values for prompt variables for a Deployment using Octopus.Client.dll.
Below is the code I am using to deploy, but I am not able to pass the variables.

I want to pass values for 2 variables
• Octopus.Machine.Names
• Octopus.Machine.Status

var server = “https://myoctopus”;
var apiKey = “API-SOMEAPIKEY”;

var endpoint = new OctopusServerEndpoint(server, apiKey);
var repository = new OctopusRepository(endpoint);

var environment = repository.Environments.FindByName(“Test”);
var project = repository.Projects.FindByName(“MyProject”);
var release = repository.Releases.FindOne(x => x.ProjectId == “Projects-1” && x.Version == “0.0.1”);

//creating the deployment object
var deployment = new DeploymentResource
{
ReleaseId = release.Id,
ProjectId = project.Id,
EnvironmentId = environment.Id,
FormValues = new Dictionary<string, string>

};

//Deploying the release in Octopus
repository.Deployments.Create(deployment);

(Dalmiro Grañas) #3

Hi,

Thanks for reaching out! First you need to declare your Prompted variables in your project like this: https://octopus.com/docs/deployment-process/variables/prompted-variables. Then you can pass values to those variables lie this:

FormValues = new Dictionary<string, string>
{
{ "PromptedVariable1", "Value1"},
{ "PromptedVariable2", "Value2" }
}

Hope that helps,
Dalmiro

(Mahwag) #4

Hi Dalmiro,

I had tried that before raising this ticket but it didnt work.

//creating the deployment object
var deployment = new DeploymentResource
{
ReleaseId = release.Id,
ProjectId = project.Id,
EnvironmentId = environment.Id,
FormValues = new Dictionary<string, string>
{
{ “Octopus.MyVar1”, “Value1” },
{ “Octopus.MyVar2”, “Value2” }
}
};

(Mahwag) #5

So passing the FormValues works for Prompt Variables which are not marked Required.
But next issue wherer I am stuck at is the values passed via FormValues are not used at all.
The Deployment is using the default values set for the Prompt Variables.

(Dalmiro Grañas) #6

Hi @mahwag

Sorry I’m a bit confused.

So passing the FormValues works for Prompt Variables which are not marked Required .

Here you say they are working for Required variables

But next issue wherer I am stuck at is the values passed via FormValues are not used at all.
The Deployment is using the default values set for the Prompt Variables.

But here you say the values are not being used (and the default ones are prevailing), which makes me think this is not really working?

Is there any chance you can send me:

  1. The full C# script you are using to create the release
  2. A Raw deployment log while having Debug variables turned on as shown in this document: https://octopus.com/docs/support/how-to-turn-on-variable-logging-and-export-the-task-log

Your log might include sensitive data from your project, so feel free to send it over our email thread so its not posted in this public forum

Best regards,
Dalmiro

(Mahwag) #7

var server = “https://myoctopus”;
var apiKey = “API-SOMEAPIKEY”; // Get this from your ‘profile’ page in the Octopus web portal
var endpoint = new OctopusServerEndpoint(server, apiKey);
var repository = new OctopusRepository(endpoint);

        var environment = repository.Environments.FindByName("MyEnv");
        var release = repository.Releases.FindOne(x => x.ProjectId == "MyProject" && x.Version == "0.0.1");
        
        
        //creating the deployment object
        var deployment = new DeploymentResource
        {
            ReleaseId = release.Id, 
            ProjectId = "MyProject",
            EnvironmentId = environment.Id,
            FormValues = new Dictionary<string, string>
            {
                { "Var1", "102" },
                { "Var2", "200" }
            }
        };

//Deploying the release in Octopus
var result = repository.Deployments.Create(deployment);

Var1 is setup as a prompt variable with Required checkbox unchecked.
default value for Var1 is set to “101”
If I pass value “102” as above, the default value still prevails.

(Dalmiro Grañas) #8

Hi @mahwag,

I dug into this a bit more, and it seems that passing prompted variables using the Octopus.Client required you to jump through a bit more hoops than I remembered.

I wrote the following script which should get you on the right track: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/Octopus.Client/Csharp/Deployments/CreateDeploymentWithPromptVariables.cs

The interesting bit is between lines 28-40

Let me know if that helps :slight_smile:

Dalmiro

(Mahwag) #9

Thanks Dalmiro,
The sample you provided works.

(system) closed #10

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.