Skip to content

Commit

Permalink
Build binary within docker and make a docker image (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
stlava authored Apr 5, 2019
1 parent e3eb4c4 commit 39e1522
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 112 deletions.
44 changes: 13 additions & 31 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,42 @@ defaults_itests: &defaults_itests
docker_layer_caching: true

step_library:
- &step_restore_vendor_cache
restore_cache:
keys:
- vendor-{{ checksum "Gopkg.lock" }}

- &step_save_vendor_cache
save_cache:
key: vendor-{{ checksum "Gopkg.lock" }}
paths:
- vendor

- &step_test
run:
name: Run go vet and tests
command: |
CI=true make test
- &step_itests
run:
name: Run integration tests in docker
command: |
git submodule sync
git submodule update --init || (rm -fr .git/config .git/modules && git submodule deinit -f . && git submodule update --init)
CI=true make itests-circleci
make itests-circleci
- &step_build_artifact
run:
name: Run make build
command: |
make build
# builds pg-bifrost in a docker container
make docker_build
# Save the docker container for re-use
mkdir -p docker-cache
docker save -o docker-cache/built-image.tar pg-bifrost:latest
# pull binary out of docker container
make docker_get_binary
/usr/bin/md5sum -b target/pg-bifrost | cut -d' ' -f1 > target/pg-bifrost.md5
cat target/pg-bifrost.md5
/usr/bin/sha1sum -b target/pg-bifrost | cut -d' ' -f1 > target/pg-bifrost.sha1
cat target/pg-bifrost.sha1
/usr/bin/sha256sum -b target/pg-bifrost | cut -d' ' -f1 > target/pg-bifrost.sha256
cat target/pg-bifrost.sha256
- &step_build_docker_containers
run:
name: Building integration test docker containers
command: |
make docker
jobs:
build: &build
<<: *defaults
steps:
- checkout

- *step_restore_vendor_cache
- setup_remote_docker:
docker_layer_caching: true
- *step_build_artifact
- *step_test
- *step_save_vendor_cache
- persist_to_workspace:
root: /go/src/github.com/Nextdoor
paths:
Expand All @@ -84,7 +66,7 @@ jobs:
sudo curl -L https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz -o go1.11.4.linux-amd64.tar.gz
sudo tar -xf go1.11.4.linux-amd64.tar.gz
sudo mv go /usr/local
- *step_build_docker_containers
sudo docker load < docker-cache/built-image.tar
- *step_itests
- store_test_results:
path: ./itests/test_output/
Expand Down
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
itests
.idea
.circleci
target
README.md
docs
docker-cache
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ _vendor-*/

# exclude build files
target/
itests/containers/pg-bifrost/app/pg-bifrost

# exclude itest outputs
itests/tests/*/output/*
Expand Down
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Test and build binary
FROM golang:1.11.4-stretch as intermediate

# Build dependencies
RUN go get github.com/golang/mock/gomock
RUN go install github.com/golang/mock/mockgen
RUN go get github.com/golang/dep/cmd/dep
RUN go install github.com/golang/dep/cmd/dep

WORKDIR /go/src/github.com/Nextdoor/pg-bifrost.git/

# Copy over gopkg and get deps. This will ensure that
# we don't get the deps each time but only when the files
# change.
COPY Gopkg.lock Gopkg.toml ./
RUN dep ensure --vendor-only

COPY . .

# The CI flag is used to control the auto generation of
# code from interfaces (running go generate). In dev we
# want that to happen automatically but in the CI build
# we only want to use the code that was checked in. When
# CI=true generate is not run.
ARG is_ci
ENV CI=$is_ci

# Run tests and make the binary
RUN make test && make build

# Package binary in a scratch container
FROM scratch
COPY --from=intermediate /go/src/github.com/Nextdoor/pg-bifrost.git/target/pg-bifrost /
CMD ["/pg-bifrost"]
33 changes: 23 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

GO_LDFLAGS ?= -w -extldflags "-static"

GIT_REVISION := $(shell git rev-parse --short HEAD)
GIT_TAG_VERSION := $(shell git tag -l --points-at HEAD)

ifeq ($(CI),true)
GO_TEST_EXTRAS ?= "-coverprofile=c.out"
GIT_REVISION := $(shell git rev-parse --short HEAD)
VERSION := $(shell git tag -l --points-at HEAD)
GO_LDFLAGS += -X main.GitRevision=$(GIT_REVISION) -X main.Version=$(VERSION)
GO_LDFLAGS += -X main.GitRevision=$(GIT_REVISION) -X main.Version=$(GIT_TAG_VERSION)
endif

vendor:
dep ensure
dep ensure --vendor-only

vet:
@echo "Running go vet ..."
Expand All @@ -29,15 +30,13 @@ test: vendor generate vet

itests-circleci:
@echo "Running integration tests"
TEST_NAME=test_basic docker-compose -f itests/docker-compose.yml build
cd ./itests && ./circleci_split_itests.sh

itests:
@echo "Running integration tests"
cd ./itests && ./integration_tests.bats -r tests

docker:
@echo "Building docker integration test images"
TEST_NAME=test_basic docker-compose -f itests/docker-compose.yml build
cd ./itests && ./integration_tests.bats -r tests

clean:
@echo "Removing vendor deps"
Expand All @@ -53,6 +52,20 @@ build: vendor generate
@echo "Creating GO binary"
mkdir -p target
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(GO_LDFLAGS)" -o target/pg-bifrost github.com/Nextdoor/pg-bifrost.git/main
ln -f target/pg-bifrost itests/containers/pg-bifrost/app/pg-bifrost

.PHONY: clean test itests
# Standard settings that will be used later
DOCKER := $(shell which docker)

docker_build:
@echo "Building pg-bifrost docker image"
@$(DOCKER) build -t "pg-bifrost:latest" --build-arg is_ci="${CI}" .

docker_get_binary:
@echo "Copying binary from docker image"
mkdir -p target
@$(DOCKER) rm "pg-bifrost-build" || true
@$(DOCKER) create --name "pg-bifrost-build" "pg-bifrost:latest" /pg-bifrost
@$(DOCKER) cp "pg-bifrost-build":/pg-bifrost target/
@$(DOCKER) rm "pg-bifrost-build"

.PHONY: clean test itests docker_build docker_get_binary
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,8 @@ The integration tests are setup and run with:
git submodule sync
git submodule update --init
# Build binary
make build
# Create docker images
make docker
# Build binary inside a docker container
make docker_build
# Run the integration tests
make itests
Expand All @@ -172,19 +169,13 @@ make itests
Example:

```
[Slava pg-bifrost.git] $ make build
Running go generate ...
Creating GO binary
mkdir -p target
GOOS=linux GOARCH=amd64 go build -o target/pg-bifrost github.com/Nextdoor/pg-bifrost.git/main
ln -f target/pg-bifrost itests/containers/pg-bifrost/app/pg-bifrost
[Slava pg-bifrost.git] $ make docker
Building docker integration test images
TEST_NAME=test_basic docker-compose -f itests/docker-compose.yml build
Building bifrost
Step 1/3 : FROM alpine:3.8
...
[Slava pg-bifrost.git] $ make docker_build
Building pg-bifrost docker image
Sending build context to Docker daemon 16.78MB
Step 1/15 : FROM golang:1.11.4-stretch as intermediate
---> dd46c1256829
...
[Slava pg-bifrost.git] $ make itests
Running integration tests
Expand Down
4 changes: 0 additions & 4 deletions itests/containers/pg-bifrost/Dockerfile

This file was deleted.

Empty file.
4 changes: 2 additions & 2 deletions itests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:

bifrost:
container_name: bifrost
build: containers/pg-bifrost
image: pg-bifrost:latest
env_file:
- "./containers/defaults.env"
- "./tests/${TEST_NAME}/envfile.env"
Expand All @@ -37,7 +37,7 @@ services:
aliases:
- kinesis
- postgres
command: /app/pg-bifrost replicate kinesis
command: /pg-bifrost replicate kinesis

kinesis-poller:
container_name: kinesis-poller
Expand Down
Loading

0 comments on commit 39e1522

Please sign in to comment.