Merge pull request #47 from deuill/feature/unify-dockerfile

Unify Dockerfiles for easier testing across PHP versions
This commit is contained in:
Alex Palaistras 2017-10-07 11:45:26 +01:00 committed by GitHub
commit 46f61a6114
4 changed files with 38 additions and 97 deletions

View File

@ -1,15 +1,13 @@
language: php
php:
- 5.6.31
- 7.1.10
- 7.0.23
- 7.1.9
sudo: required
services:
- docker
before_install:
- make docker-image
script:
- make docker-test PHP_VERSION="$TRAVIS_PHP_VERSION"

View File

@ -1,34 +1,29 @@
FROM debian:stable-slim
ENV PHP_VERSION="5.6.31"
ENV PHP_URL="https://secure.php.net/get/php-${PHP_VERSION}.tar.xz/from/this/mirror" PHP_ASC_URL="https://secure.php.net/get/php-${PHP_VERSION}.tar.xz.asc/from/this/mirror"
# The full PHP version to target, i.e. "7.1.10".
ARG PHP_VERSION
# Environment variables used across the build.
ENV PHP_URL="https://secure.php.net/get/php-${PHP_VERSION}.tar.xz/from/this/mirror" PHP_ASC_URL="https://secure.php.net/get/php-${PHP_VERSION}.tar.xz.asc/from/this/mirror"
ENV PHP_BASE_DIR="/tmp/php"
ENV PHP_SRC_DIR="${PHP_BASE_DIR}/src"
# Build variables.
ENV PHP_LDFLAGS="-Wl,-O1 -Wl,--hash-style=both -pie"
ENV PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2"
ENV PHP_CPPFLAGS="${PHP_CFLAGS}"
ENV GPG_KEYS="0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3"
ENV PHP_SHA256="c464af61240a9b7729fabe0314cdbdd5a000a4f0c9bd201f89f8628732fe4ae4"
ENV FETCH_DEPS="ca-certificates wget dirmngr gnupg2"
# Fetch PHP source code. This step does not currently validate keys or checksums, as this process
# will eventually transition to using the base `php` Docker images.
ENV FETCH_DEPS="ca-certificates wget"
RUN set -xe && \
apt-get update && apt-get install -y --no-install-recommends ${FETCH_DEPS} && \
mkdir -p ${PHP_BASE_DIR} && cd ${PHP_BASE_DIR} && \
wget -O php.tar.xz ${PHP_URL} && \
echo "${PHP_SHA256} *php.tar.xz" | sha256sum -c - && \
wget -O php.tar.xz.asc "${PHP_ASC_URL}" && \
export GNUPGHOME="$(mktemp -d)" && \
for key in ${GPG_KEYS}; do gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; done && \
gpg --batch --verify php.tar.xz.asc php.tar.xz && \
rm -Rf ${GNUPGHOME} && \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false ${FETCH_DEPS}
# Build PHP library from source.
ENV BUILD_DEPS="build-essential file libpcre3-dev dpkg-dev libcurl4-openssl-dev libedit-dev libsqlite3-dev libssl1.0-dev libxml2-dev zlib1g-dev"
RUN set -xe && \
apt-get update && apt-get install -y --no-install-recommends ${BUILD_DEPS} && \
export CFLAGS="${PHP_CFLAGS}" CPPFLAGS="${PHP_CPPFLAGS}" LDFLAGS="${PHP_LDFLAGS}" && \
@ -49,6 +44,7 @@ RUN set -xe && \
make -j "$(nproc)" && \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false ${BUILD_DEPS}
# Install runtime dependencies for testing, building packages etc, and clean up source.
ENV RUNTIME_DEPS="build-essential git golang curl libedit2 libssl1.0 libxml2"
ENV SOURCE_REPO="github.com/deuill/go-php"

View File

