タスクランナー「Task」の使い方
定型タスクを毎回、CLIから実行するのは面倒です。タスクランターであるTask(GitHub)はYAMLファイルに「実行させたいタスク」を定義しておくことで、定型タスクを簡単に実行することができます。
インストール¶
インストール方法は公式サイトのInstallationに記載されています。
エイリアスを設定する(任意)¶
これは好みですが、taskを「t」のように短く実行するにはログインシェルの設定ファイルに以下のようなAliasを設定しておきます。
設定ファイルを作成する¶
Taskの設定ファイル名は標準だと「Taskfile.yml」です。「task --init」を実行することで同じディレクトリに設定ファイルのテンプレートを作成することができます。
指定ディレクトリに設定ファイルを作成するには以下のように実行します。
設定ファイルテンプレートの中身¶
task --initで作成されたTaskfile.ymlというファイルが以下の内容で作成されます。
# 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」として定義されたタスク名を指定します。
但し、「default」だけは特殊で、タスク名が省略された場合は「default」が暗黙的に実行されます。
タスクの定義¶
新しいタスクを追加定義するには「tasks」配下に設定を追加します。以下では「hello」というタスクを追加しました。尚、「desc」(説明文)の定義は必須ではありません。但し、後述しますが「descの定義が無い場合、タスクの一覧表示時に該当タスクが表示されない」という挙動になります。
# 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 h」と入力しても実行することができます。
タスク一覧の表示¶
「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行目のように定義します。
# 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)
但し、直接タスク名を指定すれば実行することは可能です。
Taskfile.yml内で変数を定義して利用する¶
繰り返し登場する値は変数として定義しておくと便利です。Taskfile.yml内で変数を定義するには「vars」を利用します。定義した変数はコマンド内で「{{.変数名}}」というテンプレート記法で参照します。
以下では「GREETING」という変数を定義し、「hello」タスクの中で参照しています。
# 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」に定義した値が展開されます。
なお、「vars」はTaskfile.yml全体で共有される他、個々のタスク配下にも定義でき、その場合は該当タスク内でのみ有効なローカル変数となります。
.envファイルで変数を定義して利用する¶
APIキーや環境ごとに異なる値など、Taskfile.ymlに直接書きたくない値は「.envファイル」に切り出すことができます。Taskfile.ymlとは別に用意した.envファイルを読み込むには「dotenv」で対象ファイルを指定します。
まず、.envファイルを「キー名=値」の形式で作成します。
次に、Taskfile.ymlの「dotenv」で読み込む.envファイルを指定します。読み込んだ値は環境変数として扱われる為、コマンド内では「$キー名」で参照します。
# 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ファイルを指定することもできます。この場合、リストの先頭に指定したファイルが優先されます。
参考¶
taskのヘルプ表示¶
# 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.