justfile Unknown start of token in an exported variable

672 Views Asked by At

My justfile looks like this:

export GIT_COMMIT:="$(git show --format="%h" --no-patch)"

docker_build:
    docker build -t some/tag:$GIT_COMMIT -t some/tag:latest .

when I run it then I get the following error:

export GIT_COMMIT:="$(git show --format="%h" --no-patch)"
                                         ^

So basically just doesn't like %. But it's necessary.

Is there an elegent way to get this to work? I use the GIT_COMMIT variable in a few different places so it would be nice if it was reusable.

So far I'm using this workaround:

docker_build:
    GIT_COMMIT="$(git show --format="%h" --no-patch)";  docker build -t some/tag:$GIT_COMMIT .
    docker build -t some/tag:latest .

But it gets a bit messy

1

There are 1 best solutions below

0
Nick K9 On

The problem with your justfile is not the percent sign, it's that you're using the same type of quotes for the outer string and for the inner one. just is interpreting the second quote as ending the string, and then doesn't know what to do with the bare %.

In this case, because the format is just a single string token long with no spaces, you can simply remove the inner quotes entirely:

export GIT_COMMIT := "$(git show --format=%h --no-patch)"

But if you actually needed the format to have spaces, you could use alternating quotes like this:

export GIT_COMMIT := "$(git show --format='hash: %h' --no-patch)"

Even more options in the Just manual section on strings. You could also use backticks to save the result of the command to the just variable instead of saving the string of the unevaluated bash command substitution.