Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
build / release makeover (#766)
Browse files Browse the repository at this point in the history
* track vendored code

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* containerize devx tasks, multistage docker builds, etc.

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* cleanup brigade.js

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* fix container command prefix for some targets

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* add convenience targets for contributors to build for the OS of their choosing

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* use stable, semver tagged brigade-github-check-run image

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* document build-<OS> target

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>

* sub familialr GOOS env var for ambiguous OS env var

Signed-off-by: Kent Rancourt <kent.rancourt@microsoft.com>
  • Loading branch information
Kent Rancourt authored and technosophos committed Jun 14, 2019
1 parent cd78023 commit bf4df3a
Show file tree
Hide file tree
Showing 2,925 changed files with 990,304 additions and 332 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
@@ -0,0 +1,4 @@
*
!cmd/
!pkg/
!vendor/
1 change: 0 additions & 1 deletion .gitignore
@@ -1,5 +1,4 @@
bin/
vendor/
.vimrc
*.exe
*.cnab
Expand Down
16 changes: 11 additions & 5 deletions Dockerfile
@@ -1,7 +1,13 @@
FROM alpine:3.8
FROM quay.io/deis/lightweight-docker-go:v0.7.0
ARG LDFLAGS
ENV CGO_ENABLED=0
WORKDIR /go/src/github.com/deislabs/duffle
COPY cmd/ cmd/
COPY pkg/ pkg/
COPY vendor/ vendor/
RUN go build -ldflags "$LDFLAGS" -o bin/duffle ./cmd/...

FROM alpine:3.8
RUN apk add --no-cache bash make jq ca-certificates && update-ca-certificates

COPY bin/duffle /usr/bin/duffle

CMD /usr/bin/duffle
COPY --from=0 /go/src/github.com/deislabs/duffle/bin/duffle /usr/bin/duffle
CMD /usr/bin/duffle
205 changes: 118 additions & 87 deletions Makefile
@@ -1,101 +1,132 @@
PROJECT := duffle
ORG := deislabs
DOCKER_REGISTRY ?= $(ORG)
BINDIR := $(CURDIR)/bin
GOFLAGS :=
LDFLAGS := -w -s
TESTFLAGS :=
INSTALL_DIR := /usr/local/bin

ifeq ($(OS),Windows_NT)
TARGET = $(PROJECT).exe
SHELL = cmd.exe
CHECK = where.exe
else
TARGET = $(PROJECT)
SHELL ?= bash
CHECK ?= which
endif
SHELL ?= /bin/bash

GIT_TAG := $(shell git describe --tags --always)
VERSION ?= ${GIT_TAG}
# Replace + with -, for Docker image tag compliance
IMAGE_TAG ?= $(subst +,-,$(VERSION))
LDFLAGS += -X github.com/$(ORG)/$(PROJECT)/pkg/version.Version=$(VERSION)
.DEFAULT_GOAL := build

.PHONY: default
default: build
################################################################################
# Version details #
################################################################################

.PHONY: build
build:
go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(TARGET) github.com/$(ORG)/$(PROJECT)/cmd/...

.PHONY: install
install:
install $(BINDIR)/$(TARGET) $(INSTALL_DIR)

CX_OSES = linux windows darwin
CX_ARCHS = amd64

.PHONY: build-release
build-release:
@for os in $(CX_OSES); do \
echo "building $$os"; \
for arch in $(CX_ARCHS); do \
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(PROJECT)-$$os-$$arch github.com/$(ORG)/$(PROJECT)/cmd/...; \
done; \
if [ $$os = 'windows' ]; then \
mv $(BINDIR)/$(PROJECT)-$$os-$$arch $(BINDIR)/$(PROJECT)-$$os-$$arch.exe; \
fi; \
done

.PHONY: debug
debug:
go build $(GOFLAGS) -o $(BINDIR)/$(TARGET) github.com/$(ORG)/$(PROJECT)/cmd/...

.PHONY: build-docker-bin
build-docker-bin:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build $(GOFLAGS) -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(TARGET) github.com/$(ORG)/$(PROJECT)/cmd/...

.PHONY: docker-build
docker-build:
docker build -t $(DOCKER_REGISTRY)/$(PROJECT):$(IMAGE_TAG) .

.PHONY: docker-push
docker-push:
docker push $(DOCKER_REGISTRY)/$(PROJECT):$(IMAGE_TAG)
# This will reliably return the short SHA1 of HEAD or, if the working directory
# is dirty, will return that + "-dirty"
GIT_VERSION = $(shell git describe --always --abbrev=7 --dirty --match=NeVeRmAtCh)

.PHONY: test
test:
go test $(TESTFLAGS) ./...
################################################################################
# Go build details #
################################################################################

.PHONY: lint
lint:
golangci-lint run --config ./golangci.yml
BASE_PACKAGE_NAME := github.com/deislabs/duffle

HAS_DEP := $(shell $(CHECK) dep)
HAS_GOLANGCI := $(shell $(CHECK) golangci-lint)
HAS_GOIMPORTS := $(shell $(CHECK) goimports)
GOLANGCI_VERSION := v1.16.0
################################################################################
# Containerized development environment-- or lack thereof #
################################################################################

.PHONY: build-drivers
build-drivers:
cp drivers/azure-vm/$(PROJECT)-azvm.sh bin/$(PROJECT)-azvm
cd drivers/azure-vm && pip3 install -r requirements.txt
ifneq ($(SKIP_DOCKER),true)
PROJECT_ROOT := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
DEV_IMAGE := quay.io/deis/lightweight-docker-go:v0.7.0
DOCKER_CMD := docker run \
-it \
--rm \
-e SKIP_DOCKER=true \
-v $(PROJECT_ROOT):/go/src/$(BASE_PACKAGE_NAME) \
-w /go/src/$(BASE_PACKAGE_NAME) $(DEV_IMAGE)
endif

################################################################################
# Binaries and Docker images we build and publish #
################################################################################

.PHONY: bootstrap
bootstrap:
ifndef HAS_DEP
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
ifdef DOCKER_REGISTRY
DOCKER_REGISTRY := $(DOCKER_REGISTRY)/
endif
ifndef HAS_GOLANGCI
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_VERSION)

