Get output variable from deployment using C# client

usability
server
(Denis Volovenko) #1

Hi guys, I have a deployment that consists from one small script, that fetches some IIS properties of the site associated with my tenant.
And I want to return fetched info from this deployment to my C# client, which I use to start the deployment.

So I have C# client and there I prepare a deployment, get its task and wait for it to finish

var deploymentCreated = _repository.Deployments.Create(deployment);
var task = _repository.Tasks.Get(deploymentCreated.TaskId);
_repository.Tasks.WaitForCompletion(task);
var result = _repository.Tasks.GetDetails(task);

I created a variable called “result” in project, but I can’t see it’s value anywhere in my result object or task object.
My best guess was

var resultVariable = _repository.VariableSets.Get(deploymentCreated.ManifestVariableSetId).Variables.FirstOrDefault(v => v.Name == "result");

but it’s Value is null.

And here is how I am setting the variable value in the Octopus powershell script:

Set-OctopusVariable -name "result" -value "testresult"

Please help me, I’m in despair :slight_smile:

(Dalmiro Grañas) #3

Hi @Denis_Volovenko,

Thanks for reaching out! I have a couple of points to mention that might help you get to the bottom of this:

  1. Variables created via Set-OctopusVariable are called Output Variables. These variables are only available in steps that come AFTER the step where they were created on. So if you are creating them in step 1, they’ll be available from step 2 onwards.

  2. To fetch a variable value from a C# script you need to use the syntax shown in this example: https://octopus.com/docs/deployment-examples/custom-scripts/output-variables#using-the-variable-in-another-step ( make sure to click on the c# tab)

  3. If you want to know if your variable is available in a specific step during your deployment, you can follow this other doc. This way you’ll print all the variables and their values at each point of the deployment.

If after those steps you still find yourself in a struggle, please send me the log file from step (3) so I can take a look at it. If it contains sensitive info feel free to email it to support@Octopus.com.

Cheers!
Dalmiro

(Denis Volovenko) #4

Hi @Dalmiro, thanks for your reply.
You misunderstood what I need.
I do not use C# script in deployment step, I use C# client library. This one: https://github.com/OctopusDeploy/OctopusClients

And I need to fetch this variable using this client AFTER deploy is completed. Do you know any ways to do this?

(Denis Volovenko) #5

I figures it out myself, so here is my precious piece of code for anyone who will ever google such question:

var pages = _repository.VariableSets.Get(deploymentCreated.ManifestVariableSetId).Variables.FirstOrDefault(v => v.Name.Contains("Output.result"));
1 Like
(Dalmiro Grañas) #6

Hi @Denis_Volovenko

Sorry I misundestood your initial reply! I’m glad to hear you figured it out yourself. Thanks for taking the time to come back here and posting your solution to the community!

Cheers