TIL about just and justfiles

⋅ 2 minute read


just is just a command runner that I recently started using to define aliases for commands in my projects, e.g. build, test, or deploy commands.

Installation

just can be installed via homebrew:

brew install just

Example

I want to create alias commands for my dbt project to build my dbt models in production vs. development environments. At the moment, I need to run the commands:

# prod warehouse
poetry run dbt run --target prod
# dev warehouse
poetry run dbt run --target dev

To define shorter just commands for this project I add a justfile into the project folder.

# justfile
run-prod *ARGS:
    poetry run dbt run --target prod {{ARGS}}

run-dev *ARGS:
    poetry run dbt run --target dev {{ARGS}}

If I am inside the project folder I can now run just run-prod and just run-dev. Moreover, I can pass in any dbt keyword arguments, e.g.

just run-dev --select mymodel

will resolve to

poetry run dbt run --target dev --select mymodel

Is this not just make?

just is inspired by the Unix build tool make from 1978. However, just is not a build system and focuses solely on command running. This avoids having to deal with things like .PHONY that can can catch you out in make recipes. I also like two additional features of just:

  1. I can run my just commands from any subfolder of the project folder that contains the justfile. This is useful when I work on this blog. I have to run hugo serve from the top-level project folder and jupyter-to-markdown converter nb2hugo from the notebooks/project_x/ subfolder. With just command aliases it doesn’t matter from which folder I run the command.

  2. just allows you to load environmental variables from an .env file.

    Example .env:

    DATABASE=localhost:1111
    USERACCOUNT=rob1234
    

    Add set load-dotenv to your justfile and the environment variables will be available in your shell:

    set dotenv-load
    
    check-vars:
        @echo "Env vars are DATABASE:$DATABASE and USER:$USERACCOUNT"                                                                
    

    Running just check-vars will output:

    Env vars are DATABASE:localhost:1111 and USER:rob1234
    

If you have any thoughts, questions, or feedback about this post, I would love to hear it. Please reach out to me via email.

Tags:
#dbt  

Related: