Octopus.Client API - Step Scope not working


I’m trying to add a variable to my project using the Octopus Client (ver 3.3.17 & 3.3.20) on Octopus (ver 3.3.17). I can see the variable being added, but can’t see the scope. The code is as follows:

class X
private const string SERVER = “http://octopus.mycompany.com”;

    public void AddVar()
        var repository = new OctopusRepository(
            new OctopusServerEndpoint(SERVER, VARIABLE_API_KEY));

        // find the project
        var project = repository.Projects.FindByName("MyProject");

        // get deployment process and steps 
        var id = project.DeploymentProcessId;
        var deploymentProcess = repository.DeploymentProcesses.Get(id);
        var steps = deploymentProcess.Steps;

        //get get project level variable set
        var defaultVariableSet = repository.VariableSets.Get(project.Link("Variables"));

        var res = new VariableResource();
        res.Name = "TEST123";
        res.Value = "vale";

        // add the scoped variable and save
        res.Scope.Add(ScopeField.Action, new ScopeValue(steps[3].Name));

please HELP. Urgent

Hi Suraj,

Thanks for getting in touch! I’ve tried out the code snippet you sent through and it doesn’t work for me either, and I think I know what is wrong. In order to scope a variable to an Action you need to use the Action.Id not the Step.Id. Steps are just a way to group Actions together so you can configure rolling deployments - they aren’t something you can scope variables to.

I would suggest changing this line: res.Scope.Add(ScopeField.Action, new ScopeValue(steps[3].Name));

Make is something more like this:

// Find the action and scope the variable if the action is found
var action = deploymentProcess.Steps.SelectMany(s => s.Actions).FirstOrDefault(a => a.Name.Contains("Awesome Action"));
if (action != null)
	res.Scope.Add(ScopeField.Action, action.Id);

Hope that helps!

Hi Michael,

That seems to work. Thank you.

Hi Michael,

Thank you for your help. That worked.

Hi Suraj,

Thanks for getting back to me, and I’m really glad it worked for you!

Happy Deployments!