OctopusDeploy / Issues

| Public | Bug reports and known issues for Octopus Deploy and all related tools
https://octopus.com
162 stars 20 forks source link

Python Script Modules not Recognizing Output Variable Syntax (set_octopusvariable) #8332

Closed briggs-octo closed 1 year ago

briggs-octo commented 1 year ago

Severity

1 customer report

Version

2023.3.12795

Latest Version

I could reproduce the problem in the latest build

What happened?

An error is thrown when attempting to use the Octopus output variable syntax within a Python script module:

image (12)

Code:

set_octopusvariable("TestResult", "Passed")

Error:

17:01:40 Error | NameError: name 'set_octopusvariable' is not defined

This same code works fine directly inline, as Python code in the process editor.

Reproduction

  1. Set up a Python script module containing the following code:
def test():
  set_octopusvariable("TestResult", "Passed")
  1. Reference this script module from within a step.
  2. Deploy a new release.

Error and Stacktrace

17:01:40   Error    |       Traceback (most recent call last):
17:01:40   Error    |       File "/home/Octopus/Work/ServerTasks-2315008-8c808464-4b54-4e2f-9f58-e722819388c3/Bootstrap.aa3-4cbb-a97c-8ffd3b8cb35b.Script.py", line 3, in <module>
17:01:40   Error    |       run_path("/home/Octopus/Work/ServerTasks-2315008-8c808464-4b54-4e2f-9f58-e722819388c3/Script.py", configuration)
17:01:40   Error    |       File "/usr/lib/python3.10/runpy.py", line 289, in run_path
17:01:40   Error    |       return _run_module_code(code, init_globals, run_name,
17:01:40   Error    |       File "/usr/lib/python3.10/runpy.py", line 96, in _run_module_code
17:01:40   Error    |       _run_code(code, mod_globals, init_globals,
17:01:40   Error    |       File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
17:01:40   Error    |       exec(code, run_globals)
17:01:40   Error    |       File "/home/Octopus/Work/ServerTasks-2315008-8c808464-4b54-4e2f-9f58-e722819388c3/Script.py", line 3, in <module>
17:01:40   Error    |       PythonScriptModuleTest.test()
17:01:40   Error    |       File "/home/Octopus/Work/ServerTasks-2315008-8c808464-4b54-4e2f-9f58-e722819388c3/PythonScriptModuleTest.py", line 2, in test
17:01:40   Error    |       set_octopusvariable("TestResult", "Passed")
17:01:40   Error    |       NameError: name 'set_octopusvariable' is not defined
17:01:40   Verbose  |       Process /bin/bash in /home/Octopus/Work/ServerTasks-2315008-8c808464-4b54-4e2f-9f58-e722819388c3 exited with code 1
17:01:40   Verbose  |       Updating manifest with output variables
17:01:40   Verbose  |       Updating manifest with action evaluated variables
17:01:40   Verbose  |       Updating manifest with output variables
17:01:40   Verbose  |       Updating manifest with action evaluated variables
17:01:40   Fatal    |       The remote script failed with exit code 1
17:01:40   Fatal    |       The action Run a Script on a Worker failed


### More Information

_No response_

### Workaround

You can use the inline editor to set output variables in your process.
briggs-octo commented 1 year ago

Per discussion:

Octopus variables and functions are only available from the scope of the bootstrap script. We think this is a good design because it separates modules from Octopus implementation.

Instead, pass Octopus functions to the modules like so:

Module SetVariable:

def say_hello(set_octopusvariable):
    set_octopusvariable("Hello", "Octopus")
Script:

import SetVariable
SetVariable.say_hello(set_octopusvariable)
print(get_octopusvariable("Hello"))