I. What is Taskfile
Taskfile describes various execution tasks via yaml, and its core is written in go; it is more modern and easier to use than Makefile’s tab splitting and bash combination syntax (although it becomes a yaml engineer). Taskfile has advanced features such as built-in dynamic variables, operating system and other environment variable recognition that are more in line with the modern way of Coding.
In general, if you are not familiar with Makefile and expect to do some batch tasks with a Makefile-like tool, Taskfile is easier to get started with, has a lower learning curve and is fast enough compared to Makefile.
2. Installation and use
2.1. Install go-task
For mac users, there is an official brew installation method.
|
|
For Linux users, there are official installation packages for some Linux distributions, but since there is only one binary file, there is also an official quick install script.
|
|
If you already have a local Go language development environment, you can install it directly via the go command.
|
|
2.2. Quick start
After installation, you just need to write a yaml file Taskfile.yml
, and then you can run the corresponding task via task
command.
If you want to set a default task, just create a task with the name default
.
3. Advanced use
3.1. Environment variables
Taskfile supports three kinds of environment variables:
- Shell environment variables
- Environment variables defined in Taskfile
- Environment variables defined in the variable file
If you need to refer to the environment variables in the Shell, just use the $variable name
method to refer to them directly.
Environment variables can also be defined in the Taskfile.
In addition to this direct reference to variables, Taskfile also supports reading env files to load environment variables like docker-compose; Taskfile will load .env
files in the same directory by default, or you can configure specific files within Taskfile with the dotenv
command.
3.2. Enhanced variables
In addition to the standard environment variables, Taskfile has a built-in, more widely used enhanced variable vars
; this variable mode can be read by go’s template engine (interpolated references), and has special features that environment variables do not have. The following is an example of a vars variable.
In addition to the above use of environment variables, vars also supports dynamic definitions; For example, if we want to get the current git commit id every time a task is executed, we can use the dynamic definition feature of vars.
The vars variable also has some special predefined variables built in, such as {{.TASK}}
which always indicates the current task name, {{.CLI_ARGS}}
which references command line input, etc..
If you run task yarn -- install
, then the {{.CLI_ARGS}}
value will become install
and the yarn install
command will be executed.
In addition, vars variables have other features, such as the ability to pass overrides when referencing across tasks, which will be described later.
3.3. Execution directory
The task defined in Taskfile is executed in the current directory by default, if you want to execute in another directory, you can set the execution directory directly by configuring the dir
parameter instead of writing commands like cd
manually.
3.4. Task dependencies
In CI environments, we often need to define the order of execution and dependencies of tasks; Taskfile provides support for task dependencies through deps
configuration.
3.5. Task invocation
When we define multiple tasks in Taskfile, it is likely that some tasks have some similarity, so we can define Template Task by calling each other and dynamically overriding vars variables.
|
|
3.6. Importing other files
Taskfile supports importing other Taskfiles via the includes
keyword to facilitate the structured processing of Taskfile.
It should be noted that since the introduced file may contain multiple tasks, you need to name the introduced file and refer to the target task by naming it.
When introducing other Taskfiles, by default the commands will be executed in the current main Taskfile directory, but we can also use the dir
parameter to control the execution of tasks in the introduced Taskfile in a specific directory.
3.7. defer handling
Anyone familiar with the go language should know that go has a handy keyword defer
; this directive is used to define actions to be performed at the end of the final code, such as resource cleanup. Taskfile also supports this directive, which allows us to complete some cleanup operations during task execution.
|
|
Of course, in addition to writing commands directly, the defer command can also reference other tasks to complete cleanup.
|
|
4. Advanced applications
4.1, dynamic detection
4.1.1, Output detection
At some point, we may expect to cache some tasks, for example, not to run the download repeatedly if the file has already been downloaded; for this need, Taskfile allows us to define the source file and the generated file, and determine whether the task needs to be executed by the hash value of this set of files.
As you can see from the above diagram, when a task is executed for the first time, a .task directory is generated, which contains the hash value of the file; when the task is executed repeatedly, the real task is not really executed if the hash value does not change. Taskfile has two types of file detection by default: checksum and timestamp. checksum performs hash detection of files (default), which only requires defining sources configuration; timestamp performs timestamp detection of files, which requires defining both sources and generates configuration .
In addition to the two built-in detection modes, we can also define our own detection commands via the status configuration. If the command execution result is 0, the file is considered up-to-date and no task needs to be executed.
4.1.2. Input detection
The above output detection is used to detect the results of the files generated by the task, etc. In some cases we may want to determine a condition before running the task, to determine if the task needs to be run without executing it at all; in this case we can use the preconditions
configuration directive.
4.2. The Go Template Engine
The use of the stencil engine has been shown in the variables section above. Taskfile actually integrates with the slim-sprig library. This library provides a number of convenient methods that can be used within the template engine.
Please refer to the Go Template documentation and the slim-sprig documentation for details on these methods and the use of the template engine.
4.3. Interactive terminal
Some task commands may require interactive terminal to execute, so you can set interactive
option for task; when interactive
is set to true
, task can open interactive terminal when running.
For more details on the use of Taskfile, please read its official documentation, and I will not go into too much detail in this article.