How to use a Taskfile: Automate your development workflow
Published on

How to use a Taskfile: Automate your development workflow

Lately, I've been exploring ways to make my workflow smoother, and I found something pretty cool called Taskfile. If you’ve ever felt that typing the same long commands over and over again is a waste of time (spoiler: it is), then Taskfile is your new best friend.

So, what is a Taskfile?

It’s basically a simplified automation tool that lets you define and run commands easily. Instead of memorizing complex commands or setting up aliases for everything, you can just create a task and run it with a simple task <taskname>.

Please note that the commands and everything here were run in the Apple OS. Please make sure to check what would work on your OS.

Setting up a Taskfile

To use Taskfile, you need to have Task installed. If you don’t have it yet, please go to the official Taskfile documentation and follow the steps based on your operating system. This one is an easy one, I promise haha XS

Once installed, you can create a file named Taskfile.yml in your project directory (remember that you will need to create the file for every directory you want to use Taksfile for, so the commands and names can change in each directory too! Isn’t that kinda cool?)

Let’s take a look at an example and break it down!


My Taskfile today

Here's a Taskfile I’ve been using:

version: '3'

tasks:
  add-ssh-git:
    cmds:
      - ssh-keygen -t rsa -b 4096 -C "dignory.luna777@gmail.com"
      - eval $(ssh-agent -s)
      - ssh-add -K ~/.ssh/id_rsa
      - mkdir -p ~/.ssh
      - |
        cat << EOF > ~/.ssh/config
        Host *
          AddKeysToAgent yes
          UseKeychain yes
          IdentityFile ~/.ssh/id_rsa
        E
      - echo "SSH configuration added successfully."

  config-git:
    cmds:
      - git config --global user.email "dignory.luna777@gmail.com"
      - git config --global user.name "Dignory Carmona"

  start:
    cmds:
      - yarn install
      - yarn dev

  commit-react:
    cmds:
      - yarn build
      - git commit -a

  commit:
    cmds:
      - git add .
      - git commit
      - git push

  change-message:
    cmds:
      - git commit --amend
      - git push -f origin "main"

Let’s break it down, shall we?

This Taskfile has a bunch of useful automation tasks. Let’s go through some of the key ones:

1. Automating SSH key setup for GitHub

Ever struggled with setting up SSH for GitHub? This task makes it one command away:

task add-ssh-git

This will:

  • Generate an SSH key
  • Start the SSH agent
  • Add the key to your system
  • Set up an SSH config file
  • Print "SSH configuration added successfully."

No more manual setup, just one task and done!


2. Configuring Git with one command

Instead of running multiple Git commands every time you set up a new environment, just run:

task config-git

It will set your Git user email and name globally in one go.


3. Starting your dev server

Tired of typing yarn install and yarn dev separately?

task start

This will install dependencies and start the dev server in one step.


4. Committing and pushing code faster (my fav and most used)

Instead of typing git add ., git commit, and git push every time, just use:

task commit

Want to change the commit message after pushing?

task change-message

This will amend the last commit and force-push it. Perfect for those "oops" moments 😅.


Some common terminal commands…

git branch -d branch_name   # Delete a local branch
git push origin --delete branch_name  # Delete a remote branch
git checkout -- .   # Discard all unstaged changes
git reset --hard    # Reset everything to the last commit
git reset --soft HEAD~1  # Undo last commit but keep changes staged
git clean -fd       # Remove untracked files and directories

Anyway, I am thinking of adding new Git commands to my Taskfile. I often forget their names because I don’t use them as frequently. Any commands suggestions?