Injecting app version with tools.deps?

I would like to automatically inject a git-based (e.g., tag + commit) version identifier (for example, under resources/version.edn) when building a jar, so I can use it e.g., when logging.

I know there are some lein plugins (e.g, lein-v) that can do something like this, but I haven’t found any tools.deps equivalents. I guess it’d be easy enough to hack something together that does this, but if there’s an existing solution, I’d be happy to adopt it.

Any suggestions?

2 Likes

If you’re using git, have look at https://github.com/jgrodziski/metav

1 Like

in my build script I have this:

echo "updating version string..."
sed -i -re "s/build \{\{BUILD\}\}/build $(git rev-parse --short HEAD)/g" src/clj/foo/bar.clj

## ... build code

echo "reverting version string..."
sed -i -re "s/build [a-zA-Z0-9_]+/build \{\{BUILD\}\}/g" src/clj/foo/bar.clj

and in the src/clj/foo/bar.clj you have sth like:

(def version "build {{BUILD}}")

Thanks! I’ll have a lookg at this, it seems to do pretty much what I need.
In the meantime, I came up with a short bash script that does something similar to what @dimovich does, and run that on a post-commit hook, generating a .gitignored version file.

I’d rather just use a .edn file in resources where the git revision is dumped into.

That’s what I ended up with.

#!/usr/bin/env bash
set -euo pipefail

APP_NAME='"MyApp"'
TAG="`git tag | head -n1 | awk '{print $1}'`"
COMMIT="`git rev-parse --short HEAD`"
COMMIT_COUNT=`git rev-list --all --count`

echo "{"
echo ":app/name $APP_NAME"
echo ":git/tag \"$TAG\""
echo ":git/commit \"$COMMIT\""
echo ":git/commit-count $COMMIT_COUNT"
echo "}"

And this is called by a post-commit hook, with output redirected to resources/version.edn (which is not under version control, otherwise you always get a dirty repo)

1 Like