Set version by most recent Tag

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-06-30 11:37:03 +02:00
parent d2911c1ea9
commit e2c903c85f
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
6 changed files with 46 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
},
}

View File

@ -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"

18
ecs/tests/version_test.go Normal file
View File

@ -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"))
}