Skip to content

Commit

Permalink
version is based on git state or BUILD_VERSION (#1042)
Browse files Browse the repository at this point in the history
Git version to be calculated by 'git describe --tags --always' and add
'-unsupported' when the local workspace has an uncommitted change.

the rules are:
* BUILD_VERSION will override any calculated version if set as env/make
  cli variable
* if current commit is tagged, use just the tag
* else <tag>-<num commits since>-g<commit id>

release target will use CURRENT_VERSION to make it simple to set in the
release branch

Remove unused netplugin-version artifact

Signed-off-by: Chris Plock <chrisplo@cisco.com>
  • Loading branch information
chrisplo committed Nov 2, 2017
1 parent e6df792 commit d125eb7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
43 changes: 21 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# make BUILD_VERSION=1.2.3 [compile-with-docker] will set netplugin -version output
# make BUILD_VERSION=1.2.3 tar will set the tar filename
# default naming will otherwise be value of version/CURRENT_VERSION
# BUILD_VERSION will affect archive filenames as well as -version
# default version will be based on $(git describe --tags --always)


.PHONY: all all-CI build clean default unit-test release tar checks go-version gofmt-src \
Expand All @@ -14,13 +13,9 @@ TO_BUILD := ./netplugin/ ./netmaster/ ./netctl/netctl/ ./mgmtfn/k8splugin/contiv
HOST_GOBIN := `if [ -n "$$(go env GOBIN)" ]; then go env GOBIN; else dirname $$(which go); fi`
HOST_GOROOT := `go env GOROOT`
NAME := netplugin
# We are using date based versioning, so for consistent version during a build
# we evaluate and set the value of version once in a file and use it in 'tar'
# and 'release' targets.
VERSION_FILE := $(NAME)-version
VERSION := `cat $(VERSION_FILE)`
VERSION := $(shell scripts/getGitVersion.sh)
TAR_EXT := tar.bz2
NETPLUGIN_CONTAINER_TAG := $(shell ./scripts/getGitCommit.sh)
NETPLUGIN_CONTAINER_TAG := $(shell ./scripts/getGitVersion.sh)
TAR_FILENAME := $(NAME)-$(VERSION).$(TAR_EXT)
TAR_LOC := .
TAR_FILE := $(TAR_LOC)/$(TAR_FILENAME)
Expand Down Expand Up @@ -97,17 +92,16 @@ checks-with-docker:

compile:
cd $(GOPATH)/src/github.com/contiv/netplugin && \
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} BUILD_VERSION=${BUILD_VERSION} \
TO_BUILD="${TO_BUILD}" VERSION_FILE=${VERSION_FILE} \
scripts/build.sh
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} TO_BUILD="${TO_BUILD}" \
BUILD_VERSION=$(VERSION) scripts/build.sh

# fully prepares code for pushing to branch, includes building binaries
run-build: deps checks clean compile

compile-with-docker:
docker build \
--build-arg NIGHTLY_RELEASE=${NIGHTLY_RELEASE} \
--build-arg BUILD_VERSION=${BUILD_VERSION} \
--build-arg NIGHTLY_RELEASE=$(NIGHTLY_RELEASE) \
--build-arg BUILD_VERSION=$(VERSION) \
-t netplugin-build:$(NETPLUGIN_CONTAINER_TAG) .

build-docker-image: start
Expand Down Expand Up @@ -328,11 +322,8 @@ host-plugin-release:

# build tarball
tar: compile-with-docker
@# $(TAR_FILE) depends on local file netplugin-version (exists in image),
@# but it is evaluated after we have extracted that file to local disk
docker rm netplugin-build || :
c_id=$$(docker create --name netplugin-build netplugin-build:$(NETPLUGIN_CONTAINER_TAG)) && \
docker cp $${c_id}:/go/src/github.com/contiv/netplugin/netplugin-version ./ && \
for f in netplugin netmaster netctl contivk8s netcontiv; do \
docker cp $${c_id}:/go/bin/$$f bin/$$f; done && \
docker rm $${c_id}
Expand All @@ -342,11 +333,19 @@ tar: compile-with-docker

