Makefile increase variable on each build

Viewed 165

I've a makefile with variable DOCKER_TAG = 0.0.1

and I want that on each target called it will be bumped (only patch version ) is it possible

e.g.

DOCKER_TAG = 0.0.1

push: 
    docker push docker.io/testasm:$(DOCKER_TAG)

now If I run make push it will use version 0.0.1 but If I do it for the second time it will be bumped to 0.0.2 and so on (on each run of make push) , is it possible ?

update I've tried the following

DOCKER_TAG = 0.0.1
DOCKER_REGISTRY = testreg/vsp


VERSION_FILE   := version.txt
-include $(VERSION_FILE)

VERMAJMIN      := $(subst ., ,$(DOCKER_TAG))

DOCKER_TAG     := $(shell [ -f "$(VERSION_FILE)" ] && cat "$(VERSION_FILE)" || echo '0.0.1')
VERMAJMIN      := $(subst ., ,$(DOCKER_TAG))
VERSION        := $(word 1,$(VERMAJMIN))
MAJOR          := $(word 2,$(VERMAJMIN))
MINOR          := $(word 3,$(VERMAJMIN))
NEW_MINOR      := $(shell expr "$(MINOR)" + 1)
NEW_DOCKER_TAG := $(VERSION).$(MAJOR).$(NEW_MINOR)



NEW_DOCKER_TAG := $(VERSION).$(MAJOR).$(NEW_MINOR)


build:
    echo "$(NEW_DOCKER_TAG)" > "$(VERSION_FILE)"

And the version.txt file get value of .. two dots ... any idea how to solve it?

if I take the last option in the answer as-is I see that the version.txt file contain DOCKER_TAG := ..

update 2

I've created empty version.txt file and use the folloing as-is

VERSION_FILE   := version.txt
DOCKER_TAG     := 0.0.1.

-include $(VERSION_FILE)

VERMAJMIN      := $(subst ., ,$(DOCKER_TAG))

NEW_DOCKER_TAG := $(VERSION).$(MAJOR).$(NEW_MINOR)
build:
    docker build -t docker.io/vcd/test:$(DOCKER_TAG) .
    echo "DOCKER_TAG := $(NEW_DOCKER_TAG)" > "$(VERSION_FILE)"

and I got in the version.txt file the following DOCKER_TAG := ..

1 Answers

The best would be to use some docker command (docker search?) to query the repository and get the last version from there. In case it is not possible you can store it in a text file, read it each time you run make, and update it. Something like:

VERSION_FILE   := version.txt
DOCKER_TAG     := $(shell [ -f "$(VERSION_FILE)" ] && cat "$(VERSION_FILE)" || echo '0.0.1')
VERMAJMIN      := $(subst ., ,$(DOCKER_TAG))
VERSION        := $(word 1,$(VERMAJMIN))
MAJOR          := $(word 2,$(VERMAJMIN))
MINOR          := $(word 3,$(VERMAJMIN))
NEW_MINOR      := $(shell expr "$(MINOR)" + 1)
NEW_DOCKER_TAG := $(VERSION).$(MAJOR).$(NEW_MINOR)

push:
    docker push docker.io/testasm:$(DOCKER_TAG)
    echo "$(NEW_DOCKER_TAG)" > "$(VERSION_FILE)"

Of course you will have to take care of your version file and avoid accidental deletion. For higher security you could write the first version file yourself and instruct make to raise an error in case the file does not exist:

VERSION_FILE   := version.txt

ifneq ($(wildcard $(VERSION_FILE)),$(VERSION_FILE))

$(error $(VERSION_FILE) not found)

else

DOCKER_TAG     := $(shell cat "$(VERSION_FILE)")
VERMAJMIN      := $(subst ., ,$(DOCKER_TAG))
VERSION        := $(word 1,$(VERMAJMIN))
MAJOR          := $(word 2,$(VERMAJMIN))
MINOR          := $(word 3,$(VERMAJMIN))
NEW_MINOR      := $(shell expr "$(MINOR)" + 1)
NEW_DOCKER_TAG := $(VERSION).$(MAJOR).$(NEW_MINOR)

push:
    docker push docker.io/testasm:$(DOCKER_TAG)
    echo "$(NEW_DOCKER_TAG)" > "$(VERSION_FILE)"

endif

Variant where the version file is itself a Makefile included by the main one:

VERSION_FILE   := version.txt
DOCKER_TAG     := 0.0.1

-include $(VERSION_FILE)

VERMAJMIN      := $(subst ., ,$(DOCKER_TAG))
VERSION        := $(word 1,$(VERMAJMIN))
MAJOR          := $(word 2,$(VERMAJMIN))
MINOR          := $(word 3,$(VERMAJMIN))
NEW_MINOR      := $(shell expr "$(MINOR)" + 1)
NEW_DOCKER_TAG := $(VERSION).$(MAJOR).$(NEW_MINOR)

push:
    docker push docker.io/testasm:$(DOCKER_TAG)
    echo "DOCKER_TAG := $(NEW_DOCKER_TAG)" > "$(VERSION_FILE)"

(remove the - on front of -include $(VERSION_FILE) to get an error if the file does not exist. This last one is my favorite.

Related