Hello Rohit,
After discussing with the team, There is a way to use output variables here that would be beneficial for your use case. This involves setting your change number as a variable, then using the Octopus API to retrieve it later.
For example, you can run a script and use the following to set your change number as an output variable:
Step 1: Set Output Variable
Set-OctopusVariable -name "ChangeNumber" -value $someChangeNumber
Then, you can use the following API example to retrieve that value using the step name and some additional available data:
Step 2: Retrieve Output Variable
# Create an API key using a sensitive variable
$octopusAPIKey = "YourAPIKey"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$octopusURL = $OctopusParameters["Octopus.Web.ServerUri"]
$spaceId = $OctopusParameters["Octopus.Space.Id"]
$releaseId = $OctopusParameters["Octopus.Release.Id"]
$projectId = $OctopusParameters["Octopus.Project.Id"]
# Get last 100 deployments for the release
$deployments = Invoke-RestMethod -Uri "$octopusURL/api/$($spaceId)/releases/$($releaseId)/deployments?skip=0&take=100" -Headers $header
#Select most recently created
$deployment = $deployments.Items | Sort-Object {$_.Created} | Select-Object -First 1
Write-Verbose "Deployment Id: $($deployment.Id)"
# Get variables
$deploymentVariablesUrl="$octopusURL$($deployment.Links.Variables)"
$deploymentVariables = Invoke-RestMethod -Uri "$deploymentVariablesUrl" -Headers $header
# Select matching output variable
$changeNumber = $deploymentVariables.Variables | Where-Object {$_.Name -eq "Octopus.Action[Set Output Variable].Output.ChangeNumber"} | Select-Object -First 1
if($null -eq $changeNumber) {
Write-Host "Couldn't find existing change number"
}
else {
Write-Host "Found existing change number, value=$($changeNumber.Value)"
}
In the example above, the step where the change number output variable was set was called Set Output Variable
, which you can see referenced in the variable matching script (Where-Object {$_.Name -eq "Octopus.Action[Set Output Variable].Output.ChangeNumber"}
)
The benefit of this approach is that the value is available in the following deployment environment - I did local testing with this script, and on first run, it outputs Couldn't find existing change number
(since it hasn’t saved yet), but it correctly pulls up on subsequent deployments to other environments.
The biggest change here is that ChangeNumber
isn’t a project variable, it’s specifically an output variable that your process is using. If you need to be able to keep a project variable for this value, this may not be the best way to handle things.
There’s another option to handle this using deployment artifacts - if you’re interested in discussing more or the output variables above don’t quite meet your needs, let me know and we can walk through how it would work using artifacts. It’s reasonably similar, just a little different on how it’s handled and the necessary API calls to retrieve it!
Let me know if you have any questions or need any additional help with this, hope this gets you moving in the right direction.