From e2c903c85fde8821f20259cd7978675564bd562d Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 30 Jun 2020 11:37:03 +0200 Subject: [PATCH] Set version by most recent Tag Signed-off-by: Nicolas De Loof --- ecs/Dockerfile | 7 ++++++- ecs/Makefile | 12 +++++++++++- ecs/builder.Makefile | 4 +++- ecs/cmd/commands/version.go | 9 +++++++-- ecs/tests/e2e_deploy_services_test.go | 3 +-- ecs/tests/version_test.go | 18 ++++++++++++++++++ 6 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 ecs/tests/version_test.go diff --git a/ecs/Dockerfile b/ecs/Dockerfile index 2d2213bd..9d2b0751 100644 --- a/ecs/Dockerfile +++ b/ecs/Dockerfile @@ -14,12 +14,14 @@ RUN apk add --no-cache \ COPY go.* . RUN --mount=type=cache,target=/go/pkg/mod \ go mod download -COPY . . FROM base AS make-plugin ARG TARGETOS ARG TARGETARCH 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 \ --mount=type=cache,target=/go/pkg/mod \ GOOS=${TARGETOS} \ @@ -27,6 +29,9 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ make -f builder.Makefile build FROM base AS make-cross +ARG COMMIT +ARG TAG +COPY . . RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg/mod \ make -f builder.Makefile cross diff --git a/ecs/Makefile b/ecs/Makefile index 22dd3604..cf9c48cc 100644 --- a/ecs/Makefile +++ b/ecs/Makefile @@ -3,21 +3,31 @@ PWD=$(shell pwd) export DOCKER_BUILDKIT=1 +COMMIT := $(shell git rev-parse --short HEAD) +TAG := $(shell git describe --tags --dirty --match "v*") + .DEFAULT_GOAL := build build: ## Build for the current @docker build . \ --output ./dist \ --platform ${PLATFORM} \ + --build-arg COMMIT=${COMMIT} \ + --build-arg TAG=${TAG} \ --target build cross: ## Cross build for linux, macos and windows @docker build . \ --output ./dist \ + --build-arg COMMIT=${COMMIT} \ + --build-arg TAG=${TAG} \ --target cross test: build ## Run tests - @docker build . --target test + @docker build . \ + --build-arg COMMIT=${COMMIT} \ + --build-arg TAG=${TAG} \ + --target test e2e: build ## Run tests go test ./... -v -tags=e2e diff --git a/ecs/builder.Makefile b/ecs/builder.Makefile index 18bd2da2..4bdf2789 100644 --- a/ecs/builder.Makefile +++ b/ecs/builder.Makefile @@ -7,7 +7,9 @@ ifeq ($(GOOS),windows) endif 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) BINARY=dist/docker-ecs diff --git a/ecs/cmd/commands/version.go b/ecs/cmd/commands/version.go index d3b44687..58e7ccab 100644 --- a/ecs/cmd/commands/version.go +++ b/ecs/cmd/commands/version.go @@ -6,14 +6,19 @@ import ( "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 { return &cobra.Command{ Use: "version", Short: "Show version.", 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 }, } diff --git a/ecs/tests/e2e_deploy_services_test.go b/ecs/tests/e2e_deploy_services_test.go index e55fc93b..32b6dab5 100644 --- a/ecs/tests/e2e_deploy_services_test.go +++ b/ecs/tests/e2e_deploy_services_test.go @@ -6,10 +6,9 @@ import ( "context" "testing" - "github.com/docker/ecs-plugin/pkg/amazon/sdk" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" + "github.com/docker/ecs-plugin/pkg/amazon/sdk" "github.com/docker/ecs-plugin/pkg/docker" "gotest.tools/v3/assert" "gotest.tools/v3/fs" diff --git a/ecs/tests/version_test.go b/ecs/tests/version_test.go new file mode 100644 index 00000000..ad4d6455 --- /dev/null +++ b/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")) +}