Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue unifying build scripts to build multi-platform #66

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ permissions:
jobs:

release:
name: Publish ${{ matrix.cpu }} layers
name: Publish layers
runs-on: ubuntu-latest
strategy:
matrix:
cpu:
- x86
- arm
steps:
- uses: actions/checkout@v3

Expand All @@ -47,25 +42,13 @@ jobs:

- run: make docker-images
env:
CPU: ${{ matrix.cpu }}
USE_DEPOT: 1
DEPOT_TOKEN: ${{ secrets.DEPOT_TOKEN }}

- run: make layers
env:
CPU: ${{ matrix.cpu }}

- run: make test
env:
CPU: ${{ matrix.cpu }}

- run: make upload-layers
env:
CPU: ${{ matrix.cpu }}

- run: make upload-to-docker-hub
env:
CPU: ${{ matrix.cpu }}

update-layer-versions:
name: Update layer versions in brefphp/bref
Expand Down
40 changes: 3 additions & 37 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,14 @@ permissions:
contents: read

jobs:
matrix-prep:
name: Prepare matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.result }}
steps:
- uses: actions/github-script@v6
id: set-matrix
with:
script: |
const matrix = {
cpu: ['x86', 'arm'],
php_version: ['80', '81', '82'],
}

// If this is a third-party pull request, skip ARM builds
if (context.eventName === 'pull_request') {
const pr = context.payload.pull_request
if (pr.head.repo.full_name !== pr.base.repo.full_name) {
matrix.cpu = ['x86']
}
}

return matrix

tests:
name: Build and tests PHP ${{ matrix.php_version }}, ${{ matrix.cpu }}
name: Build and tests PHP ${{ matrix.php_version }}
runs-on: ubuntu-latest
needs: matrix-prep
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.matrix-prep.outputs.matrix) }}
matrix:
php_version: ['80', '81', '82']
steps:
- uses: actions/checkout@v3

Expand All @@ -64,26 +40,16 @@ jobs:
# If this is a third-party pull request, fall back to the local buildx builder
buildx-fallback: ${{ github.event.pull_request.head.repo.full_name != github.repository }}
env:
CPU: ${{ matrix.cpu }}
CPU_PREFIX: ${{ (matrix.cpu == 'arm') && 'arm-' || '' }}
PHP_VERSION: ${{ matrix.php_version }}
IMAGE_VERSION_SUFFIX: ${{ (matrix.cpu == 'arm') && 'arm64' || 'x86_64' }}
DOCKER_PLATFORM: ${{ (matrix.cpu == 'arm') && 'linux/arm64' || 'linux/amd64' }}

- name: Test that layers can be exported
run: |
make layer-php-${{ matrix.php_version }}
make layer-php-${{ matrix.php_version }}-fpm
env:
CPU: ${{ matrix.cpu }}

- name: Test that the "console" layer can be exported
run: make layer-console
if: ${{ matrix.php_version == 80 }}
env:
CPU: ${{ matrix.cpu }}

- name: Run tests
run: make test-${{ matrix.php_version }}
env:
CPU: ${{ matrix.cpu }}
52 changes: 27 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ export # export all variables defined in .env
# Define all the environment variables depending on the CPU
# Set CPU= (empty) to build for x86
# Set CPU=arm to build for ARM
ifeq ($(CPU), arm) # if $CPU=="arm"
ifeq ($(CPU), x86) # if $CPU=="x86"
$(info "⚠️ Building for x86") # Print a message
export CPU = x86
export CPU_PREFIX =
export DOCKER_PLATFORM = linux/amd64
export BAKE_OPTIONS = --set *.platform=${DOCKER_PLATFORM}
else ifeq ($(CPU), arm) # if $CPU=="arm"
$(info "⚠️ Building for ARM") # Print a message
export CPU = arm
export CPU_PREFIX = arm-
export IMAGE_VERSION_SUFFIX = arm64
export DOCKER_PLATFORM = linux/arm64
export BAKE_OPTIONS = --set *.platform=${DOCKER_PLATFORM}
else
$(info "⚠️ Building for x86") # Print a message
export CPU = x86
$(info "⚠️ Building for x86 and ARM") # Print a message
export CPU =
export CPU_PREFIX =
export IMAGE_VERSION_SUFFIX = x86_64
export DOCKER_PLATFORM = linux/amd64
export DOCKER_PLATFORM =
export BAKE_OPTIONS =
endif

# By default, Docker images are built using `docker buildx bake`
Expand All @@ -37,21 +43,18 @@ default: docker-images layers
# Build Docker images *locally*
docker-images: docker-images-php-80 docker-images-php-81 docker-images-php-82
docker-images-php-%:
PHP_VERSION=$* ${BAKE_COMMAND} --load
PHP_VERSION=$* ${BAKE_COMMAND} --load ${BAKE_OPTIONS}


