Tasks development

RKD has multiple approaches to define a task. The first one is simpler - in makefile in YAML or in Python. The second one is a set of tasks as a Python package.

Option 1) Simplest - in YAML syntax

Definitely the simplest way to define a task is to use YAML syntax, it is recommended for beginning users.

Example 1:

version: org.riotkit.rkd/yaml/v1
  - rkd.standardlib.jinja.RenderDirectoryTask

  # see this task in "rkd :tasks"
  # run with "rkd :examples:bash-test"
      description: Execute an example command in bash - show only python related tasks
      steps: |
             echo "RKD_DEPTH: ${RKD_DEPTH} # >= 2 means we are running rkd-in-rkd"
             echo "RKD_PATH: ${RKD_PATH}"
             rkd --silent :tasks | grep ":py"

  # try "rkd :examples:arguments-test --text=Hello --test-boolean"
      description: Show example usage of arguments in Bash
              help: "Adds text message"
              required: True
              help: "Example of a boolean flag"
              action: store_true # or store_false
        - |
          echo " ==> In Bash"
          echo " Text: ${ARG_TEXT}"
          echo " Boolean test: ${ARG_TEST_BOOLEAN}"
        - |
          print(' ==> In Python')
          print(' Text: %s ' % ctx.args['text'])
          print(' Text: %s ' % str(ctx.args['test_boolean']))
          return True

  # run with "rkd :examples:list-standardlib-modules"
      description: List all modules in the standardlib
        - |
          ctx: ExecutionContext
          this: TaskInterface

          import os

          print('Hello world')

          return True

      description: "This task runs in /tmp"
      workdir: "/tmp"
      steps: |
          echo "I run in"

Example 2:

version: org.riotkit.rkd/yaml/v1

    GLOBALLY_DEFINED: "16 May 1966, seamen across the UK walked out on a nationwide strike for the first time in half a century. Holding solid for seven weeks, they won a reduction in working hours from 56 to 48 per week "

    - env/global.env

        description: |
            #1 line: 11 June 1888 Bartolomeo Vanzetti, Italian-American anarchist who was framed & executed alongside Nicola Sacco, was born.
            #2 line: This is his short autobiography:
            #3 line: https://libcom.org/library/story-proletarian-life

            INLINE_PER_TASK: "17 May 1972 10,000 schoolchildren in the UK walked out on strike in protest against corporal punishment. Within two years, London state schools banned corporal punishment. The rest of the country followed in 1987."
        env_files: ['env/per-task.env']
        steps: |
            echo " >> ENVIRONMENT VARIABLES DEMO"
            echo "Inline defined in this task: ${INLINE_PER_TASK}\n\n"
            echo "Inline defined globally: ${GLOBALLY_DEFINED}\n\n"
            echo "Included globally - global.env: ${TEXT_FROM_GLOBAL_ENV}\n\n"
            echo "Included in task - per-task.env: ${TEXT_PER_TASK_FROM_FILE}\n\n"

Explanation of examples:

  1. “arguments” is an optional dict of arguments, key is the argument name, subkeys are passed directly to argparse
  2. “steps” is a mandatory list or text with step definition in Bash or Python language
  3. “description” is an optional text field that puts a description visible in “:tasks” task
  4. “workdir” allows to optionally specify a working directory for a task
  5. “environment” is a dict of environment variables that can be defined
  6. “env_files” is a list of paths to .env files that should be included
  7. “imports” imports a Python package that contains tasks to be used in the makefile and in shell usage

Option 2) For Python developers - task as a class

This way allows to create tasks in a structure of a Python module. Such task can be packaged, then published to eg. PyPI (or other private repository) and used in multiple projects.

Each task should implement methods of rkd.api.contract.TaskInterface interface, that’s the basic rule.

Following example task could be imported with path rkd.standardlib.ShellCommandTask, in your own task you would have a different package name instead of rkd.standardlib.

Example task from RKD standardlib:

class ShellCommandTask(TaskInterface):
    """Executes shell scripts"""

    def get_name(self) -> str:
        return ':sh'

    def get_group_name(self) -> str:
        return ''

    def configure_argparse(self, parser: ArgumentParser):
        parser.add_argument('--cmd', '-c', help='Shell command', required=True)

    def execute(self, context: ExecutionContext) -> bool:
        # self.sh() and self.io() are part of TaskUtilities via TaskInterface

            self.sh(context.get_arg('cmd'), capture=False)
        except CalledProcessError as e:
            return False

        return True

Explanation of example:

  1. The docstring in Python class is what will be shown in :tasks as description. You can also define your description by implementing def get_description() -> str
  2. Name and group name defines a full name eg. :your-project:build
  3. def configure_argparse() allows to inject arguments, and –help description for a task - it’s a standard Python’s argparse object to use
  4. def execute() provides a context of execution, please read Tasks API chapter about it. In short words you can get commandline arguments, environment variables there.
  5. self.io() is providing input-output interaction, please use it instead of print, please read Tasks API chapter about it.

Option 3) Quick and elastic way in Python code of Makefile.py

Multiple Makefile files can be used at one time, you don’t have to choose between YAML and Python. This opens a possibility to define more advanced tasks in pure Python, while you have most of the tasks in YAML. It’s elastic - use YAML, or Python or both.

Let’s define then a task in Python in a simplest method.


import os
from rkd.api.syntax import TaskDeclaration
from rkd.api.contract import ExecutionContext
from rkd.standardlib import CallableTask

def union_method(context: ExecutionContext) -> bool:
    os.system('xdg-open https://iwa-ait.org')
    return True

    # just declare a task with a name + code as function! Yay, simple!
    TaskDeclaration(CallableTask(':create-union', union_method), workdir='/var')

TASKS = []

Extended usage - Makefile in Python syntax.

Please check Tasks API for interfaces description