Octo Client and Project Template Variables

How do you access the Project Template Variables via the Octo C# Client?

How can I add/modify project template vars through the Octo Client?

Hi Colin,

Project template variables are exposed by the Templates collection on the ProjectResource. There is also a convenience method on the ProjectResource named AddOrUpdateVariableTemplate. For example:

var project = repository.Projects.Get("Projects-1");
project.AddOrUpdateVariableTemplate("TestVariable", "Label", ControlType.AsDisplaySettings(ControlType.SingleLineText));
repository.Projects.Modify(project);

I hope that helps! Please let me know if I can assist further.

Regards,
Jayden

Can you run this from a project step somehow? Maybe as powershell? Is the Octo client available during to deployment steps that run on the Octo Server?

Colin,

Certainly! The client isn’t automatically available, but it is quite straightforward to reference it.

Firstly, go to Library, External Feeds and add a new feed for the public Nuget server, with a URL of https://api.nuget.org/v3/index.json. Feel free to enable the extended API.

After that, create a script step in your project, and in the Referenced Packages section add a reference to the Octopus.Client package in the new external feed you created.

Finally, in the Inline Source Code section, select C# and provide your script. You need to add references and the like, so I’ve included an example below based on our previous discussion.

#r "Octopus.Client\lib\net45\Octopus.Client.dll"
  
using Octopus.Client;
using Octopus.Client.Model;
  
var repository = new OctopusRepository(new OctopusServerEndpoint("[ServerAddress]", "[ApiKey]"));
var project = repository.Projects.Get("Projects-1");
project.AddOrUpdateVariableTemplate("TestVariable", "Label", ControlType.AsDisplaySettings(ControlType.SingleLineText));
repository.Projects.Modify(project);

I’ve tested this locally and it all works. Let me know how that goes!

Regards,
Jayden

Thank you. I will give that a try and let you know.

I’m just cucurious… what’s the runtime behind the scenes being used to allow the C# script in the Inline source code sections?

Colin Pear

Clear Measure

Colin,

We use ScriptCS - http://scriptcs.net/

Regards,
Jayden

Will the solution we have discussed for making the OctoClient available to the process steps work for offline package drops?

Hi Colin

My name is Eddy, I am now looking after the thread as Jayden has left.

Sorry for the delay, saw you just withdraw a post, is there anything we can help or you are good.

Regards
Eddy

Yes. Jayden answered my question above about accessing the Octopus.Client via a script step.

Is it possible to make use of the Octopus.Client when you do an offline package drop for deployment to a machine that is not connected to the internet?

Right now what I am doing is I have a script step that makes use of the Octopus.Client to expose Project Templates from other projects as output variables.

The prototype seems to be mostly working but its only in a normal online setting. I would need this to work in offline package drops as well.

I decided to test this and it looks like trying to run the Octopus.Client in an offline package drop does not seem to work.

However in my case it appears I am able run a script step that uses Octopus.Client with the execution location Runs on the Octopus Server and then exposes the results of that endeavor as output variables.

This works because of the recent addition that allows output variables to work in offline package drop mode.

Hi Colin

Unfortunately, for a script that runs on server and set some output variables, those output variables do not get exported to the variable file in offline drop package. If you let that script step run on target, as your target has no network access, it cannot use Octopus.Client to get data from octopus server. So both ways do not work.

I have tried many other different ways and come up with the following work around
Option 1 (prefered):

  • Setup a parent project ( a new project)
  • Add a script step that run on server which use Octopus.Client to get the Project Template data and set it as a output variable
  • Add a Deploy a Release step that point to your current project that deploy the offline drop and use the variables feature in this step to pass the value to the child project, see my sample setup in the screenshot
  • By doing that, a new variable Thing is added and will be exported to the variable file in the offline drop package and it will work as a normal variable
  • You can find more information about how Deploy a Release step work from this link https://octopus.com/docs/deployment-process/projects/coordinating-multiple-projects/deploy-release-step

Option 2:

  • Use a script step that gets Project Template by using Octopus.Client and dump it into the offline drop output folder
  • Another script that runs on target to read the file output from the step above
  • Do whatever need to be done

I hope this helps

Let me know what you think and how you go

Regards
Eddy

Eddy, what version of Octopus Deploy server are you testing against? As of 2018.3.8 output variables for offline package drop are supported.

I have a test server instance setup with a working demo of it. I would be glad to walk you through it if you like.

Hi Colin

I was using 2018.7.x. As you mentioned output variables for offline package drop are supported from 2018.3.8, however output variables generated from the server does not get pass down to the variable json file in the offline drop package, which means while running the deployment on target, any step run on target cannot get the output variable value generated from the server.

Happy to schedule a support call, could you please pick a time that suits from our schedule system

Regards
Eddy

Hmmm. I hear what you are saying. So I went back to double check and it does indeed appear that output variables produced from a step running on the octopus server do show up in the JSON file.

Here are some screen shots of my test Octo server setup:

Hi Colin

Thanks for sending through the screenshots. I just double checked the output variables and indeed they get written to the variable file and the subsequence step runs on target can reference and use it.

So a step runs on server to generate the output variables would work for your scenario. Apologise for the misleading, i could be using a wrong version when i was testing.

Let me know if you still need the support call section.

Regards
Eddy

No problem. I’m just glad I wasnt missing something. Thanks for verifying the issue.

We can cancel the support call.

Thanks again.