Skip to content

タスクランナー「Task」の使い方

定型タスクを毎回、CLIから実行するのは面倒です。タスクランターであるTask(GitHub)はYAMLファイルに「実行させたいタスク」を定義しておくことで、定型タスクを簡単に実行することができます。

インストール

インストール方法は公式サイトのInstallationに記載されています。

curl -1sLf 'https://dl.cloudsmith.io/public/task/task/setup.deb.sh' |
sudo -E bash &&
sudo apt install -y task
choco install go-task
curl -1sLf 'https://dl.cloudsmith.io/public/task/task/setup.rpm.sh' |
sudo -E bash &&
sudo dnf install -y task
brew install go-task/tap/go-task &&
brew install go-task
nix-env -iA nixpkgs.go-task
npm install -g @go-task/cli
pip install go-task-bin
scoop install task
sudo snap install task --classic
winget install Task.Task

エイリアスを設定する(任意)

これは好みですが、taskを「t」のように短く実行するにはログインシェルの設定ファイルに以下のようなAliasを設定しておきます。

alias t='task'

設定ファイルを作成する

Taskの設定ファイル名は標準だと「Taskfile.yml」です。「task --init」を実行することで同じディレクトリに設定ファイルのテンプレートを作成することができます。

task --init

指定ディレクトリに設定ファイルを作成するには以下のように実行します。

task --init dir1/

設定ファイルテンプレートの中身

task --initで作成されたTaskfile.ymlというファイルが以下の内容で作成されます。

Taskfile.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'

vars:
  GREETING: Hello, world!

tasks:
  default:
    desc: Print a greeting message
    cmds:
      - echo "{{.GREETING}}"
    silent: true

タスクの実行

デフォルトのTaskfile.ymlを前提に、タスクを実行してみます。YAMLファイル中で「tasks」として定義されたタスク名を指定します。

# task default
Hello, world!

但し、「default」だけは特殊で、タスク名が省略された場合は「default」が暗黙的に実行されます。

# task
Hello, world!

タスクの定義

新しいタスクを追加定義するには「tasks」配下に設定を追加します。以下では「hello」というタスクを追加しました。尚、「desc」(説明文)の定義は必須ではありません。但し、後述しますが「descの定義が無い場合、タスクの一覧表示時に該当タスクが表示されない」という挙動になります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'

vars:
  GREETING: Hello, world!

tasks:
  default:
    desc: Print a greeting message
    cmds:
      - echo "{{.GREETING}}"
    silent: true

  hello:
    desc: Saying Hello to the World
    aliases: [h]
    cmds:
      - echo "Hello, World!"

task hello」と入力し、追加したタスクを実行してみます。

# task hello
task: [hello] echo "Hello, World!"
Hello, World!

追加したタスクにはエイリアスが設定されています。その為、このタスクはエイリアスの定義に従って「task h」と入力しても実行することができます。

# task h
task: [hello] echo "Hello, World!"
Hello, World!

タスク一覧の表示

task --list」と実行すると定義されているタスクの一覧が表示されます。

# task --list
task: Available tasks for this project:
* alice:       Say hello to Alice      (aliases: a)
* bob:         Say hello to Bob        (aliases: b)

これをデフォルトタスクとして定義しておくと便利です。具体的には以下の6〜9行目のように定義します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'

tasks:
  default:
    silent: true
    cmds:
      - task --list

  alice:
    aliases: [a]
    desc: Say hello to Alice
    cmds:
      - echo "Hello, Alice!"

  bob:
    aliases: [b]
    desc: Say hello to Bob
    cmds:
      - echo "Hello, Bob!"

  carol:
    aliases: [c]
    cmds:
      - echo "Hello, Carol!"

23〜26行目に定義した「carol」というタスクには「desc」の定義がありません。「desc」の定義が無いタスクは一覧に表示されません。

# task
task: [default] task --list
task: Available tasks for this project:
* alice:       Say hello to Alice      (aliases: a)
* bob:         Say hello to Bob        (aliases: b)

但し、直接タスク名を指定すれば実行することは可能です。

# task carol
task: [carol] echo "Hello, Carol!"
Hello, Carol!

Taskfile.yml内で変数を定義して利用する

繰り返し登場する値は変数として定義しておくと便利です。Taskfile.yml内で変数を定義するには「vars」を利用します。定義した変数はコマンド内で「{{.変数名}}」というテンプレート記法で参照します。

以下では「GREETING」という変数を定義し、「hello」タスクの中で参照しています。

Taskfile.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'

vars:
  GREETING: Hello, world!