clean-tar:
@rm -f $(TAR_LOC)/*.$(TAR_EXT)
@rm -f ${VERSION_FILE}

# GITHUB_USER and GITHUB_TOKEN are needed be set to run github-release
release: tar
TAR_FILENAME=$(TAR_FILENAME) TAR_FILE=$(TAR_FILE) \
OLD_VERSION=${OLD_VERSION} BUILD_VERSION=${BUILD_VERSION} \
# do not run directly, use "release" target
release-built-version: tar
TAR_FILENAME=$(TAR_FILENAME) TAR_FILE=$(TAR_FILE) OLD_VERSION=${OLD_VERSION} \
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} scripts/release.sh
@make clean-tar

# The first "release" below is not a target, it is a "target-specific variable"
# and sets (and in this case exports) a variable to the target's environment
# The second release runs make as a subshell but with BUILD_VERSION set
# to write the correct version for assets everywhere
#
# GITHUB_USER and GITHUB_TOKEN are needed be set (used by github-release)
release: export BUILD_VERSION=$(shell cat version/CURRENT_VERSION)
release:
@make release-built-version
18 changes: 3 additions & 15 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,9 @@ You'll find a few examples below:

Please keep in mind that the release notes can be updated on GitHub manually.

BUILD_VERSION can be used to override the version specified in
version/CURRENT_VERSION. This variable should be used to avoid changing
the version for every single beta/rc release.

BUILD_VERSION shouln't be used to override the version for actual
BUILD_VERSION will not override the version for actual
releases (1.0, 1.0.1, 1.1.0 and so on).

Automated nightly releases use the version from version/CURRENT_VERSION.
These nightly releases also append a timestamp to the version found in
that file.

The release process can be found below.

1. Check out the right branch and the right commit. This is necessary
Expand All @@ -61,20 +53,16 @@ release isn't made from the HEAD of master.
git push origin 1.0.1
```

3. Write down the BUILD_VERSION or update version/CURRENT_VERSION. This
will be needed for the next steps. Please refer to the explanation
related to BUILD_VERSION and version/CURRENT_VERSION above.
3. Update version/CURRENT_VERSION. This will be needed for the next steps.

4. Make sure GITHUB_USER and GITHUB_TOKEN variables are exported in your environment.

5. Make the release to GitHub.
```
# BUILD_VERSION is used to override version/CURRENT_VERSION
OLD_VERSION=1.1.0-beta.1 BUILD_VERSION=1.1.0-beta.2 make release
OLD_VERSION=1.1.0-beta.1 make release
```

```
# version/CURRENT_VERSION is used for a new stable release
# version/CURRENT_VERSION is 1.1.1
OLD_VERSION=1.1.0 make release
```
Expand Down
20 changes: 2 additions & 18 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,12 @@

set -euxo pipefail

BUILD_TIME=$(date -u +%m-%d-%Y.%H-%M-%S.UTC)
VERSION=$(cat version/CURRENT_VERSION | tr -d '\n')
PKG_NAME=github.com/contiv/netplugin/version

# BUILD_VERSION overrides the version from CURRENT_VERSION
if [ -n "$BUILD_VERSION" ]; then
VERSION=$BUILD_VERSION
fi

if [ -z "$NIGHTLY_RELEASE" ]; then
BUILD_VERSION="$VERSION"
else
BUILD_VERSION="$VERSION-$BUILD_TIME"
fi

GIT_COMMIT=$(./scripts/getGitCommit.sh)

echo $BUILD_VERSION >$VERSION_FILE

PKG_NAME=github.com/contiv/netplugin/version
GOGC=1500 go install -v \
-ldflags "-X $PKG_NAME.version=$BUILD_VERSION \
-X $PKG_NAME.buildTime=$BUILD_TIME \
-X $PKG_NAME.buildTime=$(date -u +%m-%d-%Y.%H-%M-%S.UTC) \
-X $PKG_NAME.gitCommit=$GIT_COMMIT \
-s -w" \
$TO_BUILD
32 changes: 32 additions & 0 deletions scripts/getGitVersion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -euo pipefail

BUILD_VERSION=${BUILD_VERSION:-}
NIGHTLY_RELEASE=${NIGHTLY_RELEASE:-}

# calculate version
if command -v git &>/dev/null && git rev-parse &>/dev/null; then
GIT_COMMIT=$(git describe --tags --always 2>/dev/null || echo unknown)
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
GIT_COMMIT="$GIT_COMMIT-unsupported"
fi
VERSION=$GIT_COMMIT
else
echo >&2 'error: unable to determine the git revision'
exit 1
fi

# BUILD_VERSION overrides the git calculated version
if [ -n "$BUILD_VERSION" ]; then
VERSION=$BUILD_VERSION
fi

if [ -z "$NIGHTLY_RELEASE" ]; then
VERSION="$VERSION"
else
BUILD_TIME=$(date -u +%m-%d-%Y.%H-%M-%S.UTC)
VERSION="$VERSION-$BUILD_TIME"
fi

echo $VERSION

0 comments on commit d125eb7

Please sign in to comment.