Resolution
set_octopusvariable only exists in a shell session that Octopus has prepared for you. It isn't available in two common situations:
-
The project has raw scripting turned on. When the project variable
OctopusUseRawScriptisTrue, Octopus runs your script exactly as written and doesn't load its helper functions. This applies to steps that run on a worker too, not only to SSH targets. If you've moved your steps to a worker, removeOctopusUseRawScriptfrom the project variables. If you need to stay on a direct SSH target with raw scripting, defineset_octopusvariableyourself at the top of the script. -
The call is inside a second
sshcommand that your script opens. That remote shell doesn't have the helper functions. Have the remote command print the value, capture it on the machine Octopus is running your script on, and callset_octopusvariablethere.
If you've already moved set_octopusvariable outside the ssh command and still see the error, check for OctopusUseRawScript first.
Symptom
A script step runs against an SSH deployment target, such as a Linux server, or runs on a worker and connects to that machine over SSH itself. The script calls set_octopusvariable to create an output variable, but a later step that reads #{Octopus.Action[Step Name].Output.VariableName} gets an empty value or the unreplaced #{...} text.
The rest of the script runs normally, and the step often finishes successfully. In the task log for the step that sets the variable, you see a "not found" line from the shell. The exact wording depends on the operating system:
set_octopusvariable: FSUM7351 not found
bash: set_octopusvariable: command not found
sh: 1: set_octopusvariable: not found
Scope: when this is the right article
This article applies when:
- The step runs a bash script against an SSH target, either directly or from a worker that opens an SSH connection itself.
-
set_octopusvariable(orget_octopusvariable,new_octopusartifact, orfail_step) fails with a "not found" error. - Other commands in the same script work, and Octopus variables such as
#{Octopus.Deployment.Id}are replaced correctly in the script text.
This article doesn't apply if:
-
set_octopusvariableruns without an error, but the later step still can't read the value. That's usually a step name or syntax problem in the variable expression. See the output variables documentation. - The step fails before your script starts, for example with an SSH authentication or connection error.
Cause
Octopus creates output variables by reading a specially formatted line from your script's output. set_octopusvariable is a small shell function that prints that line for you. Octopus normally loads this function, along with the other helper functions, into the session before your script starts.
There are two ways your script can end up where that function doesn't exist.
Raw scripting is turned on
Some machines where Octopus can't run its usual tooling, are deployed to with raw scripting. You turn it on by setting the project variable OctopusUseRawScript to True. In this mode, Octopus runs your script exactly as written, with nothing added.
Octopus still replaces #{...} variable expressions in the script text before it runs, which is why those keep working. The helper functions aren't loaded, so set_octopusvariable isn't a command the shell recognizes.
Because OctopusUseRawScript is a project variable, it applies to every deployment process step. A common way to hit this is to start with raw scripting directly, then later move the steps to use a worker that connects over SSH, and leave the variable in place. The worker then also runs your script without the helper functions, so set_octopusvariable fails even when you call it outside the ssh command.
A second SSH session inside your script
When your script runs ssh user@other-host "...", or ssh user@other-host << EOF ... EOF, the commands inside it run in a brand new shell on the other machine. That shell knows nothing about Octopus, so the helper functions aren't there either. This often happens when a worker connects to a mainframe or other system on your behalf.
Common misunderstandings
In both cases, Octopus and the SSH target are working as expected. The helper functions only exist in a session Octopus has prepared, and they don't follow you into sessions you open yourself.
These misunderstandings often make it look like an SSH target bug:
-
"The step succeeded, so the variable must be set." A "not found" error doesn't stop the script unless it's the last command or you use
set -e. -
"
#{...}variables work, so the helper functions should too." Octopus replaces#{...}in the script text before the script runs, in every mode. Helper functions are shell commands that only exist when Octopus loads them. -
"I moved the step to a worker, so raw scripting no longer applies."
OctopusUseRawScriptis a project variable, not a target setting. It keeps applying until you remove it or scope it away from the worker steps. -
"Everything in my script runs on the target." Anything inside
ssh other-host "...", or between<< EOFandEOF, runs on the other machine.
Diagnosis and fix
Find out which case you have
-
Find the error in the task log. Open the deployment, select Task Log, and expand the step that should set the variable. Search for
set_octopusvariableandnot found. -
Check whether the project uses raw scripting. Open the project's Variables (and any variable sets it includes) and look for a variable named
OctopusUseRawScriptset toTruethat's in scope for the step and environment that failed. Check this even if the step runs on a worker. If it's set, go to Fix: raw scripting is turned on. -
Check the script for a second SSH session. Look for
ssh,sshpass, or a (<< EOF) that wraps theset_octopusvariableline. If you find one, go to Fix: set_octopusvariable inside a second SSH session. -
If you aren't sure where a line runs, print the host name. Add these lines to the script and redeploy:
echo "Step script running on: $(hostname)"
ssh user@other-host "echo \"Remote command running on: \$(hostname)\""
The first line shows the machine Octopus runs your script on. Any set_octopusvariable call that runs somewhere else won't work.
Fix: raw scripting is turned on
Choose one of these options.
Option A: run the step on a worker without raw scripting.
- Run the script step on a worker instead of directly on the SSH target, and have the script connect to the machine over SSH.
- In the project's Variables, delete
OctopusUseRawScript. If other steps in the project still deploy directly to an SSH target with raw scripting, scope the variable to only those steps instead of deleting it. - If you changed project variables, create a new release (or update the release's variable snapshot) before you deploy again. Runbooks pick up variable changes when you publish a new snapshot.
Octopus then loads the helper functions on the worker, so set_octopusvariable and get_octopusvariable work as documented. Keep the set_octopusvariable calls in the worker's script, outside any ssh command. See Fix: set_octopusvariable inside a second SSH session for how to bring values back from the remote machine.
This option suits processes that pass several values between steps, because you don't need to add extra code to every step.
Option B: define the function in your script. If you need to stay on the direct SSH target with raw scripting, add this function near the top of each script that sets output variables, then call it as normal:
encode_servicemessagevalue() {
echo "$1" | openssl enc -base64 -A
}
set_octopusvariable() {
MESSAGE="##octopus[setVariable name='$(encode_servicemessagevalue "$1")' value='$(encode_servicemessagevalue "$2")'"
if [ "${3:-}" = "-sensitive" ]; then
MESSAGE="$MESSAGE sensitive='$(encode_servicemessagevalue "True")'"
fi
echo "$MESSAGE]"
}
set_octopusvariable "WorkingDir" "/u/myuser/octopus/#{Octopus.Deployment.Id}"
The function needs openssl on the target's PATH. If openssl is installed somewhere else, add its folder to PATH at the top of the script, for example export PATH=$PATH:/path/to/openssl/bin.
In later steps, read the value with the #{Octopus.Action[Step Name].Output.WorkingDir} expression. Octopus replaces it in the script text before the script runs. get_octopusvariable isn't available in raw scripting, so don't rely on it there.
Fix: set_octopusvariable inside a second SSH session
Move the set_octopusvariable call out of the remote command. Have the remote command print the value, capture it on the machine running your step, and set the variable there.
For a single value:
REMOTE_VALUE=$(ssh user@other-host "cat /opt/app/version.txt")
set_octopusvariable "AppVersion" "$REMOTE_VALUE"
For several values from a longer remote script, save the remote output and pick out the lines you need:
ssh user@other-host 'bash -s' < script.sh > output.log 2>&1
workingDir=$(grep 'WorkingDir:' output.log | awk -F': ' '{print $2}' | tail -n1)
datasetName=$(grep 'DatasetName:' output.log | awk -F': ' '{print $2}' | tail -n1)
set_octopusvariable "WorkingDir" "$workingDir"
set_octopusvariable "DatasetName" "$datasetName"
If your script uses (<< 'EOF'), replace each set_octopusvariable line inside it with an echo that prints a labeled value, save the output, and set the variable after EOF:
ssh user@other-host << 'EOF' > output.log 2>&1
WORKDIR="/u/myuser/octopus"
mkdir -p "$WORKDIR/test"
echo "WorkingDir: $WORKDIR"
EOF
workingDir=$(grep 'WorkingDir:' output.log | awk -F': ' '{print $2}' | tail -n1)
set_octopusvariable "WorkingDir" "$workingDir"
If set_octopusvariable still reports "not found" after you've moved it below EOF, the step is running with raw scripting. Follow Fix: raw scripting is turned on.
Keep the remote output predictable. Extra text such as login banners or warnings can end up in a captured value. Print each value on its own clearly labeled line, or filter the output before you set the variable.
Whichever fix you use, consider adding set -e near the top of your bash script, so a failed command stops the step instead of letting it pass with the variable unset.
Verify
- If you removed
OctopusUseRawScriptor changed its scope, open the project's Variables ➜ Preview for the failed step and environment, and confirmOctopusUseRawScriptno longer resolves toTrue. - Redeploy (using a new release or an updated variable snapshot if you changed project variables), or run the runbook again, to the same environment.
- In the task log for the step that sets the variable, confirm there's no
set_octopusvariable"not found" line. - In the later step that reads the variable, add a temporary line such as
echo "WorkingDir is #{Octopus.Action[Step Name].Output.WorkingDir}", replacingStep NameandWorkingDirwith your values. Confirm the task log shows the expected value, not an empty value or the unreplaced#{...}text. - Remove the temporary
echoline when you're done.
Related
- Output variables: how to set output variables in scripts and read them in later steps
- Raw scripting: what raw scripting does and which features it doesn't support
- SSH targets: how SSH deployment targets connect and run steps
- Workers: how to run steps on a worker instead of directly on a target
- Custom scripts: how Octopus runs scripts and which helper functions are available