Browse Source

Build System Fixes for Go Modules (#5655)

* Docker based builds can now use the module cache

* Simplify building the consul-dev docker image.

* Make sure to pull the latest consul image.

* Allow selecting base image version for the dev image
pull/5662/head
Matt Keeler 6 years ago committed by GitHub
parent
commit
c6be3b525b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      GNUmakefile
  2. 35
      build-support/docker/Consul-Dev.dockerfile
  3. 34
      build-support/functions/10-util.sh
  4. 34
      build-support/functions/20-build.sh

8
GNUmakefile

@ -44,6 +44,7 @@ CONSUL_DEV_IMAGE?=consul-dev
GO_BUILD_TAG?=consul-build-go GO_BUILD_TAG?=consul-build-go
UI_BUILD_TAG?=consul-build-ui UI_BUILD_TAG?=consul-build-ui
BUILD_CONTAINER_NAME?=consul-builder BUILD_CONTAINER_NAME?=consul-builder
CONSUL_IMAGE_VERSION?=latest
DIST_TAG?=1 DIST_TAG?=1
DIST_BUILD?=1 DIST_BUILD?=1
@ -114,8 +115,11 @@ dev: changelogfmt dev-build
dev-build: dev-build:
@$(SHELL) $(CURDIR)/build-support/scripts/build-local.sh -o $(GOOS) -a $(GOARCH) @$(SHELL) $(CURDIR)/build-support/scripts/build-local.sh -o $(GOOS) -a $(GOARCH)
dev-docker: go-build-image dev-docker: linux
@docker build -t '$(CONSUL_DEV_IMAGE)' --build-arg 'GIT_COMMIT=$(GIT_COMMIT)' --build-arg 'GIT_DIRTY=$(GIT_DIRTY)' --build-arg 'GIT_DESCRIBE=$(GIT_DESCRIBE)' --build-arg 'CONSUL_BUILD_IMAGE=$(GO_BUILD_TAG)' -f $(CURDIR)/build-support/docker/Consul-Dev.dockerfile '$(CURDIR)' @echo "Pulling consul container image - $(CONSUL_IMAGE_VERSION)"
@docker pull consul:$(CONSUL_IMAGE_VERSION) >/dev/null
@echo "Building Consul Development container - $(CONSUL_DEV_IMAGE)"
@docker build $(NOCACHE) $(QUIET) -t '$(CONSUL_DEV_IMAGE)' --build-arg CONSUL_IMAGE_VERSION=$(CONSUL_IMAGE_VERSION) $(CURDIR)/pkg/bin/linux_amd64 -f $(CURDIR)/build-support/docker/Consul-Dev.dockerfile
changelogfmt: changelogfmt:
@echo "--> Making [GH-xxxx] references clickable..." @echo "--> Making [GH-xxxx] references clickable..."

35
build-support/docker/Consul-Dev.dockerfile

@ -1,32 +1,3 @@
ARG CONSUL_BUILD_IMAGE ARG CONSUL_IMAGE_VERSION=latest
FROM ${CONSUL_BUILD_IMAGE}:latest as builder FROM consul:${CONSUL_IMAGE_VERSION}
# FROM golang:latest as builder COPY consul /go
ARG GIT_COMMIT
ARG GIT_DIRTY
ARG GIT_DESCRIBE
# WORKDIR /go/src/github.com/hashicorp/consul
ENV CONSUL_DEV=1
ENV COLORIZE=0
# Cache modules separately from more frequently edited source files.
#
# The trick is taken from [https://medium.com/@pliutau/docker-and-go-modules-4265894f9fc#6622]
#
# We copy the modules files in first since they are less likely to change frequently
# and the population of the go mod cache will be invalidated less frequently.
COPY go.mod .
COPY go.sum .
RUN mkdir -p api sdk
COPY api/go.mod api
COPY api/go.sum api
COPY sdk/go.mod sdk
COPY sdk/go.sum sdk
RUN go mod download
# Add the rest of the code.
ADD . /consul/
RUN make dev
FROM consul:latest
COPY --from=builder /go/bin/consul /bin

34
build-support/functions/10-util.sh

@ -959,7 +959,7 @@ function shasum_directory {
return $ret return $ret
} }
function ui_version { function ui_version {
# Arguments: # Arguments:
# $1 - path to index.html # $1 - path to index.html
# #
@ -977,8 +977,9 @@ function shasum_directory {
local ui_version="$(grep '<!-- CONSUL_VERSION: .* -->$' "$1" | sed 's/^<!-- CONSUL_VERSION: \(.*\) -->$/\1/')" || return 1 local ui_version="$(grep '<!-- CONSUL_VERSION: .* -->$' "$1" | sed 's/^<!-- CONSUL_VERSION: \(.*\) -->$/\1/')" || return 1
echo "$ui_version" echo "$ui_version"
return 0 return 0
} }
function ui_logo_type {
function ui_logo_type {
# Arguments: # Arguments:
# $1 - path to index.html # $1 - path to index.html
# #
@ -1004,4 +1005,29 @@ function shasum_directory {
echo "oss" echo "oss"
fi fi
return 0 return 0
} }
function go_mod_assert {
# Returns:
# 0 - success
# * - failure
#
# Notes: will ensure all the necessary go modules are cached
# and if the CONSUL_MOD_VERIFY env var is set will force
# reverification of all modules.
if ! go mod download >/dev/null
then
err "ERROR: Failed to populate the go module cache"
return 1
fi
if is_set "${CONSUL_MOD_VERIFY}"
then
if ! go mod verify
then
err "ERROR: Failed to verify go module checksums"
return 1
fi
fi
return 0
}

34
build-support/functions/20-build.sh

@ -254,7 +254,6 @@ function build_consul {
fi fi
pushd ${sdir} > /dev/null pushd ${sdir} > /dev/null
status "Creating the Go Build Container with image: ${image_name}"
if is_set "${CONSUL_DEV}" if is_set "${CONSUL_DEV}"
then then
if test -z "${XC_OS}" if test -z "${XC_OS}"
@ -275,7 +274,38 @@ function build_consul {
extra_dir="${extra_dir_name}/" extra_dir="${extra_dir_name}/"
fi fi
local container_id=$(docker create -it -e CGO_ENABLED=0 ${image_name} gox -os="${XC_OS}" -arch="${XC_ARCH}" -osarch="!darwin/arm !freebsd/arm !darwin/arm64" -ldflags "${GOLDFLAGS}" -output "pkg/bin/${extra_dir}{{.OS}}_{{.Arch}}/consul" -tags="${GOTAGS}") # figure out if the compiler supports modules
local use_modules=0
if go help modules >/dev/null 2>&1
then
use_modules=1
elif test -n "${GO111MODULE}"
then
use_modules=1
fi
local volume_mount=
if is_set "${use_modules}"
then
status "Ensuring Go modules are up to date"
# ensure our go module cache is correct
go_mod_assert || return 1
# setup to bind mount our hosts module cache into the container
volume_mount="--mount=type=bind,source=${MAIN_GOPATH}/pkg/mod,target=/go/pkg/mod"
fi
status "Creating the Go Build Container with image: ${image_name}"
local container_id=$(docker create -it \
${volume_mount} \
-e CGO_ENABLED=0 \
${image_name} \
gox \
-os="${XC_OS}" \
-arch="${XC_ARCH}" \
-osarch="!darwin/arm !freebsd/arm !darwin/arm64" \
-ldflags "${GOLDFLAGS}" \
-output "pkg/bin/${extra_dir}{{.OS}}_{{.Arch}}/consul" \
-tags="${GOTAGS}")
ret=$? ret=$?
if test $ret -eq 0 if test $ret -eq 0

Loading…
Cancel
Save