diff --git a/content/docs/04.workflow-components/01.tasks/02.scripts/01.commands-vs-scripts.md b/content/docs/04.workflow-components/01.tasks/02.scripts/01.commands-vs-scripts.md index 6f09a8f2c4..df5ff606e4 100644 --- a/content/docs/04.workflow-components/01.tasks/02.scripts/01.commands-vs-scripts.md +++ b/content/docs/04.workflow-components/01.tasks/02.scripts/01.commands-vs-scripts.md @@ -5,10 +5,16 @@ icon: /docs/icons/dev.svg Types of tasks for executing programming languages. +
+ +
+ +--- + For each of the [supported languages](./00.languages.md) (e.g. Python, R, Node.js, Shell), Kestra provides two types of tasks: Script and Commands. 1. The `Script` tasks — typically written **inline** in the YAML flow configuration. They are best suited for short scripts and they make it easy to pass data from flow inputs and other tasks to your custom scripts. -2. The `Commands` tasks — best suited for more complex and longer scripts, typically integrated into kestra as [namespace files](../../../05.concepts/02.namespace-files.md). +2. The `Commands` tasks — best suited for more complex and longer scripts, typically integrated into Kestra as [namespace files](../../../05.concepts/02.namespace-files.md). The following table gives an overview of all script tasks and their configuration. @@ -56,3 +62,120 @@ However, the `Script` tasks also have some **disadvantages** as compared to the - **Complex use cases**: if your business logic comprises multiple files importing classes and functions from other modules, you will need to use the `Commands` tasks instead. Overall, we recommend using `Commands` tasks for advanced production workloads. However, the `Script` tasks offer a great alternative for simple use cases. + +## Examples + +Here's an example comparing the Python `Script` and `Commands` task using the same Python Code: + +Script Task: +```yaml +id: python_script +namespace: company.team + +tasks: + - id: python + type: io.kestra.plugin.scripts.python.Script + beforeCommands: + - pip install requests kestra + script: | + from kestra import Kestra + import requests + + response = requests.get('https://kestra.io') + print(response.status_code) + + Kestra.outputs({'status': response.status_code, 'text': response.text}) +``` + +Commands Task: +```yaml +id: python_commands +namespace: company.team + +tasks: + - id: python + type: io.kestra.plugin.scripts.python.Commands + namespaceFiles: + enabled: true + include: + - main.py + beforeCommands: + - pip install requests kestra + commands: + - python main.py +``` + +`main.py` file: +```python +from kestra import Kestra +import requests + +response = requests.get('https://kestra.io') +print(response.status_code) + +Kestra.outputs({'status': response.status_code, 'text': response.text}) +``` + +You can pass values into your code too using expressions. We can put the expression directly inside of the `Script` task: + +```yaml +id: python_script_dynamic +namespace: company.team + +inputs: + - id: uri + type: STRING + defaults: https://kestra.io + +tasks: + - id: python + type: io.kestra.plugin.scripts.python.Script + beforeCommands: + - pip install requests kestra + script: | + from kestra import Kestra + import requests + + response = requests.get('{{ inputs.uri }}') + print(response.status_code) + + Kestra.outputs({'status': response.status_code, 'text': response.text}) +``` + +We can also pass values into `Commands` tasks by using an environment variable: + +```yaml +id: python_commands_dynamic +namespace: company.team + +inputs: + - id: uri + type: STRING + defaults: https://kestra.io + +tasks: + - id: python + type: io.kestra.plugin.scripts.python.Commands + namespaceFiles: + enabled: true + include: + - main.py + beforeCommands: + - pip install requests kestra + commands: + - python main.py + env: + URI: "{{ inputs.uri }}" +``` + +`main.py` file: +```python +from kestra import Kestra +import requests +import os + +response = requests.get(os.environ['URI']) +print(response.status_code) + +Kestra.outputs({'status': response.status_code, 'text': response.text}) +```