Skip to content

Commit

Permalink
Inject build number for dev builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpidcock committed Dec 12, 2019
1 parent e8efc11 commit 7e2e3ee
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
46 changes: 31 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ GIT_TREE_STATE = $(if $(shell test -d .git || echo missing),archive,$(if $(shell
# Example: BUILD_TAGS="minimal provider_kubernetes"
BUILD_TAGS ?=

# Build number passed in must be a monotonic int representing
# the build.
JUJU_BUILD_NUMBER ?=

# Compile with debug flags if requested.
ifeq ($(DEBUG_JUJU), 1)
COMPILE_FLAGS = -gcflags "all=-N -l"
LINK_FLAGS = -ldflags "-X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE)"
LINK_FLAGS = -ldflags "-X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE) -X $(PROJECT)/version.build=$(JUJU_BUILD_NUMBER)"
else
COMPILE_FLAGS =
LINK_FLAGS = -ldflags "-s -w -X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE)"
LINK_FLAGS = -ldflags "-s -w -X $(PROJECT)/version.GitCommit=$(GIT_COMMIT) -X $(PROJECT)/version.GitTreeState=$(GIT_TREE_STATE) -X $(PROJECT)/version.build=$(JUJU_BUILD_NUMBER)"
endif

define DEPENDENCIES
Expand Down Expand Up @@ -184,29 +188,41 @@ check-deps:
@echo "$(GOCHECK_COUNT) instances of gocheck not in test code"

# CAAS related targets
DOCKER_USERNAME ?= jujusolutions
JUJUD_STAGING_DIR ?= /tmp/jujud-operator
JUJUD_BIN_DIR ?= ${GOPATH}/bin
OPERATOR_IMAGE_BUILD_SRC ?= true
OPERATOR_IMAGE_TAG ?= $(shell jujud version | rev | cut -d- -f3- | rev)
OPERATOR_IMAGE_PATH = ${DOCKER_USERNAME}/jujud-operator:${OPERATOR_IMAGE_TAG}

operator-image:
ifeq ($(OPERATOR_IMAGE_BUILD_SRC),true)
make install
else
@echo "skipping to build jujud bin, use existing one at ${JUJUD_BIN_DIR}/jujud."
endif
DOCKER_USERNAME ?= jujusolutions
JUJUD_STAGING_DIR ?= /tmp/jujud-operator
JUJUD_BIN_DIR ?= ${GOPATH}/bin
OPERATOR_IMAGE_BUILD_SRC ?= true
# By default the image tag is the full version number, including the build number.
OPERATOR_IMAGE_TAG ?= $(shell jujud version | grep -E -o "^[[:digit:]]{1,9}\.[[:digit:]]{1,9}(\.|-[[:alpha:]]+)[[:digit:]]{1,9}(\.[[:digit:]]{1,9})?")
# Legacy tags never have a build number.
OPERATOR_IMAGE_TAG_LEGACY ?= $(shell jujud version | grep -E -o "^[[:digit:]]{1,9}\.[[:digit:]]{1,9}(\.|-[[:alpha:]]+)[[:digit:]]{1,9}")
OPERATOR_IMAGE_PATH = ${DOCKER_USERNAME}/jujud-operator:${OPERATOR_IMAGE_TAG}
OPERATOR_IMAGE_PATH_LEGACY = ${DOCKER_USERNAME}/jujud-operator:${OPERATOR_IMAGE_TAG_LEGACY}

operator-check-build:
ifeq ($(OPERATOR_IMAGE_BUILD_SRC),true)
make install
else
@echo "skipping to build jujud bin, use existing one at ${JUJUD_BIN_DIR}/jujud."
endif

operator-image: operator-check-build
rm -rf ${JUJUD_STAGING_DIR}
mkdir ${JUJUD_STAGING_DIR}
cp ${JUJUD_BIN_DIR}/jujud ${JUJUD_STAGING_DIR}
cp caas/jujud-operator-dockerfile ${JUJUD_STAGING_DIR}
cp caas/jujud-operator-requirements.txt ${JUJUD_STAGING_DIR}
docker build -f ${JUJUD_STAGING_DIR}/jujud-operator-dockerfile -t ${OPERATOR_IMAGE_PATH} ${JUJUD_STAGING_DIR}
ifneq ($(OPERATOR_IMAGE_PATH),$(OPERATOR_IMAGE_PATH_LEGACY))
docker tag ${OPERATOR_IMAGE_PATH} ${OPERATOR_IMAGE_PATH_LEGACY}
endif
rm -rf ${JUJUD_STAGING_DIR}

push-operator-image: operator-image
docker push ${OPERATOR_IMAGE_PATH}
ifneq ($(OPERATOR_IMAGE_PATH),$(OPERATOR_IMAGE_PATH_LEGACY))
docker push ${OPERATOR_IMAGE_PATH_LEGACY}
endif

microk8s-operator-update: operator-image
docker save ${OPERATOR_IMAGE_PATH} | microk8s.ctr --namespace k8s.io image import -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (a *API) OperatorProvisioningInfo() (params.OperatorProvisioningInfo, error
fmt.Sprintf("agent version is missing in model config %q", modelConfig.Name()),
)
}
vers.Build = 0

imagePath := podcfg.GetJujuOCIImagePath(cfg, vers)
storageClassName, _ := modelConfig.AllAttrs()[provider.OperatorStorageKey].(string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ func (f *Facade) provisioningInfo(model Model, tagString string) (*params.Kubern
fmt.Sprintf("agent version is missing in model config %q", modelConfig.Name()),
)
}
vers.Build = 0
operatorImagePath := podcfg.GetJujuOCIImagePath(controllerCfg, vers)

filesystemParams, err := f.applicationFilesystemParams(app, controllerCfg, modelConfig)
Expand Down
1 change: 0 additions & 1 deletion cloudconfig/podcfg/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func tagImagePath(path string, ver version.Number) string {
verString = splittedPath[1]
}
if ver != version.Zero {
ver.Build = 0
verString = ver.String()
}
if verString != "" {
Expand Down
3 changes: 3 additions & 0 deletions cmd/supercommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type versionDetail struct {
GitTreeState string `json:"git-tree-state,omitempty" yaml:"git-tree-state,omitempty"`
// Compiler reported by runtime.Compiler
Compiler string `json:"compiler" yaml:"compiler"`
// Build is a monotonic integer set by Jenkins.
Build int `json:"build,omitempty" yaml:"build,omitempty"`
}

// NewSuperCommand is like cmd.NewSuperCommand but
Expand All @@ -64,6 +66,7 @@ func NewSuperCommand(p cmd.SuperCommandParams) *cmd.SuperCommand {
GitCommit: jujuversion.GitCommit,
GitTreeState: jujuversion.GitTreeState,
Compiler: jujuversion.Compiler,
Build: jujuversion.Build,
}

// p.Version should be a version.Binary, but juju/cmd does not
Expand Down
29 changes: 28 additions & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

semversion "github.com/juju/version"
Expand All @@ -33,10 +34,17 @@ const (
// The version that we switched over from old style numbering to new style.
var switchOverVersion = semversion.MustParse("1.19.9")

// build is injected by Jenkins, it must be an integer or empty.
var build string

// Build is a monotonic number injected by Jenkins. It is added to the Current version only when
// the version is a development build (see IsDev(1))
var Build = mustParseBuildInt(build)

// Current gives the current version of the system. If the file
// "FORCE-VERSION" is present in the same directory as the running
// binary, it will override this.
var Current = semversion.MustParse(version)
var Current = mustParseVersion(version, build)

// Compiler is the go compiler used to build the binary.
var Compiler = runtime.Compiler
Expand Down Expand Up @@ -76,3 +84,22 @@ func IsDev(v semversion.Number) bool {
}
return v.Tag != "" || v.Build > 0
}

func mustParseVersion(versionStr string, buildInt string) semversion.Number {
ver := semversion.MustParse(versionStr)
if IsDev(ver) {
ver.Build = mustParseBuildInt(buildInt)
}
return ver
}

func mustParseBuildInt(buildInt string) int {
if buildInt == "" {
return 0
}
i, err := strconv.Atoi(buildInt)
if err != nil {
panic(err)
}
return i
}

0 comments on commit 7e2e3ee

Please sign in to comment.