Browse Source

Set version by most recent Tag

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 years ago
parent
commit
e2c903c85f

+ 6 - 1
ecs/Dockerfile

@@ -14,12 +14,14 @@ RUN apk add --no-cache \
 COPY go.* .
 COPY go.* .
 RUN --mount=type=cache,target=/go/pkg/mod \
 RUN --mount=type=cache,target=/go/pkg/mod \
     go mod download
     go mod download
-COPY . .
 
 
 FROM base AS make-plugin
 FROM base AS make-plugin
 ARG TARGETOS
 ARG TARGETOS
 ARG TARGETARCH
 ARG TARGETARCH
 RUN GO111MODULE=on go get github.com/golang/mock/mockgen@latest
 RUN GO111MODULE=on go get github.com/golang/mock/mockgen@latest
+ARG COMMIT
+ARG TAG
+COPY . .
 RUN --mount=type=cache,target=/root/.cache/go-build \
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
     --mount=type=cache,target=/go/pkg/mod \
     GOOS=${TARGETOS} \
     GOOS=${TARGETOS} \
@@ -27,6 +29,9 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
     make -f builder.Makefile build
     make -f builder.Makefile build
 
 
 FROM base AS make-cross
 FROM base AS make-cross
+ARG COMMIT
+ARG TAG
+COPY . .
 RUN --mount=type=cache,target=/root/.cache/go-build \
 RUN --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/go/pkg/mod \
     --mount=type=cache,target=/go/pkg/mod \
     make -f builder.Makefile cross
     make -f builder.Makefile cross

+ 11 - 1
ecs/Makefile

@@ -3,21 +3,31 @@ PWD=$(shell pwd)
 
 
 export DOCKER_BUILDKIT=1
 export DOCKER_BUILDKIT=1
 
 
+COMMIT := $(shell git rev-parse --short HEAD)
+TAG := $(shell git describe --tags --dirty --match "v*")
+
 .DEFAULT_GOAL := build
 .DEFAULT_GOAL := build
 
 
 build: ## Build for the current
 build: ## Build for the current
 	@docker build . \
 	@docker build . \
 		--output ./dist \
 		--output ./dist \
 		--platform ${PLATFORM} \
 		--platform ${PLATFORM} \
+		--build-arg COMMIT=${COMMIT} \
+		--build-arg TAG=${TAG} \
 		--target build
 		--target build
 
 
 cross: ## Cross build for linux, macos and windows
 cross: ## Cross build for linux, macos and windows
 	@docker build . \
 	@docker build . \
 		--output ./dist \
 		--output ./dist \
+		--build-arg COMMIT=${COMMIT} \
+		--build-arg TAG=${TAG} \
 		--target cross
 		--target cross
 
 
 test: build ## Run tests
 test: build ## Run tests
-	@docker build . --target test
+	@docker build . \
+		--build-arg COMMIT=${COMMIT} \
+		--build-arg TAG=${TAG} \
+        --target test
 
 
 e2e: build ## Run tests
 e2e: build ## Run tests
 	go test ./... -v -tags=e2e
 	go test ./... -v -tags=e2e

+ 3 - 1
ecs/builder.Makefile

@@ -7,7 +7,9 @@ ifeq ($(GOOS),windows)
 endif
 endif
 
 
 STATIC_FLAGS=CGO_ENABLED=0
 STATIC_FLAGS=CGO_ENABLED=0
-LDFLAGS:="-s -w"
+LDFLAGS := "-s -w \
+  -X github.com/docker/ecs-plugin/cmd/commands.GitCommit=$(COMMIT) \
+  -X github.com/docker/ecs-plugin/cmd/commands.Version=$(TAG)"
 GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
 GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
 
 
 BINARY=dist/docker-ecs
 BINARY=dist/docker-ecs

+ 7 - 2
ecs/cmd/commands/version.go

@@ -6,14 +6,19 @@ import (
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 )
 )
 
 
-const Version = "0.0.1"
+var (
+	// Version is the git tag that this was built from.
+	Version = "unknown"
+	// GitCommit is the commit that this was built from.
+	GitCommit = "unknown"
+)
 
 
 func VersionCommand() *cobra.Command {
 func VersionCommand() *cobra.Command {
 	return &cobra.Command{
 	return &cobra.Command{
 		Use:   "version",
 		Use:   "version",
 		Short: "Show version.",
 		Short: "Show version.",
 		RunE: func(cmd *cobra.Command, args []string) error {
 		RunE: func(cmd *cobra.Command, args []string) error {
-			fmt.Fprintf(cmd.OutOrStdout(), "Docker ECS plugin %s\n", Version)
+			fmt.Fprintf(cmd.OutOrStdout(), "Docker ECS plugin %s (%s)\n", Version, GitCommit)
 			return nil
 			return nil
 		},
 		},
 	}
 	}

+ 1 - 2
ecs/tests/e2e_deploy_services_test.go

@@ -6,10 +6,9 @@ import (
 	"context"
 	"context"
 	"testing"
 	"testing"
 
 
-	"github.com/docker/ecs-plugin/pkg/amazon/sdk"
-
 	"github.com/aws/aws-sdk-go/aws"
 	"github.com/aws/aws-sdk-go/aws"
 	"github.com/aws/aws-sdk-go/aws/session"
 	"github.com/aws/aws-sdk-go/aws/session"
+	"github.com/docker/ecs-plugin/pkg/amazon/sdk"
 	"github.com/docker/ecs-plugin/pkg/docker"
 	"github.com/docker/ecs-plugin/pkg/docker"
 	"gotest.tools/v3/assert"
 	"gotest.tools/v3/assert"
 	"gotest.tools/v3/fs"
 	"gotest.tools/v3/fs"

+ 18 - 0
ecs/tests/version_test.go

@@ -0,0 +1,18 @@
+package tests
+
+import (
+	"strings"
+	"testing"
+
+	"gotest.tools/v3/assert"
+	"gotest.tools/v3/icmd"
+)
+
+func TestVersionIsSet(t *testing.T) {
+	cmd, cleanup, _ := dockerCli.createTestCmd()
+	defer cleanup()
+
+	cmd.Command = dockerCli.Command("ecs", "version")
+	out := icmd.RunCmd(cmd).Assert(t, icmd.Success).Stdout()
+	assert.Check(t, !strings.Contains(out, "unknown"))
+}