static.yaml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ---
  2. name: Build binary releases
  3. concurrency:
  4. cancel-in-progress: true
  5. group: ${{ github.workflow }}-${{ github.ref }}
  6. on:
  7. pull_request:
  8. branches:
  9. - main
  10. paths-ignore:
  11. - "docs/**"
  12. push:
  13. branches:
  14. - main
  15. tags:
  16. - v*.*.*
  17. workflow_dispatch:
  18. inputs:
  19. #checkov:skip=CKV_GHA_7
  20. version:
  21. description: "FrankenPHP version"
  22. required: false
  23. type: string
  24. schedule:
  25. - cron: "0 0 * * *"
  26. permissions:
  27. contents: write
  28. id-token: write
  29. attestations: write
  30. env:
  31. IMAGE_NAME: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/')) && 'dunglas/frankenphp' || 'dunglas/frankenphp-dev' }}
  32. jobs:
  33. prepare:
  34. runs-on: ubuntu-24.04
  35. outputs:
  36. 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) }}
  37. platforms: ${{ steps.matrix.outputs.platforms }}
  38. metadata: ${{ steps.matrix.outputs.metadata }}
  39. ref: ${{ steps.check.outputs.ref }}
  40. steps:
  41. - name: Get version
  42. id: check
  43. if: github.event_name == 'schedule'
  44. run: |
  45. ref="${{ (github.ref_type == 'tag' && github.ref_name) || (github.event_name == 'workflow_dispatch' && inputs.version) || '' }}"
  46. if [[ -z "${ref}" ]]; then
  47. ref="$(gh release view --repo dunglas/frankenphp --json tagName --jq '.tagName')"
  48. fi
  49. echo "ref=${ref}" >> "${GITHUB_OUTPUT}"
  50. env:
  51. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  52. - uses: actions/checkout@v4
  53. with:
  54. ref: ${{ steps.check.outputs.ref }}
  55. - name: Set up Docker Buildx
  56. uses: docker/setup-buildx-action@v3
  57. - name: Create platforms matrix
  58. id: matrix
  59. run: |
  60. METADATA="$(docker buildx bake --print static-builder | jq -c)"
  61. {
  62. echo metadata="${METADATA}"
  63. echo platforms="$(jq -c 'first(.target[]) | .platforms' <<< "${METADATA}")"
  64. } >> "${GITHUB_OUTPUT}"
  65. env:
  66. SHA: ${{ github.sha }}
  67. VERSION: ${{ steps.check.outputs.ref || 'dev' }}
  68. build-linux:
  69. strategy:
  70. fail-fast: false
  71. matrix:
  72. platform: ${{ fromJson(needs.prepare.outputs.platforms) }}
  73. debug: [false]
  74. mimalloc: [false]
  75. include:
  76. - platform: linux/amd64
  77. - platform: linux/amd64
  78. debug: true
  79. - platform: linux/amd64
  80. mimalloc: true
  81. name: Build ${{ matrix.platform }} static binary${{ matrix.debug && ' (debug)' || '' }}${{ matrix.mimalloc && ' (mimalloc)' || '' }}
  82. runs-on: ${{ startsWith(matrix.platform, 'linux/arm') && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
  83. needs: [prepare]
  84. steps:
  85. - name: Prepare
  86. id: prepare
  87. run: |
  88. platform=${{ matrix.platform }}
  89. echo "sanitized_platform=${platform//\//-}" >> "${GITHUB_OUTPUT}"
  90. - uses: actions/checkout@v4
  91. with:
  92. ref: ${{ needs.prepare.outputs.ref }}
  93. - name: Set up Docker Buildx
  94. uses: docker/setup-buildx-action@v3
  95. with:
  96. platforms: ${{ matrix.platform }}
  97. - name: Login to DockerHub
  98. if: ${{ fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc }}
  99. uses: docker/login-action@v3
  100. with:
  101. username: ${{ secrets.REGISTRY_USERNAME }}
  102. password: ${{ secrets.REGISTRY_PASSWORD }}
  103. - name: Build
  104. id: build
  105. uses: docker/bake-action@v6
  106. with:
  107. pull: true
  108. load: ${{ !fromJson(needs.prepare.outputs.push) || matrix.debug || matrix.mimalloc }}
  109. targets: static-builder
  110. set: |
  111. ${{ matrix.debug && 'static-builder.args.DEBUG_SYMBOLS=1' || '' }}
  112. ${{ matrix.mimalloc && 'static-builder.args.MIMALLOC=1' || '' }}
  113. ${{ (github.event_name == 'pull_request' || matrix.platform == 'linux/arm64') && 'static-builder.args.NO_COMPRESS=1' || '' }}
  114. *.tags=
  115. *.platform=${{ matrix.platform }}
  116. *.cache-from=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
  117. *.cache-from=type=gha,scope=refs/heads/main-static-builder${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
  118. *.cache-to=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }},ignore-error=true
  119. ${{ (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) || '' }}
  120. env:
  121. SHA: ${{ github.sha }}
  122. VERSION: ${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref || 'dev' }}
  123. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  124. - # Workaround for https://github.com/actions/runner/pull/2477#issuecomment-1501003600
  125. name: Export metadata
  126. if: fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc
  127. run: |
  128. mkdir -p /tmp/metadata
  129. # shellcheck disable=SC2086
  130. digest=$(jq -r '."static-builder"."containerimage.digest"' <<< ${METADATA})
  131. touch "/tmp/metadata/${digest#sha256:}"
  132. env:
  133. METADATA: ${{ steps.build.outputs.metadata }}
  134. - name: Upload metadata
  135. if: fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc
  136. uses: actions/upload-artifact@v4
  137. with:
  138. name: metadata-static-builder-${{ steps.prepare.outputs.sanitized_platform }}
  139. path: /tmp/metadata/*
  140. if-no-files-found: error
  141. retention-days: 1
  142. - name: Copy binary
  143. run: |
  144. # shellcheck disable=SC2034
  145. digest=$(jq -r '."static-builder"."${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && 'containerimage.digest' || 'containerimage.config.digest' }}"' <<< "${METADATA}")
  146. docker create --platform=${{ matrix.platform }} --name static-builder "${{ (fromJson(needs.prepare.outputs.push) && !matrix.debug && !matrix.mimalloc) && '${IMAGE_NAME}@${digest}' || '${digest}' }}"
  147. docker cp "static-builder:/go/src/app/dist/${BINARY}" "${BINARY}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}"
  148. env:
  149. METADATA: ${{ steps.build.outputs.metadata }}
  150. BINARY: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}
  151. - name: Upload artifact
  152. if: ${{ !fromJson(needs.prepare.outputs.push) }}
  153. uses: actions/upload-artifact@v4
  154. with:
  155. name: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
  156. path: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
  157. - name: Upload assets
  158. if: fromJson(needs.prepare.outputs.push) && (needs.prepare.outputs.ref || github.ref_type == 'tag')
  159. run: gh release upload "${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref }}" frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }} --repo dunglas/frankenphp --clobber
  160. env:
  161. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  162. - if: fromJson(needs.prepare.outputs.push) && (needs.prepare.outputs.ref || github.ref_type == 'tag')
  163. uses: actions/attest-build-provenance@v2
  164. with:
  165. subject-path: ${{ github.workspace }}/frankenphp-linux-*
  166. - name: Run sanity checks
  167. run: |
  168. "${BINARY}" version
  169. "${BINARY}" list-modules | grep frankenphp
  170. "${BINARY}" list-modules | grep http.encoders.br
  171. "${BINARY}" list-modules | grep http.handlers.mercure
  172. "${BINARY}" list-modules | grep http.handlers.mercure
  173. "${BINARY}" list-modules | grep http.handlers.vulcain
  174. env:
  175. BINARY: ./frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}${{ matrix.debug && '-debug' || '' }}${{ matrix.mimalloc && '-mimalloc' || '' }}
  176. # Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
  177. push:
  178. runs-on: ubuntu-24.04
  179. needs:
  180. - prepare
  181. - build-linux
  182. if: fromJson(needs.prepare.outputs.push)
  183. steps:
  184. - name: Download metadata
  185. uses: actions/download-artifact@v4
  186. with:
  187. pattern: metadata-static-builder-*
  188. path: /tmp/metadata
  189. merge-multiple: true
  190. - name: Set up Docker Buildx
  191. uses: docker/setup-buildx-action@v3
  192. - name: Login to DockerHub
  193. uses: docker/login-action@v3
  194. with:
  195. username: ${{ secrets.REGISTRY_USERNAME }}
  196. password: ${{ secrets.REGISTRY_PASSWORD }}
  197. - name: Create manifest list and push
  198. working-directory: /tmp/metadata
  199. run: |
  200. # shellcheck disable=SC2046,SC2086
  201. docker buildx imagetools create $(jq -cr '.target."static-builder".tags | map("-t " + .) | join(" ")' <<< "${METADATA}") \
  202. $(printf "${IMAGE_NAME}@sha256:%s " *)
  203. env:
  204. METADATA: ${{ needs.prepare.outputs.metadata }}
  205. - name: Inspect image
  206. run: |
  207. # shellcheck disable=SC2046,SC2086
  208. docker buildx imagetools inspect "$(jq -cr '.target."static-builder".tags | first' <<< "${METADATA}")"
  209. env:
  210. METADATA: ${{ needs.prepare.outputs.metadata }}
  211. build-mac:
  212. strategy:
  213. fail-fast: false
  214. matrix:
  215. platform: ["arm64", "x86_64"]
  216. name: Build macOS ${{ matrix.platform }} binaries
  217. runs-on: ${{ matrix.platform == 'arm64' && 'macos-14' || 'macos-13' }}
  218. needs: [prepare]
  219. env:
  220. HOMEBREW_NO_AUTO_UPDATE: 1
  221. steps:
  222. - uses: actions/checkout@v4
  223. with:
  224. ref: ${{ needs.prepare.outputs.ref }}
  225. - uses: actions/setup-go@v5
  226. with:
  227. go-version: "1.22"
  228. cache-dependency-path: |
  229. go.sum
  230. caddy/go.sum
  231. - name: Set FRANKENPHP_VERSION
  232. run: |
  233. if [ "${GITHUB_REF_TYPE}" == "tag" ]; then
  234. export FRANKENPHP_VERSION=${GITHUB_REF_NAME:1}
  235. elif [ "${GITHUB_EVENT_NAME}" == "schedule" ]; then
  236. export FRANKENPHP_VERSION="${{ needs.prepare.outputs.ref }}"
  237. else
  238. export FRANKENPHP_VERSION=${GITHUB_SHA}
  239. fi
  240. echo "FRANKENPHP_VERSION=${FRANKENPHP_VERSION}" >> "${GITHUB_ENV}"
  241. - name: Build FrankenPHP
  242. run: ./build-static.sh
  243. env:
  244. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  245. RELEASE: ${{ (needs.prepare.outputs.ref || github.ref_type == 'tag') && '1' || '' }}
  246. NO_COMPRESS: ${{ github.event_name == 'pull_request' && '1' || '' }}
  247. - if: needs.prepare.outputs.ref || github.ref_type == 'tag'
  248. uses: actions/attest-build-provenance@v2
  249. with:
  250. subject-path: ${{ github.workspace }}/dist/frankenphp-mac-*
  251. - name: Upload artifact
  252. if: github.ref_type == 'branch'
  253. uses: actions/upload-artifact@v4
  254. with:
  255. name: frankenphp-mac-${{ matrix.platform }}
  256. path: dist/frankenphp-mac-${{ matrix.platform }}
  257. - name: Run sanity checks
  258. run: |
  259. "${BINARY}" version
  260. "${BINARY}" list-modules | grep frankenphp
  261. "${BINARY}" list-modules | grep http.encoders.br
  262. "${BINARY}" list-modules | grep http.handlers.mercure
  263. "${BINARY}" list-modules | grep http.handlers.mercure
  264. "${BINARY}" list-modules | grep http.handlers.vulcain
  265. env:
  266. BINARY: dist/frankenphp-mac-${{ matrix.platform }}