Variables and Variable sets

Hello Octopus Community.
I am wondering if anyone here has come across this scenario before, and found a solution for it. This involves two projects one of which a variable is dependent on setting a variables value from a different projects (name,environment,release number) for a path. This is to search and replace application.json for a web application.

I was given these two links from Support, but I havent the first clue how to integrate an api call in the variable sets of a project.

Examples: https://github.com/OctopusDeploy/OctopusDeploy-Api
API Wiki: https://github.com/OctopusDeploy/OctopusDeploy-Api/wiki
Example of what you want to do: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Releases/GetLastSuccessfulForProjectAndEnvironment.ps1

Does anyone have an example?

Thanks in advance.

Hi ggiacalone,

If you ever get a response from us in support that you need further explanation or help with, please don’t hesitate to reply and ask more questions, that’s what we’re here for!

I can get you started with your API calls. I will give you an example of a script with API calls to get another project’s last successful release version.

These instructions are assuming you are needing a static project and environment. If the script needs to dynamically pick a project and environment that would be more work.

First on your server you would want to go to these web pages to find the Space Id, Project Id and the Environment ID.

http://YOUR_OCTOPUS_URL/api/Spaces
Find the Space # where your project is and use it in the following URLs.

http://YOUR_OCTOPUS_URL/api/Spaces-#/projects (Change the # to the space you found in the first URL)
Find the project name, then scroll up to find the Id. It will be something like “Projects-2”.

http://YOUR_OCTOPUS_URL/api/Spaces-#/environments (Change the # to the space you found in the first URL)
Find the environment name, then grab the environment ID. It will be something like “Environments-1”.

Once you have these two IDs, you can insert the following code into your PowerShell script step. I have put comments in to try to explain the important lines.

##CONFIG for later API calls.
$OctopusURL = "http://YOUR_OCTOPUS_URL" #Octopus URL (change it to your octopus location)
$OctopusAPIKey = "" #Octopus API Key (for info getting an API key, go to (https://octopus.com/docs/octopus-rest-api/how-to-create-an-api-key)

##PROCESS##
$header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } #This is used for API Calls to pass the API Key
$ProjectID = "Projects-2"  #You got this variable earlier
$EnvironmentID = "Environments-1" #You got this variable earlier

#This is an API call getting the releases of the project we have identified.
$ProjectDashboardReleases = (Invoke-WebRequest $OctopusURL/api/progression/$ProjectID -UseBasicParsing -Method Get -Headers $header).content | ConvertFrom-Json

#This is us setting a variable to the last successful release
$LastSuccessfullRelease = $ProjectDashboardReleases.Releases.Deployments.$EnvironmentId | ?{$_.state -eq "Success"} | select -First 1

#This is the variable call that will give us the latest successful release version.
$LastSuccessfullRelease.ReleaseVersion

You can then use the last variable in your script to do what you need to do. Hopefully this is enough to get you going. If you need more help please reach out.

Thank you Jeremy I am on board with using these API’s. I am not stranger to them, however where I need these values are in your applications variable sets, to be used to search and replace key value pair in application.json files. The variable set is a project, and that project is needing some values from another project on that server. I am no stranger to ansible or python, but unfortunately powershell is not my forte. Are you suggesting to run a script separate from the variable sets in that project using these api’s to set this value?

Hi ggiacalone,

I think the best way to achieve this is by using PowerShell to modify/retrieve values from another project. I have written two scripts in the past for someone else to do something similar. The first script will allow you to modify/add variables to a project’s variable set. The second one will allow you to retrieve a value from a variable set in a project. You will only need to put in a few pieces of information and then call the function in the desired way. I have examples in both scripts on how to call the functions. Please let me know if you have any questions, or if I misunderstood what you were trying to accomplish. As always with scripts like these, please read them carefully and do testing in a dummy project to make sure you are getting your desired results.

ModifyOrAddVariableToProject.ps1

GetVariableInProject.ps1