build: add an option to compile in a container

This makes it possible to build on any platform that supports Linux
containers. The container image used for building is created once, or on
updating the `scripts/Dockerfile.build` and is cached afterwards.

To build the executable in a container, use `make containerized-build`
and everything will be done automatically. The executable will also be
available on the usual location.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2019-12-30 20:47:02 +01:00 committed by mergify[bot]
parent ae4d269836
commit 7381253ee0
4 changed files with 35 additions and 1 deletions

3
.gitignore vendored
View File

@ -4,6 +4,9 @@ _output
# docker build
deploy/cephcsi/image/cephcsi
# build container
.devel-container-id
# git merge files
*.orig
*.patch

View File

@ -37,6 +37,11 @@ ifeq ($(origin GOARCH), undefined)
GOARCH := $(shell go env GOARCH)
endif
SELINUX := $(shell getenforce 2>/dev/null)
ifeq ($(SELINUX),Enforcing)
SELINUX_VOL_FLAG = :z
endif
all: cephcsi
test: go-test static-check mod-check
@ -60,6 +65,16 @@ cephcsi:
if [ ! -d ./vendor ]; then (go mod tidy && go mod vendor); fi
GOOS=linux GOARCH=$(GOARCH) go build -mod vendor -a -ldflags '$(LDFLAGS)' -o _output/cephcsi ./cmd/
.PHONY: containerized-build
containerized-build: .devel-container-id
$(CONTAINER_CMD) run --rm -v $(PWD):/go/src/github.com/ceph/ceph-csi$(SELINUX_VOL_FLAG) $(CSI_IMAGE_NAME):devel make -C /go/src/github.com/ceph/ceph-csi cephcsi
# create a (cached) container image with dependencied for building cephcsi
.devel-container-id: scripts/Dockerfile.devel
[ ! -f .devel-container-id ] || $(CONTAINER_CMD) rmi $(CSI_IMAGE_NAME):devel
$(CONTAINER_CMD) build -t $(CSI_IMAGE_NAME):devel -f ./scripts/Dockerfile.devel .
$(CONTAINER_CMD) inspect -f '{{.Id}}' $(CSI_IMAGE_NAME):devel > .devel-container-id
image-cephcsi: cephcsi
cp _output/cephcsi deploy/cephcsi/image/cephcsi
chmod +x deploy/cephcsi/image/cephcsi
@ -75,3 +90,5 @@ clean:
go clean -r -x
rm -f deploy/cephcsi/image/cephcsi
rm -f _output/cephcsi
[ ! -f .devel-container-id ] || $(CONTAINER_CMD) rmi $(CSI_IMAGE_NAME):devel
$(RM) .devel-container-id

View File

@ -36,9 +36,12 @@ it is **highly** encouraged to:
### Building Ceph-CSI
To build ceph-csi run:
To build ceph-csi locally run:
`$ make`
To build ceph-csi in a container:
`$ make containerized-build`
The built binary will be present under `_output/` directory.
### Code contribution workflow

11
scripts/Dockerfile.devel Normal file
View File

@ -0,0 +1,11 @@
FROM ceph/ceph:v14
ENV GOPATH=/go
RUN yum -y install \
golang \
make \
librados-devel \
librbd-devel \
&& yum -y update \
&& true