Skip to content

Commit 5732e53

Browse files
authoredOct 13, 2017
Enhance Dockerfile for local building (#1002)
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
1 parent d5ef1a9 commit 5732e53

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed
 

‎Dockerfile

+17-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,27 @@ FROM golang:1.7.6
2828
#ENV https_proxy ""
2929
ARG http_proxy
3030
ARG https_proxy
31-
ENV GOPATH=/go/ NET_CONTAINER_BUILD=1
31+
32+
WORKDIR /go/src/github.com/contiv/netplugin/
3233

3334
ENTRYPOINT ["netplugin"]
3435
CMD ["--help"]
3536

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

38-
WORKDIR /go/src/github.com/contiv/netplugin/
41+
RUN GOGC=1500 go install -ldflags "-s -w" $(go list ./vendor/...)
42+
43+
# build the netplugin binaries
44+
COPY ./ /go/src/github.com/contiv/netplugin/
3945

40-
RUN make build
46+
ARG BUILD_VERSION=""
47+
ARG NIGHTLY_RELEASE=""
4148

49+
RUN GOPATH=/go/ \
50+
BUILD_VERSION="${BUILD_VERSION}" \
51+
NIGHTLY_RELEASE="${NIGHTLY_RELEASE}" \
52+
make compile \
53+
&& cp scripts/contrib/completion/bash/netctl /etc/bash_completion.d/netctl \
54+
&& netplugin -version

‎Makefile

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

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

45
DEFAULT_DOCKER_VERSION := 1.12.6
56
SHELL := /bin/bash
@@ -86,23 +87,26 @@ endif
8687

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

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

96+
# fully prepares code for pushing to branch, includes building binaries
97+
run-build: deps checks clean compile
98+
99+
compile-with-docker:
100+
docker build \
101+
--build-arg NIGHTLY_RELEASE=${NIGHTLY_RELEASE} \
102+
--build-arg BUILD_VERSION=${BUILD_VERSION} \
103+
-t netplugin:$${BUILD_VERSION:-devbuild}-$$(./scripts/getGitCommit.sh) .
104+
95105
build-docker-image: start
96106
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"'
97107

98-
99-
ifdef NET_CONTAINER_BUILD
100-
install-shell-completion:
101-
cp scripts/contrib/completion/bash/netctl /etc/bash_completion.d/netctl
102-
else
103108
install-shell-completion:
104109
sudo cp scripts/contrib/completion/bash/netctl /etc/bash_completion.d/netctl
105-
endif
106110

107111
build: start ssh-build stop
108112

@@ -116,12 +120,8 @@ update:
116120

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

126126
# ===================================================================
127127
#kubernetes demo targets
@@ -195,26 +195,17 @@ mesos-cni-destroy:
195195
demo-ubuntu:
196196
CONTIV_NODE_OS=ubuntu make demo
197197

198-
ifdef NET_CONTAINER_BUILD
199-
stop:
200-
else
201198
stop:
202199
CONTIV_NODES=$${CONTIV_NODES:-3} vagrant destroy -f
203-
endif
204200

205201
demo: ssh-build
206202
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"'
207203

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

211-
ifdef NET_CONTAINER_BUILD
212-
ssh-build:
213-
cd /go/src/github.com/contiv/netplugin && make run-build install-shell-completion
214-
else
215207
ssh-build: start
216208
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"'
217-
endif
218209

219210
unit-test: stop clean
220211
./scripts/unittests -vagrant

‎scripts/build.sh

+2-10
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ else
1515
BUILD_VERSION="$VERSION-$BUILD_TIME"
1616
fi
1717

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

2820
echo $BUILD_VERSION >$VERSION_FILE
2921

@@ -32,4 +24,4 @@ GOGC=1500 go install \
3224
-X $PKG_NAME.buildTime=$BUILD_TIME \
3325
-X $PKG_NAME.gitCommit=$GIT_COMMIT \
3426
-s -w" \
35-
-v $TO_BUILD
27+
$TO_BUILD

‎scripts/getGitCommit.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
if command -v git &>/dev/null && git rev-parse &>/dev/null; then
6+
GIT_COMMIT=$(git rev-parse --short HEAD)
7+
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
8+
GIT_COMMIT="$GIT_COMMIT-unsupported"
9+
fi
10+
echo $GIT_COMMIT
11+
exit 0
12+
fi
13+
echo >&2 'error: unable to determine the git revision'
14+
exit 1

0 commit comments

Comments
 (0)
Please sign in to comment.