forked from fabric8-services/fabric8-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better docker build/deploy-pipeline (#147)
To allow for better development with docker I've integrated the files from the `jenkins/linux` directory into the normal Makefile process. If you are on Linux and have docker installed you get the following new `make` targets. **NOTE: During executing of the docker command, the local directory is mounted with r/w permissions into the container and artifacts will be written to your local directory structure. The reason for this is that it allows us to package them further.** * `make docker-start` Builds a docker image (if needed) that will be used to build the project inside a docker container. After creating the builder image from `Dockerfile.builder` a container is started in the background under a name that is known to the Makefile. This container will be used in the other new Make targets to execute steps one by one and repeatable. * `make docker-deps` Inside the running container execute the `make deps` step. * `make docker-generate` Inside the running container execute the `make generate` step. * `make docker-build` Inside the running container execute the `make build` step. * `make docker-test-unit` Inside the running container execute the `make test-unit` step. * `make docker-test-integration` Inside the running container execute the `make test-integration` step. This target checks for the existence of a PostgreSQL container that was started using the `make integration-test-env-prepare` command. This target doesn't bother if there is any other PostgreSQL container running at the moment. It only accepts the one created from the `.make/docker-compose.integration-test.yaml`. * `make docker-rm` Stops and removes the running container that was started with `make docker-start`. * `make docker-image-deploy` Creates a docker image named "almighty-core-deploy" based on `Dockerfile.deploy` that contains a ready to run almighty-core server. The artifacts packaged in this image will be taken from the `bin/` directory, so beware that you've created them using the new `docker-*` targets. * `make docker-publish-deploy` Tags the image that was build with `docker-image-deploy` to `almightycore/almighty-core:latest` and pushes it to the docker hub. To not break compatibility with running Jenkins jobs, the Makefile in `jenkins/linux` now calls the above `docker-*` make targets. *** We have two `Dockerfile`s now: 1. `Dockerfile.builder` - Used to build almighty-core (using centos:7 so #118 is done) 2. `Dockerfile.deploy` - Used to package almighty-core for deployment. *** Tests now populate the return code as exit code to the CLI when executed with `go test`. *** The `docker-compose.yaml` file now contains two services `db` and `core` and uses version 2 of the docker-compose file format. The core container waits until the database container has opened the required port before it starts the almighty-core process.
- Loading branch information
Showing
18 changed files
with
290 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
DOCKER_IMAGE_CORE := $(PROJECT_NAME) | ||
DOCKER_IMAGE_DEPLOY := $(PROJECT_NAME)-deploy | ||
|
||
# If running in Jenkins we don't allow for interactively running the container | ||
ifneq ($(BUILD_TAG),) | ||
DOCKER_RUN_INTERACTIVE_SWITCH := | ||
else | ||
DOCKER_RUN_INTERACTIVE_SWITCH := -i | ||
endif | ||
|
||
# The workspace environment is set by Jenkins and defaults to /tmp if not set | ||
WORKSPACE ?= /tmp | ||
DOCKER_BUILD_DIR := $(WORKSPACE)/$(PROJECT_NAME)-build | ||
|
||
# The BUILD_TAG environment variable will be set by jenkins | ||
# to reflect jenkins-${JOB_NAME}-${BUILD_NUMBER} | ||
BUILD_TAG ?= $(PROJECT_NAME)-local-build | ||
DOCKER_CONTAINER_NAME := $(BUILD_TAG) | ||
|
||
## Where is the GOPATH inside the build container? | ||
GOPATH_IN_CONTAINER=/tmp/go | ||
PACKAGE_PATH=$(GOPATH_IN_CONTAINER)/src/$(PACKAGE_NAME) | ||
|
||
.PHONY: docker-image-builder | ||
## Builds the docker image used to build the software. | ||
docker-image-builder: | ||
@echo "Building docker image $(DOCKER_IMAGE_CORE)" | ||
docker build -t $(DOCKER_IMAGE_CORE) -f $(CUR_DIR)/Dockerfile.builder $(CUR_DIR) | ||
|
||
.PHONY: docker-image-deploy | ||
## Creates a runnable image using the artifacts from the bin directory. | ||
docker-image-deploy: | ||
docker build -t $(DOCKER_IMAGE_DEPLOY) -f $(CUR_DIR)/Dockerfile.deploy $(CUR_DIR) | ||
|
||
.PHONY: docker-publish-deploy | ||
## Tags the runnable image and pushes it to the docker hub. | ||
docker-publish-deploy: | ||
docker tag $(DOCKER_IMAGE_DEPLOY) almightycore/almighty-core:latest | ||
docker push almightycore/almighty-core:latest | ||
|
||
.PHONY: docker-build-dir | ||
## Creates the docker build directory. | ||
docker-build-dir: | ||
@echo "Creating build directory $(BUILD_DIR)" | ||
mkdir -p $(DOCKER_BUILD_DIR) | ||
|
||
CLEAN_TARGETS += clean-docker-build-container | ||
.PHONY: clean-docker-build-container | ||
## Removes any existing container used to build the software (if any). | ||
clean-docker-build-container: | ||
@echo "Removing container named \"$(DOCKER_CONTAINER_NAME)\" (if any)" | ||
ifneq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
@docker rm -f $(DOCKER_CONTAINER_NAME) | ||
else | ||
@echo "No container named \"$(DOCKER_CONTAINER_NAME)\" to remove" | ||
endif | ||
|
||
CLEAN_TARGETS += clean-docker-build-dir | ||
.PHONY: clean-docker-build-dir | ||
## Removes the docker build directory. | ||
clean-docker-build-dir: | ||
@echo "Cleaning build directory $(BUILD_DIR)" | ||
-rm -rf $(DOCKER_BUILD_DIR) | ||
|
||
.PHONY: docker-start | ||
## Starts the docker build container in the background (detached mode). | ||
docker-start: docker-build-dir docker-image-builder | ||
ifneq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
@echo "Docker container \"$(DOCKER_CONTAINER_NAME)\" already exists. To recreate, run \"make docker-rm\"." | ||
else | ||
docker run \ | ||
--detach=true \ | ||
-t \ | ||
$(DOCKER_RUN_INTERACTIVE_SWITCH) \ | ||
--name="$(DOCKER_CONTAINER_NAME)" \ | ||
-v $(CUR_DIR):$(PACKAGE_PATH):rw \ | ||
-u $(shell id -u $(USER)):$(shell id -g $(USER)) \ | ||
-e GOPATH=$(GOPATH_IN_CONTAINER) \ | ||
-w $(PACKAGE_PATH) \ | ||
$(DOCKER_IMAGE_CORE) | ||
@echo "Docker container \"$(DOCKER_CONTAINER_NAME)\" created. Continue with \"make docker-deps\"." | ||
endif | ||
|
||
.PHONY: docker-deps | ||
## Runs "make deps" inside the already started docker build container (see "make docker-start"). | ||
docker-deps: | ||
ifeq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
$(error No container name "$(DOCKER_CONTAINER_NAME)" exists to run the build. Try running "make docker-start && make docker-deps") | ||
endif | ||
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" make deps | ||
|
||
.PHONY: docker-generate | ||
## Runs "make generate" inside the already started docker build container (see "make docker-start"). | ||
docker-generate: | ||
ifeq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
$(error No container name "$(DOCKER_CONTAINER_NAME)" exists to run the build. Try running "make docker-start && make docker-deps && make docker-generate") | ||
endif | ||
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" make generate | ||
|
||
.PHONY: docker-build | ||
## Runs "make build" inside the already started docker build container (see "make docker-start"). | ||
docker-build: | ||
ifeq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
$(error No container name "$(DOCKER_CONTAINER_NAME)" exists to run the build. Try running "make docker-start && make docker-deps && make docker-generate && make docker-build") | ||
endif | ||
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" make build | ||
|
||
.PHONY: docker-test-unit | ||
## Runs "make test-unit" inside the already started docker build container (see "make docker-start"). | ||
docker-test-unit: | ||
ifeq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
$(error No container name "$(DOCKER_CONTAINER_NAME)" exists to run the build. Try running "make docker-start && make docker-deps && make docker-generate && make docker-build && make docker-test-unit") | ||
endif | ||
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" make test-unit | ||
|
||
.PHONY: docker-test-integration | ||
## Runs "make test-unit" inside the already started docker build container (see "make docker-start"). | ||
## Make sure you ran "make integration-test-env-prepare" before you run this target. | ||
docker-test-integration: | ||
ifeq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
$(error No container name "$(DOCKER_CONTAINER_NAME)" exists to run the build. Try running "make docker-start && make docker-deps && make docker-generate && make docker-build && make docker-test-unit") | ||
endif | ||
ifeq ($(strip $(shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' make_postgres_integration_test_1 2>/dev/null)),) | ||
$(error Failed to find PostgreSQL container. Try running "make integration-test-env-prepare && make docker-test-integration") | ||
endif | ||
$(eval ALMIGHTY_DB_HOST := $(shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' make_postgres_integration_test_1 2>/dev/null)) | ||
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" bash -c 'export ALMIGHTY_DB_HOST=$(ALMIGHTY_DB_HOST); make test-integration' | ||
|
||
.PHONY: docker-rm | ||
## Removes the docker build container, if any (see "make docker-start"). | ||
docker-rm: | ||
ifneq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) | ||
docker rm -f "$(DOCKER_CONTAINER_NAME)" | ||
else | ||
@echo "No container named \"$(DOCKER_CONTAINER_NAME)\" to remove." | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM centos:7 | ||
MAINTAINER "Konrad Kleine <kkleine@redhat.com>" | ||
ENV LANG=en_US.utf8 | ||
|
||
COPY bin/alm /usr/bin/alm | ||
|
||
# This script waits for a port to be open before it continues to execute what's | ||
# behind the two dashes "--". | ||
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/local/bin/wait-for-it.sh | ||
RUN chmod +x /usr/local/bin/wait-for-it.sh | ||
|
||
# Wait until the postgres service ("db") from the docker-compose.yaml file is | ||
# ready to accept connections then then start the almighty-core. | ||
ENTRYPOINT [ "/usr/bin/alm" ] | ||
|
||
EXPOSE 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.