Skip to content

Commit

Permalink
Add version to CLI and RP image
Browse files Browse the repository at this point in the history
Fixes: radius-project#37

Part of: radius-project#231

This change adds version info to our binary builds and our RP image.
  • Loading branch information
rynowak committed Apr 15, 2021
1 parent 2cf0971 commit 06fbc54
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ GIT_COMMIT = $(shell git rev-list -1 HEAD)
GIT_VERSION = $(shell git describe --always --abbrev=7 --dirty)
CGO ?= 0


WEBAPP_BINARY = radius-rp
CLI_BINARY = rad

Expand Down Expand Up @@ -71,7 +70,8 @@ endif
OUT_DIR := ./dist

BINS_OUT_DIR := $(OUT_DIR)/$(GOOS)_$(GOARCH)/$(BUILDTYPE_DIR)
LDFLAGS := "-s -w -X main.version=$(REL_VERSION)"
BASE_PACKAGE_NAME := github.com/Azure/radius
LDFLAGS := "-s -w -X $(BASE_PACKAGE_NAME)/pkg/version.release=$(REL_VERSION) -X $(BASE_PACKAGE_NAME)/pkg/version.commit=$(GIT_COMMIT) -X $(BASE_PACKAGE_NAME)/pkg/version.version=$(GIT_VERSION)"
GOPATH := $(shell go env GOPATH)

ifeq (,$(shell go env GOBIN))
Expand All @@ -80,6 +80,7 @@ else
GOBIN=$(shell go env GOBIN)
endif


################################################################################
# Docker build details #
################################################################################
Expand Down Expand Up @@ -211,7 +212,12 @@ clean:
.PHONY: docker
docker:
$(info $(H) Building image as '$(DOCKER_IMAGE)')
docker build . -f ./deploy/rp/Dockerfile -t $(DOCKER_IMAGE)
docker build . \
-f ./deploy/rp/Dockerfile \
-t $(DOCKER_IMAGE) \
--build-arg LDFLAGS=$(LDFLAGS) \
--label org.opencontainers.image.version="$(REL_VERSION)" \
--label org.opencontainers.image.revision="$(GIT_COMMIT)"

################################################################################
# Target: dockerpush #
Expand Down
6 changes: 6 additions & 0 deletions cmd/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path"

"github.com/Azure/radius/pkg/version"
"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -39,6 +40,11 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Initialize support for --version
RootCmd.Version = version.Release()
template := fmt.Sprintf("Release: %s \nVersion: %s\nCommit: %s\n", version.Release(), version.Version(), version.Commit())
RootCmd.SetVersionTemplate(template)

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rad/config.yaml)")
}

Expand Down
6 changes: 4 additions & 2 deletions deploy/rp/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
ARG LDFLAGS="-w -s"

FROM golang:alpine AS build-env
RUN apk --no-cache add build-base git gcc
WORKDIR /src
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY . ./
RUN go build -o radius-rp -ldflags "-w -s" cmd/rp/main.go
RUN go build -o radius-rp -ldflags "$LD_FLAGS" cmd/rp/main.go

FROM alpine
FROM alpine
RUN apk --no-cache add ca-certificates
COPY --from=build-env /src/radius-rp ./radius-rp
EXPOSE 5000
Expand Down
28 changes: 28 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------

package version

// Values for these are injected by the build.
var (
release = "edge"
version = "edge"
commit = "unknown"
)

// Commit returns the full git SHA of the build.
func Commit() string {
return commit
}

// Release returns the semver release version of the build.
func Release() string {
return release
}

// Version returns the 'git describe' output of the build.
func Version() string {
return version
}

0 comments on commit 06fbc54

Please sign in to comment.