Skip to content

Commit

Permalink
Enhance Dockerfile for local building (#1002)
Browse files Browse the repository at this point in the history
Added a caching layer in the docker build for compiling all of the
vendor dependencies, drops compilation time to about 15s

Added support in Dockerfile for passing BUILD_VERSION and
NIGHTLY_RELEASE to build.sh

Add compile-with-docker target

Split out how we get the git commit SHA so it's reusable for docker
image tag
  • Loading branch information
chrisplo committed Oct 13, 2017
1 parent d5ef1a9 commit 5732e53
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
21 changes: 17 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ FROM golang:1.7.6
#ENV https_proxy ""
ARG http_proxy
ARG https_proxy
ENV GOPATH=/go/ NET_CONTAINER_BUILD=1

WORKDIR /go/src/github.com/contiv/netplugin/

ENTRYPOINT ["netplugin"]
CMD ["--help"]

COPY ./ /go/src/github.com/contiv/netplugin/
# by far, most of the compilation time is building vendor packages
# build the vendor dependencies as a separate docker caching layer
COPY ./vendor/ /go/src/github.com/contiv/netplugin/vendor/

WORKDIR /go/src/github.com/contiv/netplugin/
RUN GOGC=1500 go install -ldflags "-s -w" $(go list ./vendor/...)

# build the netplugin binaries
COPY ./ /go/src/github.com/contiv/netplugin/

RUN make build
ARG BUILD_VERSION=""
ARG NIGHTLY_RELEASE=""

RUN GOPATH=/go/ \
BUILD_VERSION="${BUILD_VERSION}" \
NIGHTLY_RELEASE="${NIGHTLY_RELEASE}" \
make compile \
&& cp scripts/contrib/completion/bash/netctl /etc/bash_completion.d/netctl \
&& netplugin -version
33 changes: 12 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

.PHONY: all all-CI build clean default unit-test release tar checks go-version gofmt-src golint-src govet-src
.PHONY: all all-CI build clean default unit-test release tar checks go-version gofmt-src \
golint-src govet-src run-build compile-with-docker

DEFAULT_DOCKER_VERSION := 1.12.6
SHELL := /bin/bash
Expand Down Expand Up @@ -86,23 +87,26 @@ endif

checks: go-version gofmt-src golint-src govet-src misspell-src

run-build: deps checks clean
compile:
cd $(GOPATH)/src/github.com/contiv/netplugin && \
NIGHTLY_RELEASE=${NIGHTLY_RELEASE} BUILD_VERSION=${BUILD_VERSION} \
TO_BUILD="${TO_BUILD}" VERSION_FILE=${VERSION_FILE} \
scripts/build.sh

# fully prepares code for pushing to branch, includes building binaries
run-build: deps checks clean compile

compile-with-docker:
docker build \
--build-arg NIGHTLY_RELEASE=${NIGHTLY_RELEASE} \
--build-arg BUILD_VERSION=${BUILD_VERSION} \
-t netplugin:$${BUILD_VERSION:-devbuild}-$$(./scripts/getGitCommit.sh) .

build-docker-image: start
vagrant ssh netplugin-node1 -c 'bash -lc "source /etc/profile.d/envvar.sh && cd /opt/gopath/src/github.com/contiv/netplugin && make host-build-docker-image"'


ifdef NET_CONTAINER_BUILD
install-shell-completion:
cp scripts/contrib/completion/bash/netctl /etc/bash_completion.d/netctl
else
install-shell-completion:
sudo cp scripts/contrib/completion/bash/netctl /etc/bash_completion.d/netctl
endif

build: start ssh-build stop

Expand All @@ -116,12 +120,8 @@ update:

# setting CONTIV_NODES=<number> while calling 'make demo' can be used to bring
# up a cluster of <number> nodes. By default <number> = 1
ifdef NET_CONTAINER_BUILD
start:
else
start:
CONTIV_DOCKER_VERSION="$${CONTIV_DOCKER_VERSION:-$(DEFAULT_DOCKER_VERSION)}" CONTIV_NODE_OS=${CONTIV_NODE_OS} vagrant up
endif

# ===================================================================
#kubernetes demo targets
Expand Down Expand Up @@ -195,26 +195,17 @@ mesos-cni-destroy:
demo-ubuntu:
CONTIV_NODE_OS=ubuntu make demo

ifdef NET_CONTAINER_BUILD
stop:
else
stop:
CONTIV_NODES=$${CONTIV_NODES:-3} vagrant destroy -f
endif

demo: ssh-build
vagrant ssh netplugin-node1 -c 'bash -lc "source /etc/profile.d/envvar.sh && cd /opt/gopath/src/github.com/contiv/netplugin && make host-restart host-swarm-restart"'

ssh:
@vagrant ssh netplugin-node1 -c 'bash -lc "cd /opt/gopath/src/github.com/contiv/netplugin/ && bash"' || echo 'Please run "make demo"'

ifdef NET_CONTAINER_BUILD
ssh-build:
cd /go/src/github.com/contiv/netplugin && make run-build install-shell-completion
else
ssh-build: start
vagrant ssh netplugin-node1 -c 'bash -lc "source /etc/profile.d/envvar.sh && cd /opt/gopath/src/github.com/contiv/netplugin && make run-build install-shell-completion"'
endif

unit-test: stop clean
./scripts/unittests -vagrant
Expand Down
12 changes: 2 additions & 10 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ else
BUILD_VERSION="$VERSION-$BUILD_TIME"
fi

if command -v git &>/dev/null && git rev-parse &>/dev/null; then
GIT_COMMIT=$(git rev-parse --short HEAD)
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
GIT_COMMIT="$GIT_COMMIT-unsupported"
fi
else
echo >&2 'error: unable to determine the git revision'
exit 1
fi
GIT_COMMIT=$(./scripts/getGitCommit.sh)

echo $BUILD_VERSION >$VERSION_FILE

Expand All @@ -32,4 +24,4 @@ GOGC=1500 go install \
-X $PKG_NAME.buildTime=$BUILD_TIME \
-X $PKG_NAME.gitCommit=$GIT_COMMIT \
-s -w" \
-v $TO_BUILD
$TO_BUILD
14 changes: 14 additions & 0 deletions scripts/getGitCommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -euo pipefail

if command -v git &>/dev/null && git rev-parse &>/dev/null; then
GIT_COMMIT=$(git rev-parse --short HEAD)
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
GIT_COMMIT="$GIT_COMMIT-unsupported"
fi
echo $GIT_COMMIT
exit 0
fi
echo >&2 'error: unable to determine the git revision'
exit 1

0 comments on commit 5732e53

Please sign in to comment.