I recently moved all of my inline scripts to script modules library and bash works well, but python scripts throw an error when trying to use a function related to octopus variables:
Traceback (most recent call last):
March 24th 2023 17:22:58
Error
File “/root/.octopus/OctopusServer/Server/Work/20230324162253-193422-966/Bootstrap.f6e-4b7c-933d-4e032305e73a.Script.py”, line 3, in
March 24th 2023 17:22:58
Error
run_path(“/root/.octopus/OctopusServer/Server/Work/20230324162253-193422-966/Script.py”, configuration)
March 24th 2023 17:22:58
Error
File “/usr/lib/python3.8/runpy.py”, line 265, in run_path
March 24th 2023 17:22:58
Error
return _run_module_code(code, init_globals, run_name,
March 24th 2023 17:22:58
Error
File “/usr/lib/python3.8/runpy.py”, line 97, in _run_module_code
March 24th 2023 17:22:58
Error
_run_code(code, mod_globals, init_globals,
March 24th 2023 17:22:58
Error
File “/usr/lib/python3.8/runpy.py”, line 87, in _run_code
March 24th 2023 17:22:58
Error
exec(code, run_globals)
March 24th 2023 17:22:58
Error
File “/root/.octopus/OctopusServer/Server/Work/20230324162253-193422-966/Script.py”, line 3, in
March 24th 2023 17:22:58
Error
locktranslations.lock()
March 24th 2023 17:22:58
Error
File “/root/.octopus/OctopusServer/Server/Work/20230324162253-193422-966/locktranslations.py”, line 78, in lock
March 24th 2023 17:22:58
Error
DB_NAME = get_octopusvariable(f"Octopus.Action[{OCTO_STEP_NAME}].Output.translations.locks.db")
March 24th 2023 17:22:58
Error
NameError: name ‘get_octopusvariable’ is not defined
March 24th 2023 17:22:58
Fatal
The remote script failed with exit code 1
March 24th 2023 17:22:58
Fatal
The action Lock translations on the Octopus Server failed
Thanks for reaching out, and sorry you are running into an issue in using Python Script Modules in Octopus Deploy, but I would be happy to take a closer look at things.
As a first step to investigating this further, would you be able to upload the complete, raw task log for the lines you referenced so I can review it on my side?
Here is a secure link where you can upload this, so you don’t have to post it to the public forum.
I’m also experimenting with this on my side to see if I can replicate the behavior and I will let you know what I find out.
In doing some testing on my side, I replicated this behavior on the latest version of Octopus Deploy.
With this being the case, I will escalate your request to our engineering team for a deeper review, and I will reach back out as soon as I have an update. Our engineering team is based in Australia, so today is their Saturday, and I won’t hear back on this until Sunday/Monday at the earliest.
Sorry you ran into this, and let me know if I can be of any other help in the meantime, but otherwise I will be in touch soon.
Sorry for the multiple posts, but I wanted to let you know I was able to work around this issue by using raw Octostache syntax in my Python Script Module (where test_script_module_var is my project variable name):
def say_hello():
var = #{test_script_module_var}
print(var)
With this syntax in my script module, my deployment process could access the variable during my test deployment.
In discussing this internally with the team, it looks like this behavior is expected, as the get_octopusvariable() function is imported as part of the Bash environment set up during a deployment, which would not apply within the context of a Python execution environment.
Hopefully you can get things going by using the raw Octostache syntax format for your variables, but let me know if you have any trouble, and I would be happy to help further.
Hello @britton.riggs and thanks for the quick reply. Is it possible to use this syntax to get an output from an earlier step? Right now I’m getting an error:
DB_NAME = #{translations.locks.db}
^
March 27th 2023 08:07:10
Error
SyntaxError: invalid syntax
I uploaded the full log with the link you sent earlier.
Thank you for following up! I’ll jump in for Britton briefly as he’s currently offline as part of our US-based team.
Assuming this variable was created as an output variable in the previous step, you can reference it in subsequent steps with the syntax #{Octopus.Action[PreviousStepName].Output.VariableName}.
I hope that helps! Let us know how you go or if you have any further questions.
Hello @jeremy.miller using the get_octopusvariable method is the original problem and the reason I created this thread on the forum. It doesn’t work when used in python Octopus script modules.
I’m stepping back in on this, but I’m happy to continue helping! Yes, you are correct that the get_octopusvariable() syntax won’t work here, for the reasons I mentioned on Friday.
In looking through things this morning, I see that you are trying to set an output variable in one step and then access this via a script module in another step.
In looking over my tests from Friday, the raw Octostache syntax (#{}) should work here -
Setting my output variable in a previous step (named runascript):
set_octopusvariable "TestResult" "Passed"
My script module:
def say_hello():
var = "#{Octopus.Action[runascript].Output.TestResult}"
print(var)
I was wondering what would happen if this variable wasn’t able to be evaluated, so I also tested that scenario and got the following output when not using double quotes around my variable (i.e., var = #{Octopus.Action[runascript-nonexist].Output.TestResult}):
This output seems to align with what I saw in your task log - do you know if the variable in question is being evaluated to an actual value in your process? From the task log, it doesn’t seem like the variable is being populated as expected, but you might try adding some echo/print statements in parts of your process to make sure that things look ok at each step (i.e., when you set the output variable, when you are pulling it into your script module).
I hope this additional information helps, but let me know what you find out.