ci: use Docker Bake to build images

This commit is contained in:
Marko Korhonen
2023-01-14 10:14:57 +02:00
committed by Kévin Dunglas
parent d284deab9d
commit e66ac34ac7
9 changed files with 155 additions and 290 deletions
+10 -2
View File
@@ -1,2 +1,10 @@
Dockerfile
Dockerfile.dev
/.git/
/.github/
/.gitmodules/
/.idea/
/.vscode/
/docs/
/*Dockerfile*
/.*ignore
/*.hcl
/*.md
-105
View File
@@ -1,105 +0,0 @@
name: Build and push Docker image (latest)
on:
push:
branches:
- main
tags:
- v*
workflow_dispatch:
inputs: {}
jobs:
docker-tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Build test image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: builder
tags: frankenphp:${{ github.sha }}-builder
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: type=docker,dest=/tmp/.builder.tar
- name: Run tests
run: |
docker load -i /tmp/.builder.tar
docker run --rm frankenphp:${{ github.sha }}-builder "sh -c 'go test -race -v ./... && cd caddy && go test -race -v ./...'"
push-image:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
include:
- dockerfile: Dockerfile
flavor: ""
- dockerfile: Dockerfile.alpine
flavor: "-alpine"
steps:
- uses: actions/checkout@v3
- name: Docker Login
uses: docker/login-action@v2
with:
#registry: ${{secrets.REGISTRY_LOGIN_SERVER}}
username: ${{secrets.REGISTRY_USERNAME}}
password: ${{secrets.REGISTRY_PASSWORD}}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
# list of Docker images to use as base name for tags
images: ${{secrets.IMAGE_NAME}}
flavor: |
suffix=${{matrix.flavor}}
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
type=sha
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Build and Push Image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: true
pull: true
target: final
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
-94
View File
@@ -1,94 +0,0 @@
name: Tests in Docker
on:
pull_request:
branches:
- main
jobs:
docker-tests:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Build test image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: builder
tags: frankenphp:${{ github.sha }}-builder
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: type=docker,dest=/tmp/.builder.tar
- name: Run tests
run: |
docker load -i /tmp/.builder.tar
docker run --rm frankenphp:${{ github.sha }}-builder "sh -c 'go test -race -v ./... && cd caddy && go test -race -v ./...'"
push-image:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
include:
- dockerfile: Dockerfile
flavor: ""
- dockerfile: Dockerfile.alpine
flavor: "-alpine"
steps:
- uses: actions/checkout@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
suffix=${{matrix.flavor}}
# list of Docker images to use as base name for tags
images: |
frankenphp
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest
type=sha
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Build Image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: final
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
+45
View File
@@ -0,0 +1,45 @@
name: Build Docker images
on:
pull_request:
branches:
- main
push:
branches:
- main
tags:
- v*
workflow_dispatch:
inputs: {}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
#registry: ${{secrets.REGISTRY_LOGIN_SERVER}}
username: ${{secrets.REGISTRY_USERNAME}}
password: ${{secrets.REGISTRY_PASSWORD}}
- name: Build for amd64
uses: docker/bake-action@v3
with:
pull: true
load: true
set: "*.platform=linux/amd64"
- name: Build for all platforms
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
uses: docker/bake-action@v3
with:
pull: true
push: true
- run: docker image ls
- name: Run tests on Debian Bookworm
run: |
docker run --rm dunglas/frankenphp:builder-bookworm "sh -c 'go test -race -v ./... && cd caddy && go test -race -v ./...'"
- name: Run tests on Alpine
run: |
docker run --rm dunglas/frankenphp:builder-alpine "sh -c 'go test -race -v ./... && cd caddy && go test -race -v ./...'"
+2 -52
View File
@@ -1,55 +1,5 @@
FROM php:8.2-zts-bookworm AS php-base
FROM golang:1.20-bookworm AS golang-base
FROM php-base AS builder
COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go
COPY --from=golang-base /usr/local/go /usr/local/go
# This is required to link the FrankenPHP binary to the PHP binary
RUN apt-get update && \
apt-get -y --no-install-recommends install \
libargon2-dev \
libcurl4-openssl-dev \
libonig-dev \
libreadline-dev \
libsodium-dev \
libsqlite3-dev \
libssl-dev \
libxml2-dev \
zlib1g-dev \
&& \
apt-get clean
WORKDIR /go/src/app
COPY go.mod go.sum ./
RUN go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get
RUN mkdir caddy && cd caddy
COPY caddy/go.mod caddy/go.sum ./caddy/
RUN cd caddy && go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get
COPY *.* ./
COPY caddy caddy
COPY C-Thread-Pool C-Thread-Pool
COPY internal internal
COPY testdata testdata
# todo: automate this?
# see https://github.com/docker-library/php/blob/master/8.2/bookworm/zts/Dockerfile#L57-L59 for PHP values
ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS=$PHP_CFLAGS CGO_CPPFLAGS=$PHP_CPPFLAGS
RUN cd caddy/frankenphp && \
go build && \
cp frankenphp /usr/local/bin && \
cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile
ENTRYPOINT ["/bin/bash","-c"]
FROM php-base AS final
# syntax=docker/dockerfile:1
FROM php-base
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
+1
View File
@@ -1,3 +1,4 @@
# syntax=docker/dockerfile:1
FROM golang:1.20
ENV CFLAGS="-ggdb3"
@@ -1,8 +1,5 @@
FROM php:8.2-zts-alpine3.18 AS php-base
FROM golang:1.20-alpine3.18 AS golang-base
FROM php-base AS builder
# syntax=docker/dockerfile:1
FROM php-base
COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go
COPY --from=golang-base /usr/local/go /usr/local/go
@@ -47,26 +44,3 @@ RUN cd caddy/frankenphp && \
cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile
ENTRYPOINT ["/bin/sh","-c"]
FROM php-base AS final
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
WORKDIR /app
RUN mkdir -p /app/public
RUN echo '<?php phpinfo();' > /app/public/index.php
COPY --from=builder /usr/local/bin/frankenphp /usr/local/bin/frankenphp
COPY --from=builder /etc/Caddyfile /etc/Caddyfile
COPY --from=php-base /usr/local/include/php/ /usr/local/include/php
COPY --from=php-base /usr/local/lib/libphp.* /usr/local/lib
COPY --from=php-base /usr/local/lib/php/ /usr/local/lib/php
COPY --from=php-base /usr/local/php/ /usr/local/php
COPY --from=php-base /usr/local/bin/ /usr/local/bin
COPY --from=php-base /usr/src /usr/src
RUN sed -i 's/php/frankenphp run/g' /usr/local/bin/docker-php-entrypoint
CMD [ "--config", "/etc/Caddyfile" ]
+47
View File
@@ -0,0 +1,47 @@
# syntax=docker/dockerfile:1
FROM php-base
COPY --from=golang-base /usr/local/go/bin/go /usr/local/bin/go
COPY --from=golang-base /usr/local/go /usr/local/go
# This is required to link the FrankenPHP binary to the PHP binary
RUN apt-get update && \
apt-get -y --no-install-recommends install \
libargon2-dev \
libcurl4-openssl-dev \
libonig-dev \
libreadline-dev \
libsodium-dev \
libsqlite3-dev \
libssl-dev \
libxml2-dev \
zlib1g-dev \
&& \
apt-get clean
WORKDIR /go/src/app
COPY go.mod go.sum ./
RUN go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get
RUN mkdir caddy && cd caddy
COPY caddy/go.mod caddy/go.sum ./caddy/
RUN cd caddy && go mod graph | awk '{if ($1 !~ "@") print $2}' | xargs go get
COPY *.* ./
COPY caddy caddy
COPY C-Thread-Pool C-Thread-Pool
COPY internal internal
COPY testdata testdata
# todo: automate this?
# see https://github.com/docker-library/php/blob/master/8.2-rc/bullseye/zts/Dockerfile#L57-L59 for php values
ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" CGO_CFLAGS=$PHP_CFLAGS CGO_CPPFLAGS=$PHP_CPPFLAGS
RUN cd caddy/frankenphp && \
go build && \
cp frankenphp /usr/local/bin && \
cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile
ENTRYPOINT ["/bin/bash","-c"]
+48 -9
View File
@@ -3,27 +3,66 @@ variable "REPO_NAME" {
}
group "default" {
targets = ["bookworm", "alpine"]
targets = ["bookworm-variants", "alpine-variants"]
}
group "bookworm-variants" {
targets = ["bookworm-php-82", "builder-bookworm-php-82"]
}
group "alpine-variants" {
targets = ["alpine-php-82", "builder-alpine-php-82"]
}
target "common" {
context = "."
platforms = ["linux/amd64", "linux/arm64"]
}
target "common-bookworm" {
contexts = {
php-base = "docker-image://php:8.2-zts-bookworm"
golang-base = "docker-image://golang:1.20-bookworm"
}
}
target "common-alpine" {
contexts = {
php-base = "docker-image://php:8.2-zts-alpine3.18"
golang-base = "docker-image://golang:1.20-alpine3.18"
}
}
# Builders
target "builder-bookworm-php-82" {
inherits = ["common-bookworm"]
dockerfile = "builder-debian.Dockerfile"
tags = ["${REPO_NAME}:builder", "${REPO_NAME}:builder-bookworm"]
}
target "builder-alpine-php-82" {
inherits = ["common-alpine"]
dockerfile = "builder-alpine.Dockerfile"
tags = ["${REPO_NAME}:builder-alpine"]
}
#
# FrankenPHP
#
target "bookworm" {
inherits = ["common"]
context = "."
dockerfile = "Dockerfile"
target "bookworm-php-82" {
inherits = ["common", "common-bookworm"]
contexts = {
builder = "target:builder-bookworm-php-82"
}
tags = ["${REPO_NAME}:bookworm", "${REPO_NAME}:latest"]
}
target "alpine" {
inherits = ["common"]
context = "."
dockerfile = "Dockerfile.alpine"
target "alpine-php-82" {
inherits = ["common", "common-alpine"]
contexts = {
builder = "target:builder-alpine-php-82"
}
tags = ["${REPO_NAME}:alpine"]
}