# Build Lambda layers (zip files) *locally*
layers: layer-php-80 layer-php-81 layer-php-82 layer-php-80-fpm layer-php-81-fpm layer-php-82-fpm
# Build the console layer only once (x86 and single PHP version)
@if [ ${CPU} = "x86" ]; then \
$(MAKE) layer-console; \
fi
layers: layer-php-80 layer-php-81 layer-php-82 layer-php-80-fpm layer-php-81-fpm layer-php-82-fpm layer-console
layer-console:
./utils/docker-zip-dir.sh bref/console-zip console
DOCKER_PLATFORM=linux/amd64 ./utils/docker-zip-dir.sh bref/console-zip console
# This rule matches with a wildcard, for example `layer-php-80`.
# The `$*` variable will contained the matched part, in this case `php-80`.
layer-%:
./utils/docker-zip-dir.sh bref/${CPU_PREFIX}$* ${CPU_PREFIX}$*
DOCKER_PLATFORM=linux/amd64 ./utils/docker-zip-dir.sh bref/$* $*
DOCKER_PLATFORM=linux/arm64 ./utils/docker-zip-dir.sh bref/$* arm-$*


# Upload the layers to AWS Lambda
Expand All @@ -73,8 +76,8 @@ upload-layers-php-%:
upload-to-docker-hub: upload-to-docker-hub-php-80 upload-to-docker-hub-php-81 upload-to-docker-hub-php-82
upload-to-docker-hub-php-%:
for image in \
"bref/${CPU_PREFIX}php-$*" "bref/${CPU_PREFIX}php-$*-fpm" "bref/${CPU_PREFIX}php-$*-console" \
"bref/${CPU_PREFIX}build-php-$*" "bref/${CPU_PREFIX}php-$*-fpm-dev"; \
"bref/php-$*" "bref/php-$*-fpm" "bref/php-$*-console" \
"bref/build-php-$*" "bref/php-$*-fpm-dev"; \
do \
docker tag $$image $$image:2 ; \
docker push $$image --all-tags ; \
Expand All @@ -83,20 +86,19 @@ upload-to-docker-hub-php-%:

test: test-80 test-81 test-82
test-%:
cd tests && $(MAKE) test-$*
cd tests && DOCKER_PLATFORM=linux/amd64 $(MAKE) test-$*
cd tests && DOCKER_PLATFORM=linux/arm64 $(MAKE) test-$*