ifdef DOCKER_ORG
DOCKER_ORG := $(DOCKER_ORG)/
endif
ifndef HAS_GOIMPORTS
go get -u golang.org/x/tools/cmd/goimports

BASE_IMAGE_NAME := duffle

ifdef VERSION
MUTABLE_DOCKER_TAG := latest
else
VERSION := $(GIT_VERSION)
MUTABLE_DOCKER_TAG := edge
endif
dep ensure -vendor-only -v

LDFLAGS := -w -s -X $(BASE_PACKAGE_NAME)/pkg/version.Version=$(VERSION)

IMAGE_NAME := $(DOCKER_REGISTRY)$(DOCKER_ORG)$(BASE_IMAGE_NAME):$(VERSION)
MUTABLE_IMAGE_NAME := $(DOCKER_REGISTRY)$(DOCKER_ORG)$(BASE_IMAGE_NAME):$(MUTABLE_DOCKER_TAG)

################################################################################
# Utility targets #
################################################################################

.PHONY: dep
dep:
$(DOCKER_CMD) dep ensure -v

.PHONY: goimports
goimports:
find . -name "*.go" | fgrep -v vendor/ | xargs goimports -w -local github.com/deislabs/duffle
$(DOCKER_CMD) sh -c "find . -name \"*.go\" | fgrep -v vendor/ | xargs goimports -w -local github.com/deislabs/duffle"

.PHONY: build-drivers
build-drivers:
mkdir -p bin
cp drivers/azure-vm/duffle-azvm.sh bin/duffle-azvm
cd drivers/azure-vm && pip3 install -r requirements.txt

################################################################################
# Tests #
################################################################################

# Verifies there are no discrepancies between desired dependencies and the
# tracked, vendored dependencies
.PHONY: verify-vendored-code
verify-vendored-code:
$(DOCKER_CMD) dep check

.PHONY: lint
lint:
$(DOCKER_CMD) golangci-lint run --config ./golangci.yml

.PHONY: test
test:
$(DOCKER_CMD) go test -v ./...

################################################################################
# Build / Publish #
################################################################################

.PHONY: build
build: build-all-bins build-image

.PHONY: build-all-bins
build-all-bins:
$(DOCKER_CMD) bash -c "LDFLAGS=\"$(LDFLAGS)\" scripts/build.sh"

# You can make this target build for a specific OS and architecture using GOOS
# and GOARCH environment variables.
.PHONY: build-bin
build-bin:
$(DOCKER_CMD) bash -c "GOOS=\"$(GOOS)\" GOARCH=\"$(GOARCH)\" LDFLAGS=\"$(LDFLAGS)\" scripts/build.sh"

# This target is for contributor convenience.
.PHONY: build-%
build-%:
$(DOCKER_CMD) bash -c "GOOS=$* LDFLAGS=\"$(LDFLAGS)\" scripts/build.sh"

.PHONY: build-image
build-image:
docker build \
-t $(IMAGE_NAME) \
--build-arg LDFLAGS='$(LDFLAGS)' \
.
docker tag $(IMAGE_NAME) $(MUTABLE_IMAGE_NAME)

.PHONY: push
push: push-image

.PHONY: push-image
push-image:
docker push $(IMAGE_NAME)
docker push $(MUTABLE_IMAGE_NAME)

0 comments on commit bf4df3a

Please sign in to comment.