Commands and Script tasks
Types of tasks for executing programming languages.
For each of the supported languages (e.g. Python, R, Node.js, Shell), Kestra provides two types of tasks: Script and Commands.
- 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. - The
Commands
tasks — best suited for more complex and longer scripts, typically integrated into Kestra as namespace files.
The following table gives an overview of all script tasks and their configuration.
Language | Default image | beforeCommands example | Script example | Commands example |
---|---|---|---|---|
Python | python | pip install requests kestra | print("Hello World!") | python hello.py |
R | r-base | Rscript -e "install.packages('dplyr')" | print("Hello World!") | Rscript hello.R |
Julia | julia | julia -e 'using Pkg; Pkg.add("CSV")' | println("Hello World!") | julia hello.jl |
Ruby | ruby | gem install httparty | puts "Hello World!" | ruby hello.rb |
Node.js | node | npm install json2csv | console.log('Hello World!'); | node hello.js |
Shell | ubuntu | apt-get install curl | echo "Hello World!" | ./hello.bash |
Powershell | powershell | Install-Module -Name ImportExcel | Write-Output "Hello World!" | .\hello.ps1 |
Full class names:
- io.kestra.plugin.scripts.python.Commands
- io.kestra.plugin.scripts.python.Script
- io.kestra.plugin.scripts.r.Commands
- io.kestra.plugin.scripts.r.Script
- io.kestra.plugin.scripts.julia.Commands
- io.kestra.plugin.scripts.julia.Script
- io.kestra.plugin.scripts.ruby.Commands
- io.kestra.plugin.scripts.ruby.Script
- io.kestra.plugin.scripts.node.Commands
- io.kestra.plugin.scripts.node.Script
- io.kestra.plugin.scripts.shell.Commands
- io.kestra.plugin.scripts.shell.Script
- io.kestra.plugin.scripts.powershell.Commands
- io.kestra.plugin.scripts.powershell.Script
Check available blueprints to get started with those tasks.
When to use Script
over Commands
?
The Script
pattern has several advantages:
- Simplicity: the script code is stored and versioned using Kestra's revision history along with your orchestration logic.
- Easier integration with the templating engine: when the entire workflow is defined in one configuration file, it can be easier to access flow inputs, variable definitions, pass outputs to downstream tasks, etc.
However, the Script
tasks also have some disadvantages as compared to the Commands
tasks:
- Readability: adding scripts into a YAML configuration file makes the scripts less readable, especially if they get long. Inline scripts also lack the syntax highlighting and testability that you would get when leveraging our embedded Code Editor instead.
- 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:
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:
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:
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})
Pass Values Into Code
You can pass values into your code too using expressions. We can put the expression directly inside of the Script
task:
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:
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:
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})
Was this page helpful?