tasks:
  hello:
    desc: Saying Hello to the World
    cmds:
      - echo "{{.GREETING}}"

実行すると、変数「GREETING」に定義した値が展開されます。

# task hello
task: [hello] echo "Hello, world!"
Hello, world!

なお、「vars」はTaskfile.yml全体で共有される他、個々のタスク配下にも定義でき、その場合は該当タスク内でのみ有効なローカル変数となります。

.envファイルで変数を定義して利用する

APIキーや環境ごとに異なる値など、Taskfile.ymlに直接書きたくない値は「.envファイル」に切り出すことができます。Taskfile.ymlとは別に用意した.envファイルを読み込むには「dotenv」で対象ファイルを指定します。

まず、.envファイルを「キー名=値」の形式で作成します。

.env
1
2
GREETING=Hello, world!
ENDPOINT=testing.com

次に、Taskfile.ymlの「dotenv」で読み込む.envファイルを指定します。読み込んだ値は環境変数として扱われる為、コマンド内では「$キー名」で参照します。

Taskfile.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'

dotenv: ['.env']

tasks:
  hello:
    desc: Saying Hello to the World
    cmds:
      - echo "Using $GREETING and endpoint $ENDPOINT"

実行すると、.envファイルで定義した値が展開されます。

# task hello
task: [hello] echo "Using $GREETING and endpoint $ENDPOINT"
Using Hello, world! and endpoint testing.com

dotenv」には複数の.envファイルを指定することもできます。この場合、リストの先頭に指定したファイルが優先されます。

Taskfile.yml
dotenv: ['.env', '{{.ENV}}/.env', '{{.HOME}}/.env']

参考

taskのヘルプ表示

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# task --help
Usage: task [flags...] [task...]

Runs the specified task(s). Falls back to the "default" task if no task name
was specified, or lists all tasks if an unknown task name was specified.

Example: 'task hello' with the following 'Taskfile.yml' file will generate an
'output.txt' file with the content "hello".

'''
version: '3'
tasks:
  hello:
    cmds:
      - echo "I am going to write a file named 'output.txt' now."
      - echo "hello" > output.txt
    generates:
      - output.txt
'''

Options:
  -c, --color                       Colored output. Enabled by default. Set flag to false or use NO_COLOR=1 to disable. (default true)
      --completion string           Generates shell completion script.
  -C, --concurrency int             Limit number of tasks to run concurrently.
  -d, --dir string                  Sets the directory in which Task will execute and look for a Taskfile.
      --disable-fuzzy               Disables fuzzy matching for task names.
  -n, --dry                         Compiles and prints tasks in the order that they would be run, without executing them.
  -x, --exit-code                   Pass-through the exit code of the task command.
      --experiments                 Lists all the available experiments and whether or not they are enabled.
  -F, --failfast                    When running tasks in parallel, stop all tasks if one fails.
  -f, --force                       Forces execution even when the task is up-to-date.
  -g, --global                      Runs global Taskfile, from $HOME/{T,t}askfile.{yml,yaml}.
  -h, --help                        Shows Task usage.
  -i, --init                        Creates a new Taskfile.yml in the current folder.
      --insecure                    Forces Task to download Taskfiles over insecure connections.
      --interactive                 Prompt for missing required variables.
  -I, --interval duration           Interval to watch for changes.
  -j, --json                        Formats task list as JSON.
  -l, --list                        Lists tasks with description of current Taskfile.
  -a, --list-all                    Lists tasks with or without a description.
      --nested                      Nest namespaces when listing tasks as JSON
      --no-status                   Ignore status when listing tasks as JSON
  -o, --output string               Sets output style: [interleaved|group|prefixed].
      --output-group-begin string   Message template to print before a task's grouped output.
      --output-group-end string     Message template to print after a task's grouped output.
      --output-group-error-only     Swallow output from successful tasks.
  -p, --parallel                    Executes tasks provided on command line in parallel.
  -s, --silent                      Disables echoing.
      --sort string                 Changes the order of the tasks when listed. [default|alphanumeric|none].
      --status                      Exits with non-zero exit code if any of the given tasks is not up-to-date.
      --summary                     Show summary about a task.
  -t, --taskfile string             Choose which Taskfile to run. Defaults to "Taskfile.yml".
      --temp-dir string             Sets the directory used to store Task temporary files, such as checksums. Relative paths are relative to the root Taskfile.
  -v, --verbose                     Enables verbose mode.
      --version                     Show Task version.
  -w, --watch                       Enables watch of the given task.
  -y, --yes                         Assume "yes" as answer to all prompts.