Add Single Project Variable via Powershell

usability
(Pgruidl) #1

On one of the steps of my deployment, I am creating a hash value of a file. Upon successful deployment, I’d like to inject that hash value as a Project Variable (one that persists) so I can compare against it on future deployments. This would allow the deployment to possibly skip these steps if the file hashes have not changed.

Is there any way to just inject one single variable to a project? Potentially using the Octopus API? Thanks.

(Shawn Sesna) #2

Hello there! This blog post shows how to use the API to add variables to a project. Be advised that using the method you proposed would show a warning that something has changed the next time that release is deployed as the Variable collection would have been changed.

(Pgruidl) #4

This helped so much, thank you!

Now, one follow up question. This is great for creating a variable that isnt there… How would I use the same script to just replace the value of an existing variable? I can’t seem to get that to work…

Thank you!

(Shawn Sesna) #5

Hello there! Sorry to hear that you are having difficulties replacing an existing value. I ran a quick test and was able to use pretty much the same code. I chose a single variable from the list and was able to update the value. The only change was the for loop, all function calls were unchanged.

# Get reference to project
$octopusProject = Get-OctopusProject -OctopusServerUrl "https://shawnsesna.octopusdemos.app" -ApiKey "API-XXXXXXXXXXXXXXXXXXXXXX" -ProjectName "SSIS Example"

# Get list of existing variables
$octopusProjectVariables = Get-OctopusProjectVariables -OctopusDeployProject $octopusProject -OctopusServerUrl "https://shawnsesna.octopusdemos.app" -ApiKey "API-XXXXXXXXXXXXXXXXXXXXX"


foreach ($octoVariable in $octopusProjectVariables.Variables)
{
    if ($octoVariable.Name -eq "CM.sql2016.AdventureWorks2017.sa.UserName")
    {
        $octoVariable.Value = "shawn.sesna"
    }
}
# Update the project 
Update-ProjectVariables -ProjectVariables $octopusProjectVariables -ApiKey "API-XXXXXXXXXXXXXXXXXXXXXXX" -OctopusServerUrl "https://shawnsesna.octopusdemos.app" -VariablesId $octopusProjectVariables.Id
(system) closed #6

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