@ -1,60 +0,0 @@
FROM debian:stable-slim
ENV PHP_VERSION="7.1.10"
ENV PHP_URL="https://secure.php.net/get/php-${PHP_VERSION}.tar.xz/from/this/mirror" PHP_ASC_URL="https://secure.php.net/get/php-${PHP_VERSION}.tar.xz.asc/from/this/mirror"
ENV PHP_BASE_DIR="/tmp/php"
ENV PHP_SRC_DIR="${PHP_BASE_DIR}/src"
ENV PHP_LDFLAGS="-Wl,-O1 -Wl,--hash-style=both -pie"
ENV PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2"
ENV PHP_CPPFLAGS="${PHP_CFLAGS}"
ENV GPG_KEYS="A917B1ECDA84AEC2B568FED6F50ABC807BD5DCD0 528995BFEDFBA7191D46839EF9BA0ADA31CBD89E"
ENV PHP_SHA256="2b8efa771a2ead0bb3ae67b530ca505b5b286adc873cca9ce97a6e1d6815c50b"
ENV FETCH_DEPS="ca-certificates wget dirmngr gnupg2"
RUN set -xe && \
apt-get update && apt-get install -y --no-install-recommends ${FETCH_DEPS} && \
mkdir -p ${PHP_BASE_DIR} && cd ${PHP_BASE_DIR} && \
wget -O php.tar.xz ${PHP_URL} && \
echo "${PHP_SHA256} *php.tar.xz" | sha256sum -c - && \
wget -O php.tar.xz.asc "${PHP_ASC_URL}" && \
export GNUPGHOME="$(mktemp -d)" && \
for key in ${GPG_KEYS}; do gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; done && \
gpg --batch --verify php.tar.xz.asc php.tar.xz && \
rm -Rf ${GNUPGHOME} && \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false ${FETCH_DEPS}
ENV BUILD_DEPS="build-essential file libpcre3-dev dpkg-dev libcurl4-openssl-dev libedit-dev libsqlite3-dev libssl-dev libxml2-dev zlib1g-dev"
RUN set -xe && \
apt-get update && apt-get install -y --no-install-recommends ${BUILD_DEPS} && \
export CFLAGS="${PHP_CFLAGS}" CPPFLAGS="${PHP_CPPFLAGS}" LDFLAGS="${PHP_LDFLAGS}" && \
arch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" && multiarch="$(dpkg-architecture --query DEB_BUILD_MULTIARCH)" && \
if [ ! -d /usr/include/curl ]; \
then ln -sT "/usr/include/$multiarch/curl" /usr/local/include/curl; \
fi && \
mkdir -p ${PHP_SRC_DIR} && cd ${PHP_SRC_DIR} && \
tar -xJf ${PHP_BASE_DIR}/php.tar.xz -C . --strip-components=1 && \
./configure \
--prefix=/usr --build="$arch" \
--with-libdir="lib/$multiarch" \
--with-pcre-regex=/usr \
--disable-cgi --disable-fpm \
--enable-embed --enable-ftp --enable-mbstring \
--with-curl --with-libedit --with-openssl --with-zlib \
&& \
make -j "$(nproc)" && \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false ${BUILD_DEPS}
ENV RUNTIME_DEPS="build-essential git golang curl libedit2 libssl1.1 libxml2"
ENV SOURCE_REPO="github.com/deuill/go-php"
RUN set -xe && \
apt-get update && apt-get install -y --no-install-recommends ${RUNTIME_DEPS} && \
cd ${PHP_SRC_DIR} && make -j "$(nproc)" PHP_SAPI=embed install-sapi install-headers && \
cd / && rm -Rf ${PHP_BASE_DIR} ${PHP_SRC_DIR}
ENTRYPOINT ["/bin/sh", "-c"]

View File

