Skip to content

Commit 3bc4264

Browse files
crazywhalecchenderkesdunglas
authored
feat: add glibc-based static binary (#1438)
* Add gnu static binary build support * Remove --libc option * configure ./build-static.sh to allow extension loading with glibc * use tabs everywhere * do not use prebuilt sources for glibc build * ffi does not work with musl builds * remove unnecessary tabs * disable opcache jit on musl * disable opcache jit on musl again * err, build command, not download command * cs fixes * spellcheck * even more cs fixes * fix ar removing .a libs * disable ffi extension for now * add gnu static action * add gnu-static target * skip CHECKOV 2 and 3 * rename static-builder to static-builder-musl, gnu-static to static-builder-gnu run arm64 gnu job on ubuntu-arm * rename build-linux to build-linux-musl * rename job description to specify musl * higher optimisation flags * Update docker-bake.hcl --------- Co-authored-by: DubbleClick <m@pyc.ac> Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent 341b024 commit 3bc4264

14 files changed

Lines changed: 433 additions & 83 deletions

.github/workflows/static.yaml

Lines changed: 143 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
push: ${{ toJson((steps.check.outputs.ref || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/') || (github.ref == 'refs/heads/main' && github.event_name != 'pull_request')) && true || false) }}
3737
platforms: ${{ steps.matrix.outputs.platforms }}
3838
metadata: ${{ steps.matrix.outputs.metadata }}
39+
gnu_metadata: ${{ steps.matrix.outputs.gnu_metadata }}
3940
ref: ${{ steps.check.outputs.ref }}
4041
steps:
4142
- name: Get version
@@ -58,15 +59,17 @@ jobs:
5859
- name: Create platforms matrix
5960
id: matrix
6061
run: |
61-
METADATA="$(docker buildx bake --print static-builder | jq -c)"
62+
METADATA="$(docker buildx bake --print static-builder-musl | jq -c)"
63+
GNU_METADATA="$(docker buildx bake --print static-builder-gnu | jq -c)"
6264
{
6365
echo metadata="${METADATA}"
6466
echo platforms="$(jq -c 'first(.target[]) | .platforms' <<< "${METADATA}")"
67+
echo gnu_metadata="${GNU_METADATA}"
6568
} >> "${GITHUB_OUTPUT}"
6669
env:
6770
SHA: ${{ github.sha }}
6871
VERSION: ${{ steps.check.outputs.ref || 'dev' }}
69-
build-linux:
72+
build-linux-musl:
7073
strategy:
7174
fail-fast: false
7275
matrix:
@@ -79,7 +82,7 @@ jobs:
7982
debug: true
8083
- platform: linux/amd64
8184
mimalloc: true
82-
name: Build ${{ matrix.platform }} static binary${{ matrix.debug && ' (debug)' || '' }}${{ matrix.mimalloc && ' (mimalloc)' || '' }}
85+
name: Build ${{ matrix.platform }} static musl binary${{ matrix.debug && ' (debug)' || '' }}${{ matrix.mimalloc && ' (mimalloc)' || '' }}
8386
runs-on: ${{ startsWith(matrix.platform, 'linux/arm') && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
8487
needs: [prepare]
8588
steps:
@@ -107,16 +110,16 @@ jobs:
107110
with:
108111
pull: true
109112
load: ${{ !fromJson(needs.prepare.outputs.push) || matrix.debug || matrix.mimalloc }}
110-
targets: static-builder
113+
targets: static-builder-musl
111114
set: |
112-
${{ matrix.debug && 'static-builder.args.DEBUG_SYMBOLS=1' || '' }}
113-
${{ matrix.mimalloc && 'static-builder.args.MIMALLOC=1' || '' }}
114-
${{ (github.event_name == 'pull_request' || matrix.platform == 'linux/arm64') && 'static-builder.args.NO_COMPRESS=1' || '' }}
115+
${{ matrix.debug && 'static-builder-musl.args.DEBUG_SYMBOLS=1' || '' }}
116+
${{ matrix.mimalloc && 'static-builder-musl.args.MIMALLOC=1' || '' }}
117+
${{ (github.event_name == 'pull_request' || matrix.platform == 'linux/arm64') && 'static-builder-musl.args.NO_COMPRESS=1' || '' }}
115118
*.tags=
116119
*.platform=${{ matrix.platform }}
117-
*.cache-from=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
118-
*.cache-from=type=gha,scope=refs/heads/main-static-builder${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
119-
*.cache-to=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }},ignore-error=true
120+
*.cache-from=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder-musl${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
121+
*.cache-from=type=gha,scope=refs/heads/main-static-builder-musl${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
122+
*.cache-to=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder-musl${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }},ignore-error=true
120123
${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && format('*.output=type=image,name={0},push-by-digest=true,name-canonical=true,push=true', env.IMAGE_NAME) || '' }}
121124
env:
122125
SHA: ${{ github.sha }}
@@ -129,24 +132,24 @@ jobs:
129132
mkdir -p /tmp/metadata
130133
131134
# shellcheck disable=SC2086
132-
digest=$(jq -r '."static-builder"."containerimage.digest"' <<< ${METADATA})
135+
digest=$(jq -r '."static-builder-musl"."containerimage.digest"' <<< ${METADATA})
133136
touch "/tmp/metadata/${digest#sha256:}"
134137
env:
135138
METADATA: ${{ steps.build.outputs.metadata }}
136139
- name: Upload metadata
137140
if: fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc
138141
uses: actions/upload-artifact@v4
139142
with:
140-
name: metadata-static-builder-${{ steps.prepare.outputs.sanitized_platform }}
143+
name: metadata-static-builder-musl-${{ steps.prepare.outputs.sanitized_platform }}
141144
path: /tmp/metadata/*
142145
if-no-files-found: error
143146
retention-days: 1
144147
- name: Copy binary
145148
run: |
146149
# shellcheck disable=SC2034
147-
digest=$(jq -r '."static-builder"."${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && 'containerimage.digest' || 'containerimage.config.digest' }}"' <<< "${METADATA}")
148-
docker create --platform=${{ matrix.platform }} --name static-builder "${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && '${IMAGE_NAME}@${digest}' || '${digest}' }}"
149-
docker cp "static-builder:/go/src/app/dist/${BINARY}" "${BINARY}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}"
150+
digest=$(jq -r '."static-builder-musl"."${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && 'containerimage.digest' || 'containerimage.config.digest' }}"' <<< "${METADATA}")
151+
docker create --platform=${{ matrix.platform }} --name static-builder-musl "${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && '${IMAGE_NAME}@${digest}' || '${digest}' }}"
152+
docker cp "static-builder-musl:/go/src/app/dist/${BINARY}" "${BINARY}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}"
150153
env:
151154
METADATA: ${{ steps.build.outputs.metadata }}
152155
BINARY: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}
@@ -177,20 +180,127 @@ jobs:
177180
env:
178181
BINARY: ./frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
179182

183+
build-linux-gnu:
184+
strategy:
185+
fail-fast: false
186+
matrix:
187+
platform: ${{ fromJson(needs.prepare.outputs.platforms) }}
188+
name: Build ${{ matrix.platform }} static GNU binary
189+
runs-on: ${{ startsWith(matrix.platform, 'linux/arm') && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
190+
needs: [prepare]
191+
steps:
192+
- name: Prepare
193+
id: prepare
194+
run: |
195+
platform=${{ matrix.platform }}
196+
echo "sanitized_platform=${platform//\//-}" >> "${GITHUB_OUTPUT}"
197+
- uses: actions/checkout@v4
198+
with:
199+
ref: ${{ needs.prepare.outputs.ref }}
200+
- name: Set up Docker Buildx
201+
uses: docker/setup-buildx-action@v3
202+
with:
203+
platforms: ${{ matrix.platform }}
204+
- name: Login to DockerHub
205+
if: ${{ fromJson(needs.prepare.outputs.push) }}
206+
uses: docker/login-action@v3
207+
with:
208+
username: ${{ secrets.REGISTRY_USERNAME }}
209+
password: ${{ secrets.REGISTRY_PASSWORD }}
210+
- name: Build
211+
id: build
212+
uses: docker/bake-action@v6
213+
with:
214+
pull: true
215+
load: ${{ !fromJson(needs.prepare.outputs.push) }}
216+
targets: static-builder-gnu
217+
set: |
218+
${{ (github.event_name == 'pull_request' || matrix.platform == 'linux/arm64') && 'static-builder-gnu.args.NO_COMPRESS=1' || '' }}
219+
*.tags=
220+
*.platform=${{ matrix.platform }}
221+
*.cache-from=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder-gnu
222+
*.cache-from=type=gha,scope=refs/heads/main-static-builder-gnu
223+
*.cache-to=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder-gnu,ignore-error=true
224+
${{ fromJson(needs.prepare.outputs.push) && format('*.output=type=image,name={0}-gnu,push-by-digest=true,name-canonical=true,push=true', env.IMAGE_NAME) || '' }}
225+
env:
226+
SHA: ${{ github.sha }}
227+
VERSION: ${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref || 'dev' }}
228+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
229+
- # Workaround for https://github.com/actions/runner/pull/2477#issuecomment-1501003600
230+
name: Export metadata
231+
if: fromJson(needs.prepare.outputs.push)
232+
run: |
233+
mkdir -p /tmp/metadata-gnu
234+
235+
# shellcheck disable=SC2086
236+
digest=$(jq -r '."static-builder-gnu"."containerimage.digest"' <<< ${METADATA})
237+
touch "/tmp/metadata-gnu/${digest#sha256:}"
238+
env:
239+
METADATA: ${{ steps.build.outputs.metadata }}
240+
- name: Upload metadata
241+
if: fromJson(needs.prepare.outputs.push)
242+
uses: actions/upload-artifact@v4
243+
with:
244+
name: metadata-static-builder-gnu-${{ steps.prepare.outputs.sanitized_platform }}
245+
path: /tmp/metadata-gnu/*
246+
if-no-files-found: error
247+
retention-days: 1
248+
- name: Copy binary
249+
run: |
250+
# shellcheck disable=SC2034
251+
digest=$(jq -r '."static-builder-gnu"."${{ fromJson(needs.prepare.outputs.push) && 'containerimage.digest' || 'containerimage.config.digest' }}"' <<< "${METADATA}")
252+
docker create --platform=${{ matrix.platform }} --name static-builder-gnu "${{ fromJson(needs.prepare.outputs.push) && format('{0}-gnu@{1}', env.IMAGE_NAME, '${digest}') || '${digest}' }}"
253+
docker cp "static-builder-gnu:/go/src/app/dist/${BINARY}" "${BINARY}-gnu"
254+
env:
255+
METADATA: ${{ steps.build.outputs.metadata }}
256+
BINARY: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}
257+
- name: Upload artifact
258+
if: ${{ !fromJson(needs.prepare.outputs.push) }}
259+
uses: actions/upload-artifact@v4
260+
with:
261+
name: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}-gnu
262+
path: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}-gnu
263+
- name: Upload assets
264+
if: fromJson(needs.prepare.outputs.push) && (needs.prepare.outputs.ref || github.ref_type == 'tag')
265+
run: gh release upload "${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref }}" frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}-gnu --repo dunglas/frankenphp --clobber
266+
env:
267+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
268+
- if: fromJson(needs.prepare.outputs.push) && (needs.prepare.outputs.ref || github.ref_type == 'tag')
269+
uses: actions/attest-build-provenance@v2
270+
with:
271+
subject-path: ${{ github.workspace }}/frankenphp-linux-*-gnu
272+
- name: Run sanity checks
273+
run: |
274+
"${BINARY}" version
275+
"${BINARY}" list-modules | grep frankenphp
276+
"${BINARY}" list-modules | grep http.encoders.br
277+
"${BINARY}" list-modules | grep http.handlers.mercure
278+
"${BINARY}" list-modules | grep http.handlers.mercure
279+
"${BINARY}" list-modules | grep http.handlers.vulcain
280+
env:
281+
BINARY: ./frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}-gnu
282+
180283
# Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
181284
push:
182285
runs-on: ubuntu-24.04
183286
needs:
184287
- prepare
185-
- build-linux
288+
- build-linux-musl
289+
- build-linux-gnu
186290
if: fromJson(needs.prepare.outputs.push)
187291
steps:
188292
- name: Download metadata
189293
uses: actions/download-artifact@v4
190294
with:
191-
pattern: metadata-static-builder-*
295+
pattern: metadata-static-builder-musl-*
192296
path: /tmp/metadata
193297
merge-multiple: true
298+
- name: Download GNU metadata
299+
uses: actions/download-artifact@v4
300+
with:
301+
pattern: metadata-static-builder-gnu-*
302+
path: /tmp/metadata-gnu
303+
merge-multiple: true
194304
- name: Set up Docker Buildx
195305
uses: docker/setup-buildx-action@v3
196306
- name: Login to DockerHub
@@ -202,16 +312,30 @@ jobs:
202312
working-directory: /tmp/metadata
203313
run: |
204314
# shellcheck disable=SC2046,SC2086
205-
docker buildx imagetools create $(jq -cr '.target."static-builder".tags | map("-t " + .) | join(" ")' <<< "${METADATA}") \
315+
docker buildx imagetools create $(jq -cr '.target."static-builder-musl".tags | map("-t " + .) | join(" ")' <<< "${METADATA}") \
206316
$(printf "${IMAGE_NAME}@sha256:%s " *)
207317
env:
208318
METADATA: ${{ needs.prepare.outputs.metadata }}
319+
- name: Create GNU manifest list and push
320+
working-directory: /tmp/metadata-gnu
321+
run: |
322+
# shellcheck disable=SC2046,SC2086
323+
docker buildx imagetools create $(jq -cr '.target."static-builder-gnu".tags | map("-t " + . + "-gnu") | join(" ")' <<< "${GNU_METADATA}") \
324+
$(printf "${IMAGE_NAME}-gnu@sha256:%s " *)
325+
env:
326+
GNU_METADATA: ${{ needs.prepare.outputs.gnu_metadata }}
209327
- name: Inspect image
210328
run: |
211329
# shellcheck disable=SC2046,SC2086
212-
docker buildx imagetools inspect "$(jq -cr '.target."static-builder".tags | first' <<< "${METADATA}")"
330+
docker buildx imagetools inspect "$(jq -cr '.target."static-builder-musl".tags | first' <<< "${METADATA}")"
213331
env:
214332
METADATA: ${{ needs.prepare.outputs.metadata }}
333+
- name: Inspect GNU image
334+
run: |
335+
# shellcheck disable=SC2046,SC2086
336+
docker buildx imagetools inspect "$(jq -cr '.target."static-builder-gnu".tags | first' <<< "${GNU_METADATA}")-gnu"
337+
env:
338+
GNU_METADATA: ${{ needs.prepare.outputs.gnu_metadata }}
215339

216340
build-mac:
217341
strategy:

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ docker buildx bake -f docker-bake.hcl --pull --no-cache --push
117117
--set static-builder.args.DEBUG_SYMBOLS=1 \
118118
--set "static-builder.platform=linux/amd64" \
119119
static-builder
120-
docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/dist/frankenphp-linux-$(uname -m) frankenphp
120+
docker cp $(docker create --name static-builder-musl dunglas/frankenphp:static-builder-musl):/go/src/app/dist/frankenphp-linux-$(uname -m) frankenphp
121121
```
122122

123123
2. Replace your current version of `frankenphp` by the debug FrankenPHP executable

0 commit comments

Comments
 (0)