clean: clean-80 clean-81 clean-82
# Clear the build cache, else all images will be rebuilt using cached layers
docker builder prune
# Remove zip files
rm -f output/${CPU_PREFIX}*.zip
rm -f output/*.zip
clean-%:
# Clean Docker images to force rebuilding them
docker image rm --force bref/${CPU_PREFIX}build-php-$* \
bref/${CPU_PREFIX}php-$* \
bref/${CPU_PREFIX}php-$*-zip \
bref/${CPU_PREFIX}php-$*-fpm \
bref/${CPU_PREFIX}php-$*-fpm-zip \
bref/${CPU_PREFIX}php-$*-fpm-dev \
bref/${CPU_PREFIX}php-$*-console
docker image rm --force bref/build-php-$* \
bref/php-$* \
bref/php-$*-fpm \
bref/php-$*-fpm-dev \
bref/php-$*-console
66 changes: 23 additions & 43 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,45 @@ group "default" {
targets = ["build-php", "php", "php-fpm", "console-zip", "console", "php-fpm-dev"]
}

variable "CPU" {
default = "x86"
}
variable "CPU_PREFIX" {
default = ""
}
variable "PHP_VERSION" {
default = "80"
}
variable "IMAGE_VERSION_SUFFIX" {
default = "x86_64"
}
variable "DOCKER_PLATFORM" {
default = "linux/amd64"
}
variable "PHP_COMPILATION_FLAGS" {
default = ""
default = ""
}

target "build-php" {
dockerfile = "php-${PHP_VERSION}/Dockerfile"
target = "build-environment"
tags = ["bref/${CPU_PREFIX}build-php-${PHP_VERSION}"]
tags = ["bref/build-php-${PHP_VERSION}"]
args = {
"IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}"
"PHP_COMPILATION_FLAGS" = "${PHP_COMPILATION_FLAGS}"
}
platforms = ["${DOCKER_PLATFORM}"]
platforms = ["linux/arm64", "linux/amd64"]
}

target "php" {
dockerfile = "php-${PHP_VERSION}/Dockerfile"
target = "function"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}"]
tags = ["bref/php-${PHP_VERSION}"]
args = {
"IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}"
"PHP_COMPILATION_FLAGS" = "${PHP_COMPILATION_FLAGS}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
contexts = {
"bref/build-php-${PHP_VERSION}" = "target:build-php"
}
platforms = ["${DOCKER_PLATFORM}"]
platforms = ["linux/arm64", "linux/amd64"]
}

target "php-fpm" {
dockerfile = "php-${PHP_VERSION}/Dockerfile"
target = "fpm"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm"]
args = {
"IMAGE_VERSION_SUFFIX" = "${IMAGE_VERSION_SUFFIX}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php"
tags = ["bref/php-${PHP_VERSION}-fpm"]
contexts = {
"bref/build-php-${PHP_VERSION}" = "target:build-php"
"bref/php-${PHP_VERSION}" = "target:php"
}
platforms = ["${DOCKER_PLATFORM}"]
platforms = ["linux/arm64", "linux/amd64"]
}

target "console-zip" {
Expand All @@ -66,38 +49,35 @@ target "console-zip" {
tags = ["bref/console-zip"]
args = {
PHP_VERSION = "${PHP_VERSION}"
CPU_PREFIX = "${CPU_PREFIX}"
}
platforms = ["${DOCKER_PLATFORM}"]
platforms = ["linux/arm64", "linux/amd64"]
}

target "console" {
context = "layers/console"
target = "console"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-console"]
tags = ["bref/php-${PHP_VERSION}-console"]
args = {
PHP_VERSION = "${PHP_VERSION}"
CPU_PREFIX = "${CPU_PREFIX}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php"
"bref/build-php-${PHP_VERSION}" = "target:build-php"
"bref/php-${PHP_VERSION}" = "target:php"
}
platforms = ["${DOCKER_PLATFORM}"]
platforms = ["linux/arm64", "linux/amd64"]
}

target "php-fpm-dev" {
context = "layers/fpm-dev"
tags = ["bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm-dev"]
tags = ["bref/php-${PHP_VERSION}-fpm-dev"]
args = {
PHP_VERSION = "${PHP_VERSION}"
CPU_PREFIX = "${CPU_PREFIX}"
}
contexts = {
"bref/${CPU_PREFIX}build-php-${PHP_VERSION}" = "target:build-php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}" = "target:php"
"bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm" = "target:php-fpm"
"bref/local-api-gateway" = "docker-image://bref/local-api-gateway:latest"
"bref/build-php-${PHP_VERSION}" = "target:build-php"
"bref/php-${PHP_VERSION}" = "target:php"
"bref/php-${PHP_VERSION}-fpm" = "target:php-fpm"
"bref/local-api-gateway" = "docker-image://bref/local-api-gateway:latest"
}
platforms = ["${DOCKER_PLATFORM}"]
platforms = ["linux/arm64", "linux/amd64"]
}
9 changes: 4 additions & 5 deletions layers/fpm-dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# syntax = docker/dockerfile:1.4
ARG CPU_PREFIX
ARG PHP_VERSION


FROM bref/${CPU_PREFIX}build-php-$PHP_VERSION as build_extensions
FROM bref/build-php-$PHP_VERSION as build_extensions

ARG PHP_VERSION

Expand All @@ -16,11 +15,11 @@ RUN cp $(php -r "echo ini_get('extension_dir');")/xdebug.so /opt/bref/extensions
# Install Blackfire
# https://blackfire.io/docs/up-and-running/installation?action=install&mode=full&version=latest&mode=full&location=server&os=manual&language=php#install-the-php-probe
ARG BLACKFIRE_VERSION=1.86.1
RUN if [[ $CPU_PREFIX == "" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_amd64-php-"$PHP_VERSION".so"; fi
RUN if [[ $CPU_PREFIX == "arm-" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_arm64-php-"$PHP_VERSION".so"; fi
RUN if [[ $CPU == "x86" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_amd64-php-"$PHP_VERSION".so"; fi
RUN if [[ $CPU == "arm" ]]; then curl -A "Docker" -o /opt/bref/extensions/blackfire.so -L -s "https://packages.blackfire.io/binaries/blackfire-php/$BLACKFIRE_VERSION/blackfire-php-linux_arm64-php-"$PHP_VERSION".so"; fi


FROM bref/${CPU_PREFIX}php-${PHP_VERSION}-fpm
FROM bref/php-${PHP_VERSION}-fpm

COPY --link --from=build_extensions /opt /opt
COPY --link bref-entrypoint.sh /
Expand Down
7 changes: 2 additions & 5 deletions php-80/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# syntax = docker/dockerfile:1.4

# Can be "x86_64" or "arm64"
ARG IMAGE_VERSION_SUFFIX

# https://www.php.net/downloads
ARG VERSION_PHP=8.0.28

Expand All @@ -11,7 +8,7 @@ ARG VERSION_PHP=8.0.28
# https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
# AWS provides a Docker image that we use here:
# https://github.com/amazonlinux/container-images/tree/amzn2
FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as build-environment
FROM public.ecr.aws/lambda/provided:al2 as build-environment


RUN set -xe \
Expand Down Expand Up @@ -450,7 +447,7 @@ RUN cp ${CA_BUNDLE} /bref-layer/bref/ssl/cert.pem

# ---------------------------------------------------------------
# Start from a clean image to copy only the files we need
FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation
FROM public.ecr.aws/lambda/provided:al2 as isolation

# We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer)
COPY --link --from=build-environment /bref-layer /opt
Expand Down
Loading