Task – The Task Runner

Shivam Bhatnagar

The task tool is a utility used to run tasks and is an alternative to GNU make. Task uses a versioned yaml schema to define its the configuration for each of the tasks that need to be run. It is therefore sensible to consider how the version of Task will be managed across multiple projects and systems

References

Taskfile Documentation

Installation

Task can be easily downloaded as an operating system package

brew install go-task      # MacOS
winget install Task.Task  # winget on Microsoft Windows
doas apk add go-task      # apk on Alpine Linux
pacstall -I go-task-deb.  # pacstall on Debian or Ubuntu

Consider using an approach/tool which lets you use/install a specific version of Task for a given project

Getting Task to work in CI

For example Task can be downloaded into CI systems using the following approach. The version in the following command corresponds to the git tag releases for Task

curl --location https://taskfile.dev/install.sh > install_task.sh

./install-task.sh -b $HOME/.local/bin v3.40.0

Getting Task to work with your shell

Follow the guidance as documented in the Task documentation for your shell of choice

Fish

Add the following to your fish config ~/.config/fish/config.fish

task --completion fish | source

Create a Taskfile

You can create a simple Taskfile by creating a file called Taskfile.yaml in your home directory

When Task is called without specifying a task, the default task is run in the Taskfile.yaml

version: '3'

tasks:
  default:
    cmds:
      - task: list

  list:
    desc: List available tasks
    cmds:
      - task --list

  system-info:
    desc: Display system information
    cmds:
      - 'echo CPU architecture: {{ARCH}}'
      - 'echo Operating system: {{OS}}'

The -g flag can be used to run the global/user Taskfile, rather than the nearest

task -g system-info

To test a task you can use the following command

task --dry TASK-NAME

To debug a task you should make sure the following flag is set

silent: false

Taskfile for a Project

This is an example Taskfile for a project. This introduces the following concepts

  1. Includes – https://taskfile.dev/usage/#including-other-taskfiles
  2. Namespaces – https://taskfile.dev/usage/#namespace-aliases
version: '3'

silent: true

# Namespaces
includes:
  package: tasks/package/Taskfile_{{OS}}.yaml
  pre-commit: tasks/pre-commit

# Top-level tasks
tasks:
  default:
    cmds:
      - task: list

  bootstrap:
    desc: Set up environment for development
    cmds:
      - task: pre-commit:setup

  build:
    desc: Build packages
    cmds:
      - task: package:build

  clean:
    desc: Delete generated files
    cmds:
      - task: package:clean

  fmt:
    desc: Format code
    aliases: [format]
    cmds:
      - task: pre-commit:run
        vars: { HOOK_ID: 'ruff-format' }

  lint:
    desc: Run all checks
    aliases: [check]
    cmds:
      - task: pre-commit:check

  list:
    desc: List available tasks
    cmds:
      - task --list

The default task in this file can be run with

task

This will run the default command which will list all available commands.

Specific commands can be run using the appropriate namespace reference

task build

# which is the same as

task package:build

Checking Taskfile (JSON Schema Checks)

Taskfiles are written using a JSON schema so they can be validated using any tool/software which supports this

For a project that uses pre-commit, the following hooks are suggested for json schema checking

- repo: https://github.com/python-jsonschema/check-jsonschema
  rev: '0.29.4'
  hooks:
    - id: check-taskfile
- repo: https://github.com/adrienverge/yamllint.git
  rev: 'v1.35.1'
  hooks:
    - id: yamllint
      args: [--strict]

To ensure that yamllint handles Task files, add a .yamllint.yaml file with this content:

# Begin with yamllint default settings
extends: default

rules:
  # Rules for curly braces: {}
  braces:
    forbid: false
    min-spaces-inside: 0
    max-spaces-inside: 0
    min-spaces-inside-empty: 0
    max-spaces-inside-empty: 0

  # Rules for round brackets: ()
  brackets:
    forbid: false
    min-spaces-inside: 0
    max-spaces-inside: 0
    min-spaces-inside-empty: 0
    max-spaces-inside-empty: 0

  # Do not require three dashes at the start of a YAML document
  document-start: disable

  # Rules for line length
  line-length:
    max: 88
    level: error

Leave a Comment

Your email address will not be published. Required fields are marked *