@ -5,11 +5,15 @@ IMPORT_PATH := github.com/deuill/$(NAME)
VERSION := $(shell git describe --tags --always --dirty="-dev")
DATE := $(shell date '+%Y-%m-%d-%H%M UTC')
# Build options.
# Generic build options.
BUILD_OPTIONS := -ldflags='-X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
PACKAGE_FORMAT := tar.xz
PHP_VERSION := php7
GO := go
PHP_VERSION := 7.1.10
DOCKER_IMAGE := deuill/$(NAME):$(PHP_VERSION)
# Go build options.
GO := go
TAGS := -tags 'php$(word 1,$(subst ., ,$(PHP_VERSION)))'
# Install options.
PREFIX := /usr
@ -22,7 +26,7 @@ all: $(NAME)
$(NAME): .build/env/GOPATH/.ok
@echo "Building '$(NAME)'..."
$Q $(GO) install $(if $(VERBOSE),-v) $(BUILD_OPTIONS) $(IMPORT_PATH)
$Q $(GO) install $(if $(VERBOSE),-v) $(TAGS) $(BUILD_OPTIONS) $(IMPORT_PATH)
## Print internal package list.
list: .build/env/GOPATH/.ok
@ -38,19 +42,19 @@ install: $(NAME)
## Run test for all local packages or specified PACKAGE.
test: .build/env/GOPATH/.ok
@echo "Running tests for '$(NAME)'..."
$Q $(GO) test -race $(if $(VERBOSE),-v) -tags $(PHP_VERSION) $(if $(PACKAGE),$(PACKAGE),$(PACKAGES))
$Q $(GO) test -race $(if $(VERBOSE),-v) $(TAGS) $(if $(PACKAGE),$(PACKAGE),$(PACKAGES))
@echo "Running 'vet' for '$(NAME)'..."
$Q $(GO) vet $(if $(VERBOSE),-v) -tags $(PHP_VERSION) $(if $(PACKAGE),$(PACKAGE),$(PACKAGES))
$Q $(GO) vet $(if $(VERBOSE),-v) $(TAGS) $(if $(PACKAGE),$(PACKAGE),$(PACKAGES))
## Create test coverage report for all local packages or specified PACKAGE.
cover: .build/env/GOPATH/.ok
@echo "Creating code coverage report for '$(NAME)'..."
$Q rm -Rf .build/tmp && mkdir -p .build/tmp
$Q for pkg in $(if $(PACKAGE),$(PACKAGE),$(PACKAGES)); do \
name=`echo $$pkg.cover | tr '/' '.'`; \
imports=`go list -f '{{ join .Imports " " }}' $$pkg`; \
coverpkg=`echo "$$imports $(PACKAGES)" | tr ' ' '\n' | sort | uniq -d | tr '\n' ','`; \
$(GO) test $(if $(VERBOSE),-v) -tags $(PHP_VERSION) -coverpkg $$coverpkg$$pkg -coverprofile .build/tmp/$$name $$pkg; done
name=`echo $$pkg.cover | tr '/' '.'`; \
imports=`go list -f '{{ join .Imports " " }}' $$pkg`; \
coverpkg=`echo "$$imports $(PACKAGES)" | tr ' ' '\n' | sort | uniq -d | tr '\n' ','`; \
$(GO) test $(if $(VERBOSE),-v) $(TAGS) -coverpkg $$coverpkg$$pkg -coverprofile .build/tmp/$$name $$pkg; done
$Q awk "$$COVERAGE_MERGE" .build/tmp/*.cover > .build/tmp/cover.merged
$Q $(GO) tool cover -html .build/tmp/cover.merged -o .build/tmp/coverage.html
@echo "Coverage report written to '.build/tmp/coverage.html'"
@ -82,14 +86,17 @@ help:
.DEFAULT:
$Q $(MAKE) -s -f $(MAKEFILE) help
# Pull or build Docker image for PHP version specified.
docker-image:
$Q docker build -t "$(NAME):$(PHP_VERSION)" -f Dockerfile.$(PHP_VERSION) .
$Q docker image pull $(DOCKER_IMAGE) || \
docker build --build-arg=PHP_VERSION=$(PHP_VERSION) \
-t $(DOCKER_IMAGE) -f Dockerfile . \
docker-test: docker-image
$Q docker run --rm \
-e GOPATH="/tmp/go" \
-v "$(CURDIR):/tmp/go/src/$(IMPORT_PATH)" $(NAME):$(PHP_VERSION) \
"make -C /tmp/go/src/$(IMPORT_PATH) test VERBOSE=$(VERBOSE) PHP_VERSION=$(PHP_VERSION)"
# Run Make target in Docker container. For instance, to run 'test', call as 'docker-test'.
docker-%: docker-image
$Q docker run --rm -e GOPATH="/tmp/go" \
-v "$(CURDIR):/tmp/go/src/$(IMPORT_PATH)" $(DOCKER_IMAGE) \
"$(MAKE) -C /tmp/go/src/$(IMPORT_PATH) $(word 2,$(subst -, ,$@)) VERBOSE=$(VERBOSE) PHP_VERSION=$(PHP_VERSION)"
$(NAME)_$(VERSION).tar.xz: .build/dist/.ok
@echo "Building 'tar' package for '$(NAME)'..."
@ -98,8 +105,8 @@ $(NAME)_$(VERSION).tar.xz: .build/dist/.ok
$(NAME)_$(VERSION).deb: .build/dist/.ok
@echo "Building 'deb' package for '$(NAME)'..."
$Q fakeroot -- fpm -f -s dir -t deb \
-n $(NAME) -v $(VERSION) -p $(NAME)_$(VERSION).deb \
-C .build/dist
-n $(NAME) -v $(VERSION) -p $(NAME)_$(VERSION).deb \
-C .build/dist
.build/dist/.ok:
$Q mkdir -p .build/dist && touch $@
@ -114,7 +121,7 @@ Q := $(if $(VERBOSE),,@)
PACKAGES = $(shell ( \
cd $(CURDIR)/.build/env/GOPATH/src/$(IMPORT_PATH) && \
GOPATH=$(CURDIR)/.build/env/GOPATH go list ./... | grep -v "vendor" \
GOPATH=$(CURDIR)/.build/env/GOPATH go list ./... | grep -v "vendor" \
))
export GOPATH := $(CURDIR)/.build/env/GOPATH