env.adoc 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. [#env-vars]
  2. = Environment variables
  3. All argument names and values are also passed as environment variables as well, which can be very useful when passing several arguments to a script, for example.
  4. [source,yaml]
  5. .`config.yaml`
  6. ----
  7. actions:
  8. - title: Print names of new files
  9. shell: /opt/newfile.py
  10. arguments:
  11. - name: filename
  12. type: unicode_identifier
  13. - name: filesizebytes
  14. type: unicode_identifier
  15. - name: fileisdir
  16. type: unicode_identifier
  17. execOnFileCreatedInDir:
  18. - /home/user/Downloads/
  19. ----
  20. This is an example of a python script using the environment variables;
  21. [source,python]
  22. .`/opt/newfile.py`
  23. ----
  24. #!/usr/bin/env python
  25. import os
  26. print(os.environ['OLIVETIN'])
  27. print(os.environ['FILENAME'])
  28. print(os.environ['FILESIZEBYTES'])
  29. print(os.environ['FILEISDIR'])
  30. ----
  31. [#execution-request-variables]
  32. == Execution Request Variables
  33. OliveTin injects two execution request variables into every action execution. They are available as template variables (e.g. in `shell`, `shellAfterCompleted`, or other template fields) and as environment variables passed to the process.
  34. * `ot_username` — The username of the user who started the execution. In templates (version 3k) use `{{ .Arguments.ot_username }}`; in the process environment it is `OT_USERNAME`. For unauthenticated or automated runs this may be `guest`, `cron`, or similar, depending on how the action was triggered.
  35. * `ot_executionTrackingId` — A unique identifier for this execution. In templates (version 3k) use `{{ .Arguments.ot_executionTrackingId }}`; in the process environment it is `OT_EXECUTIONTRACKINGID`. Useful for logging, correlating with the API or execution log, or idempotency in scripts.
  36. In version 2k, the template syntax was `{{ ot_username }}` and `{{ ot_executionTrackingId }}` (without the `.Arguments.` prefix). Version 3k uses the `.Arguments.` form.
  37. Example in a shell command (version 3k):
  38. [source,yaml]
  39. ----
  40. shell: echo "Run by {{ .Arguments.ot_username }} (execution {{ .Arguments.ot_executionTrackingId }})"
  41. ----
  42. Example in a script using environment variables:
  43. [source,shell]
  44. ----
  45. #!/bin/sh
  46. echo "Started by $OT_USERNAME with tracking id $OT_EXECUTIONTRACKINGID"
  47. ----
  48. In xref:action_execution/aftercompletion.adoc[Execute after completion] (`shellAfterCompleted`), the same variables are available as template variables (e.g. `{{ .Arguments.ot_username }}`, `{{ .Arguments.ot_executionTrackingId }}` in version 3k); user-defined argument values are not passed there.
  49. [#olivetin-env-var]
  50. == The OLIVETIN environment variable
  51. OliveTin sets the environment variable `OLIVETIN` to `1` for every action it runs. Scripts can check this variable to detect whether they are running inside OliveTin (for example, to adjust logging, skip interactive prompts, or enable OliveTin-specific behavior).
  52. [source,shell]
  53. .Example: detect OliveTin in a shell script
  54. ----
  55. #!/bin/sh
  56. if [ "$OLIVETIN" = "1" ]; then
  57. echo "Running under OliveTin"
  58. else
  59. echo "Running outside OliveTin"
  60. fi
  61. ----
  62. == Using process environment in templates
  63. To use the *process* environment (the environment OliveTin was started with) inside action template fields such as `shell` or `shellAfterCompleted`, use the `.Env` template variable: `{{ .Env.VAR_NAME }}`. This substitutes the value at execution time. See xref:advanced_configuration/config_envs.adoc#using-env-in-template-replacements[Using .Env in template replacements] for details and examples.
  64. For other template features, including JSON encoding of argument and entity values, see xref:args/templates.adoc[Templates in actions].
  65. == Notes
  66. . Argument names are converted to uppercase for environment variables, `name: filename` becomes `FILENAME`.
  67. . OliveTin sets `OLIVETIN=1` in the process environment for every action; see <<olivetin-env-var,The OLIVETIN environment variable>> above.
  68. . The execution request variables are exposed as `OT_USERNAME` and `OT_EXECUTIONTRACKINGID` in the process environment; see <<execution-request-variables,Execution Request Variables>> above.
  69. . The environment variables are passed into the execution context which uses a shell (/bin/sh on Linux), so it is also possible to use them with the $ notation in the `shell` line, like this; `shell: echo $FILENAME` for example.