2

Can a script that may be run by Buildbot's ShellCommand detect whether it is indeed running inside the context of Buildbot (e.g. as opposed to having been started manually in a shell)?

For instance, does Buildbot set any environment variables (such as GitLab's CI_JOB_STAGE that the script could inspect?

I am looking for a well-portable solution, so setting a proprietary environment variable e.g. at the level of some Buildbot builders or inspecting the script's parent process is not quite what I am looking for.

rookie099
  • 2,201
  • 2
  • 26
  • 52

1 Answers1

2

At least, it can detect if it is in an interactive session (when launched manually) vs. launched non-interactively (from a script or, in this instance, buildbox builder).
In a bash session, $- would display the current options set for the shell.

Change you job to include a call to your script with:

scriptshell=$- ./myscript.sh

In your script:

if [[ ${scriptshell} != *i* ]]; then
    echo "non-interractive (script)"
else
    echo "interractive (manual launch)
fi
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This looks promising, but is there a variant that leaves invocations from the shell unchanged (`./myscript.sh`)? Changing the call in Buildbot e.g. into `... ./myscript.sh` is fine. – rookie099 Aug 22 '21 at 15:35
  • @rookie099 I never managed to made it work without that `scriptshell` being set *before* calling the script. Hence my proposition. – VonC Aug 22 '21 at 15:36
  • Yes, I see. (It's also a bit like inspecting the parent process.) – rookie099 Aug 22 '21 at 15:40