Hi, we have a project set up in Octopus which I would like to release/deploy programmatically via #C code using Octopus.Client (the project clears a key from a Redis cache on a server). All works great, apart from the fact that I can not add/edit/update (not sure which) the main variable which is KeyName. I am struggling to understand if I am even on the right approach, I think I am close but could you guide me please as atm when I run this code the release fails in Octopus due to no value set in the KeyName variable, so clearly I am not doing it right:
public static bool ClearCacheKey(Logger auditLog, IConfiguration config, string cacheKey)
{
var octopusURL = config.GetValue<string>("Octopus:ApiURL");
var octopusAPIKey = config.GetValue<string>("Octopus:ApiKey");
var endpoint = new OctopusServerEndpoint(octopusURL, octopusAPIKey);
var repository = new OctopusRepository(endpoint);
//var spaceName = "Spaces-1";
var spaceName = "Default";
var projectName = "Redis.FlushSpecificKeys";
var channelName = "Default"; // Channels are not used in the Redis.FlushSpecificKeys project
var environmentName = "Stage-DATACENTRE";
try
{
// Get the space to work in
var space = repository.Spaces.FindByName(spaceName);
auditLog.Info($"Using Space named {space.Name} with id {space.Id}");
// Create space specific repository
var repositoryForSpace = repository.ForSpace(space);
// Get project by name
var project = repositoryForSpace.Projects.FindByName(projectName);
auditLog.Info($"Using Project named {project.Name} with id {project.Id}");
// Get variables
var variables = repository.VariableSets.Get(project.VariableSetId);
// Get channel by name
var channel = repositoryForSpace.Channels.FindByName(project, channelName);
auditLog.Info($"Using Channel named {channel.Name} with id {channel.Id}");
// Get environment by name
var environment = repositoryForSpace.Environments.FindByName(environmentName);
auditLog.Info($"Using Environment named {environment.Name} with id {environment.Id}");
// Get the deployment process template
auditLog.Info("Fetching deployment process template");
var process = repositoryForSpace.DeploymentProcesses.Get(project.DeploymentProcessId);
var template = repositoryForSpace.DeploymentProcesses.GetTemplate(process, channel);
var release = new Octopus.Client.Model.ReleaseResource
{
ChannelId = channel.Id,
ProjectId = project.Id,
Version = template.NextVersionIncrement,
ReleaseNotes = $"Created automatically by Sentinel for cache key {cacheKey}"
};
// Set the package version to the latest for each package
// If you have channel rules that dictate what versions can be used
// you'll need to account for that by overriding the selected package version
auditLog.Info("Getting action package versions");
foreach (var package in template.Packages)
{
var feed = repositoryForSpace.Feeds.Get(package.FeedId);
var latestPackage = repositoryForSpace.Feeds.GetVersions(feed, new[] { package.PackageId }).FirstOrDefault();
var selectedPackage = new SelectedPackage
{
ActionName = package.ActionName,
Version = latestPackage.Version
};
auditLog.Info($"Using version {latestPackage.Version} for step {package.ActionName} package {package.PackageId}");
release.SelectedPackages.Add(selectedPackage);
}
// # Create release
release = repositoryForSpace.Releases.Create(release, true); // pass in $true if you want to ignore channel rules
// Re-snapshot the variables so we can modify them
release = repository.Releases.SnapshotVariables(release);
var projVarSetId = release.ProjectVariableSetSnapshotId;
//Get the project variable set using the new snapshot id
var projSet = repository.VariableSets.Get(projVarSetId);
//Amend the KeyName var
projSet.Variables.Where(item => item.Name == "KeyName").FirstOrDefault().Value = cacheKey;
projSet.Variables.Where(item => item.Name == "KeyName").FirstOrDefault().Scope.Add(ScopeField.Environment, environmentName);
// # Create deployment
var deployment = new DeploymentResource
{
ReleaseId = release.Id,
EnvironmentId = environment.Id
};
auditLog.Info($"Creating deployment for release {release.Version} of project {projectName} to environment {environmentName}");
deployment = repositoryForSpace.Deployments.Create(deployment);
return true;
}
catch (Exception ex)
{
auditLog.Error(ex.Message);
return false;
}
}