I am trying to update the value as #{empty} to octopus variable using api in octopus, but value alway repacing with empty value , but when I ru
n from my laptop its working as expected
I am trying to update the value as #{empty} to octopus variable using api in octopus, but value alway repacing with empty value , but when I ru
Thanks for reaching out to Octopus Support!
When it comes to making changes to variables using the user interface of Octopus, these requests are handled via API by your browser.
This means that any change that you make to the variables page of a project in your browser should be possible via REST API request, also.
If you open developer tools inside your browser and make the changes to the variables page with your network tab open, you can view the request that is being made and see the syntax Octopus is using to make these changes.
I hope this helps! Please let me know if you’re still not sure or running into issues and I’ll do my best to assist how I can.
Kind Regards,
Adam
Hello , Both the scenario I am using api only , when I from labtop the values gets updated , but from octopus run script step its replacing empty value
is it possible I can update “#{empty}” value to variable from octopus run script step
Hey @YuvarajDuraisamy,
Thanks for the extra information.
Would you mind sharing the process JSON you’re using for the project with the run a script step, please?
You can share it privately via our support files page here: Support Files | YuvarajDuraisamy.
We do also have an example script to add/edit variables that should work for what you’re looking to do if you’d like to use it as a reference or compare what you have: OctopusDeploy-Api/AddOrEditVariablesWithScoping.ps1 at master · OctopusDeploy/OctopusDeploy-Api · GitHub.
Kind Regards,
Adam
I have uploaded the code which running in the octopus as run script step
same code updating #{empty} to the variable when run from my machine but not from octopus run script step
Thanks for following up! I’ll jump in for Adam as he’s currently offline as part of our UK-based team.
I think I understand the cause of this difference in behavior from when you’re running this script in a script step vs when you run it from outside of Octopus, but please correct me if I’m mistaken in any way. When the script step runs during an executing deployment, it’s invoking Octostache, which recognizes this #{}
as variable substitution syntax. My guess is this value is being recognized as a variable name, substituting its value in its place (in this case, an empty string), then when the API script is executing it’s using that evaluated value in its place.
If this doesn’t seem to apply to your case, would you be willing to turn on variable debugging in your project, then create and deploy a new release and send us the resulting task log (steps outlined in this doc page)? These debugging variables write all variables before and after evaluation to the task log, and this should hopefully point us in the right direction towards what’s causing this.
I look forward to hearing back!
Best regards,
Kenny
function Add_Values {
param (
[CmdletBinding()]
$variable_name,
$value
)
$variable = @{
Name = $variable_name
Value = $empty_value
Type = "String"
IsSensitive = $false}
return $variable
}
$var_setname = 'test_lib'
$check_formats = "*value1,*value2"
$flag = ""
$header = @{ "X-Octopus-ApiKey" = $API_Key_Common }
$variable_setId = ((Invoke-RestMethod -Method Get -Uri "$Octopus_Url/api/$Space_ID_eLIMS_NG/libraryvariablesets/all" -Headers $header) | Where-Object { $_.Name -eq $var_setname }).Links.Variables
if ($null -eq $variable_setId) {
Write-Warning "Library variable set not found with name '$var_setname'."
exit
}
#Getallvariables
$variable_setvariables = Invoke-RestMethod -Method Get -Uri "$OctoBaseURL$($variable_setId)" -Headers $header
$empty_value = '#{empty}'
foreach($format in $check_formats.Split(',')){
variables_details = $variable_setvariables.Variables | Where-Object { $_.Name -like $format }
$variables_details = $variables_details | Select-Object -Unique -Property Name
foreach ($variables_detail in $variables_details) {
$variable = $variable_setvariables.Variables | Where-Object { $_.Name -eq $variables_detail.Name }
if(($variable.Name -like '*value1') -or ($variable.Name-like '*value2') -and ($empty_value -notin $variable.Value))
{
$flag = "update"
$variable_name = $variable.Name | Select-Object -Unique
$newvariable = Add_Values -variable_name $variable_name -value $empty_value
write-host "Varaiable--> $newvariable.Value"
$variable_setvariables.Variables += $newvariable
}
}
}
if($flag -eq "update")
{
write-host "variable_setvariables ---> $variable_setvariables.Variables"
Invoke-RestMethod -Method Put -Uri "$OctoBaseURL$($variable_setId)" -Headers $header -Body ($variable_setvariables | ConvertTo-Json -Depth 10)
}
this is my code , its updating empty string in the library when run from octopus ,if ran the same from local machine updating the value as expected
could you please try from your side
Thank you for getting back to us.
If you have tried the script run from Octopus inside a Project or Runbook, are you able to try running it in the Script Console? This may help isolate variable evaluation being the issue.
Also, are you able to share a raw task log with variable logging enabled? This will help to confirm if #{empty}
may be evaluating to a blank value before the script actually runs.
I have created a secure upload link you may use to share the log file(s).
Let us know regarding the above once you get a chance.
Best Regards,
Donny Bell
when tried from script console , it works as expected
but from project run book again empty value is updated
I have shared run book and script console raw logs
I attempted to test this with the script you provided but kept hitting the error:
ObjectNotFound: The term 'variables_details' is not recognized as a name of a cmdlet, function, script file, or executable program.
UPDATE: There was a $
missing in the script you copied. I fixed this and ran it successfully. It inserted the #{empty}
value correctly
e.g.
I then tested using a runbook to run our sample “Update Library Set Variable” script and this inserts the value as expected.
e.g.
This suggests that either there is a bug within the version of Octopus you are running or you have another variable within your project or library set that is overwriting the #{empty}
value into a blank one.
To narrow down where the problem is could you try running this sample script within your runbook to see if it succeeds for you too?
I’ve set the values of the octopus URL, API key and space ID to match the variables you used in your script, so you should be able to run it as is.
$ErrorActionPreference = "Stop";
# Define working variables
$octopusURL = $OctoBaseURL
$octopusAPIKey = $API_Key_Common
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
# Specify the Space to search in
$spaceId = $Space_ID_eLIMS_NG
# Library Variable Set
$libraryVariableSetName = "test_lib"
# Variable name to search for
$VariableName = "Value1"
# New variable value to set
$VariableValue = "#{empty}"
Write-Host "Looking for library variable set '$libraryVariableSet'"
$LibraryvariableSets = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$spaceId/libraryvariablesets?contentType=Variables" -Headers $header)
$LibraryVariableSet = $LibraryVariableSets.Items | Where-Object { $_.Name -eq $libraryVariableSetName }
if ($null -eq $libraryVariableSet) {
Write-Warning "Library variable set not found with name '$libraryVariableSetName'."
exit
}
$LibraryVariableSetVariables = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$spaceId/variables/$($LibraryVariableSet.VariableSetId)" -Headers $Header)
for($i=0; $i -lt $LibraryVariableSetVariables.Variables.Length; $i++) {
$existingVariable = $LibraryVariableSetVariables.Variables[$i];
if($existingVariable.Name -eq $VariableName) {
Write-Host "Found existing variable, updating its value"
$existingVariable.Value = $VariableValue
}
}
$existingVariable = $LibraryVariableSetVariables.Variables | Where-Object {$_.name -eq $VariableName} | Select-Object -First 1
$UpdatedLibraryVariableSet = Invoke-RestMethod -Method Put -Uri "$OctopusURL/api/$spaceId/variables/$($LibraryVariableSetVariables.Id)" -Headers $Header -Body ($LibraryVariableSetVariables | ConvertTo-Json -Depth 10)
Regards,
Paul
Thanks Team ,I have cross verified there is empty variable were declared in the some other variable set , it causes the issue
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.