Home > Enterprise >  Makefile: compare two version string values from git tag and package.json
Makefile: compare two version string values from git tag and package.json

Time:11-11

I'm trying to compare version values between the pacakge.json latest git tag so that I can fail the CI build if the versions do not match. I've created a make target that will take the two version values and do a string comparison to make sure they are the same, although I'm open to numeric comparsions if possible.

PKG_VERSION=$(shell echo v$$(jq .version package.json))
LATEST_TAG=$(shell git describe --tags --abbrev=0 $$(git rev-list --tags --max-count=1))
enforce-versions:
    @echo "Comparing \"$(PKG_VERSION)\" & \"$(LATEST_TAG)\""
ifeq ($(PKG_VERSION), $(LATEST_TAG))
    exit 0;
else
    exit 1;
endif

Output:

$ make enforce-versions 
Comparing "v1.0.0" & "v1.1.0"
exit 1;
make: *** [enforce-versions] Error 1
$ make enforce-versions
Comparing "v1.1.0" & "v1.1.0"
exit 1;
make: *** [enforce-versions] Error 1

CodePudding user response:

You can try this to remove \r:

PKG_VERSION=$(shell echo v$$(jq .version package.json | tr -d $$\'\\r\'))

You may need to apply the pipe to LATEST_TAG as well.

CodePudding user response:

You can do the comparison in the shell command instead of inside of an ifeq as so:

PKG_VERSION=$(shell echo v$$(jq .version package.json))
LATEST_TAG=$(shell git describe --tags --abbrev=0 $$(git rev-list --tags --max-count=1))
enforce-versions:
    @echo "Comparing \"$(PKG_VERSION)\" & \"$(LATEST_TAG)\""
    @[ "$(PKG_VERSION)" = "$(LATEST_TAG)" ]

Notice you don't need the explicit exit 0 or exit 1 in this case. If the tags are not equal, the command will return false, which will cause make to fail the recipe line, and return 1

  • Related