docker.yaml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ---
  2. name: Build Docker images
  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. version:
  20. description: 'FrankenPHP version'
  21. required: false
  22. type: string
  23. schedule:
  24. - cron: '0 4 * * *'
  25. env:
  26. IMAGE_NAME: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/')) && 'dunglas/frankenphp' || 'dunglas/frankenphp-dev' }}
  27. LATEST: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/')) && '0' || '1' }}
  28. jobs:
  29. prepare:
  30. runs-on: ubuntu-latest
  31. outputs:
  32. # Push if it's a scheduled job, a tag, or if we're committing to the main branch
  33. push: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/') || (github.ref == 'refs/heads/main' && github.event_name != 'pull_request')) && true || false }}
  34. variants: ${{ steps.matrix.outputs.variants }}
  35. platforms: ${{ steps.matrix.outputs.platforms }}
  36. metadata: ${{ steps.matrix.outputs.metadata }}
  37. php_version: ${{ steps.check.outputs.php_version }}
  38. php82_version: ${{ steps.check.outputs.php82_version }}
  39. php83_version: ${{ steps.check.outputs.php83_version }}
  40. skip: ${{ steps.check.outputs.skip }}
  41. ref: ${{ steps.check.outputs.ref || (github.event_name == 'workflow_dispatch' && inputs.version) || '' }}
  42. steps:
  43. -
  44. name: Check PHP versions
  45. id: check
  46. env:
  47. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  48. run: |
  49. PHP_82_LATEST=$(skopeo inspect docker://docker.io/library/php:8.2 --override-os linux --override-arch amd64 | jq -r '.Env[] | select(test("^PHP_VERSION=")) | sub("^PHP_VERSION="; "")')
  50. PHP_83_LATEST=$(skopeo inspect docker://docker.io/library/php:8.3 --override-os linux --override-arch amd64 | jq -r '.Env[] | select(test("^PHP_VERSION=")) | sub("^PHP_VERSION="; "")')
  51. {
  52. echo php_version="${PHP_83_LATEST},${PHP_82_LATEST}"
  53. echo php82_version="${PHP_82_LATEST//./-}"
  54. echo php83_version="${PHP_83_LATEST//./-}"
  55. } >> "${GITHUB_OUTPUT}"
  56. # Check if the Docker images must be rebuilt
  57. if [[ "${GITHUB_EVENT_NAME}" != "schedule" ]]; then
  58. echo skip=false >> "${GITHUB_OUTPUT}"
  59. exit 0
  60. fi
  61. FRANKENPHP_82_LATEST=$(skopeo inspect docker://docker.io/dunglas/frankenphp:latest-php8.2 --override-os linux --override-arch amd64 | jq -r '.Env[] | select(test("^PHP_VERSION=")) | sub("^PHP_VERSION="; "")')
  62. FRANKENPHP_83_LATEST=$(skopeo inspect docker://docker.io/dunglas/frankenphp:latest-php8.3 --override-os linux --override-arch amd64 | jq -r '.Env[] | select(test("^PHP_VERSION=")) | sub("^PHP_VERSION="; "")')
  63. if [[ "${FRANKENPHP_82_LATEST}" == "${PHP_82_LATEST}" ]] && [[ "${FRANKENPHP_83_LATEST}" == "${PHP_83_LATEST}" ]]; then
  64. echo skip=true >> "${GITHUB_OUTPUT}"
  65. exit 0
  66. fi
  67. {
  68. echo ref="$(gh release view --repo dunglas/frankenphp --json tagName --jq '.tagName')"
  69. echo skip=false
  70. } >> "${GITHUB_OUTPUT}"
  71. -
  72. uses: actions/checkout@v4
  73. if: ${{ !fromJson(steps.check.outputs.skip) }}
  74. with:
  75. ref: ${{ steps.check.outputs.ref }}
  76. -
  77. name: Set up Docker Buildx
  78. uses: docker/setup-buildx-action@v3
  79. with:
  80. version: latest
  81. -
  82. name: Create variants matrix
  83. if: ${{ !fromJson(steps.check.outputs.skip) }}
  84. id: matrix
  85. run: |
  86. METADATA="$(docker buildx bake --print | jq -c)"
  87. {
  88. echo metadata="${METADATA}"
  89. echo variants="$(jq -c '.group.default.targets|map(sub("runner-|builder-"; ""))|unique' <<< "${METADATA}")"
  90. echo platforms="$(jq -c 'first(.target[]) | .platforms' <<< "${METADATA}")"
  91. } >> "${GITHUB_OUTPUT}"
  92. env:
  93. SHA: ${{ github.sha }}
  94. VERSION: ${{ (github.ref_type == 'tag' && github.ref_name) || steps.check.outputs.ref || github.sha }}
  95. PHP_VERSION: ${{ steps.check.outputs.php_version }}
  96. build:
  97. runs-on: ubuntu-latest
  98. needs:
  99. - prepare
  100. if: ${{ !fromJson(needs.prepare.outputs.skip) }}
  101. strategy:
  102. fail-fast: false
  103. matrix:
  104. variant: ${{ fromJson(needs.prepare.outputs.variants) }}
  105. platform: ${{ fromJson(needs.prepare.outputs.platforms) }}
  106. include:
  107. -
  108. race: ""
  109. qemu: true
  110. -
  111. platform: linux/amd64
  112. qemu: false
  113. race: "-race" # The Go race detector is only supported on amd64
  114. -
  115. platform: linux/386
  116. qemu: false
  117. exclude:
  118. # arm/v6 is only available for Alpine: https://github.com/docker-library/golang/issues/502
  119. -
  120. variant: php-${{ needs.prepare.outputs.php82_version }}-bookworm
  121. platform: linux/arm/v6
  122. -
  123. variant: php-${{ needs.prepare.outputs.php83_version }}-bookworm
  124. platform: linux/arm/v6
  125. steps:
  126. -
  127. uses: actions/checkout@v4
  128. with:
  129. ref: ${{ needs.prepare.outputs.ref }}
  130. -
  131. name: Set up QEMU
  132. if: matrix.qemu
  133. uses: docker/setup-qemu-action@v3
  134. with:
  135. platforms: ${{ matrix.platform }}
  136. -
  137. name: Set up Docker Buildx
  138. uses: docker/setup-buildx-action@v3
  139. with:
  140. platforms: ${{ matrix.platform }}
  141. version: latest
  142. -
  143. name: Login to DockerHub
  144. if: fromJson(needs.prepare.outputs.push)
  145. uses: docker/login-action@v3
  146. with:
  147. username: ${{ secrets.REGISTRY_USERNAME }}
  148. password: ${{ secrets.REGISTRY_PASSWORD }}
  149. -
  150. name: Build
  151. id: build
  152. uses: docker/bake-action@v4
  153. with:
  154. pull: true
  155. load: ${{ !fromJson(needs.prepare.outputs.push) }}
  156. targets: |
  157. builder-${{ matrix.variant }}
  158. runner-${{ matrix.variant }}
  159. # Remove tags to prevent "can't push tagged ref [...] by digest" error
  160. set: |
  161. *.tags=
  162. *.platform=${{ matrix.platform }}
  163. *.cache-from=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-${{ matrix.platform }}
  164. *.cache-from=type=gha,scope=refs/heads/main-${{ matrix.platform }}
  165. *.cache-to=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-${{ matrix.platform }},ignore-error=true
  166. ${{ fromJson(needs.prepare.outputs.push) && format('*.output=type=image,name={0},push-by-digest=true,name-canonical=true,push=true', env.IMAGE_NAME) || '' }}
  167. env:
  168. SHA: ${{ github.sha }}
  169. VERSION: ${{ github.ref_type == 'tag' && github.ref_name || needs.prepare.outputs.ref || github.sha }}
  170. PHP_VERSION: ${{ needs.prepare.outputs.php_version }}
  171. -
  172. # Workaround for https://github.com/actions/runner/pull/2477#issuecomment-1501003600
  173. name: Export metadata
  174. if: fromJson(needs.prepare.outputs.push)
  175. run: |
  176. mkdir -p /tmp/metadata/builder /tmp/metadata/runner
  177. builderDigest=$(jq -r '."builder-${{ matrix.variant }}"."containerimage.digest"' <<< "${METADATA}")
  178. touch "/tmp/metadata/builder/${builderDigest#sha256:}"
  179. runnerDigest=$(jq -r '."runner-${{ matrix.variant }}"."containerimage.digest"' <<< "${METADATA}")
  180. touch "/tmp/metadata/runner/${runnerDigest#sha256:}"
  181. env:
  182. METADATA: ${{ steps.build.outputs.metadata }}
  183. -
  184. name: Upload builder metadata
  185. if: fromJson(needs.prepare.outputs.push)
  186. uses: actions/upload-artifact@v3
  187. with:
  188. name: metadata-builder-${{ matrix.variant }}
  189. path: /tmp/metadata/builder/*
  190. if-no-files-found: error
  191. retention-days: 1
  192. -
  193. name: Upload runner metadata
  194. if: fromJson(needs.prepare.outputs.push)
  195. uses: actions/upload-artifact@v3
  196. with:
  197. name: metadata-runner-${{ matrix.variant }}
  198. path: /tmp/metadata/runner/*
  199. if-no-files-found: error
  200. retention-days: 1
  201. -
  202. name: Run tests
  203. if: ${{ !matrix.qemu && !fromJson(needs.prepare.outputs.push) }}
  204. run: |
  205. docker run --platform=${{ matrix.platform }} --rm \
  206. "$(jq -r '."builder-${{ matrix.variant }}"."containerimage.config.digest"' <<< "${METADATA}")" \
  207. sh -c 'go test ${{ matrix.race }} -v ./... && cd caddy && go test ${{ matrix.race }} -v ./...'
  208. env:
  209. METADATA: ${{ steps.build.outputs.metadata }}
  210. # Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
  211. push:
  212. runs-on: ubuntu-latest
  213. needs:
  214. - prepare
  215. - build
  216. if: fromJson(needs.prepare.outputs.push)
  217. strategy:
  218. fail-fast: false
  219. matrix:
  220. variant: ${{ fromJson(needs.prepare.outputs.variants) }}
  221. target: ['builder', 'runner']
  222. steps:
  223. -
  224. name: Download metadata
  225. uses: actions/download-artifact@v3
  226. with:
  227. name: metadata-${{ matrix.target }}-${{ matrix.variant }}
  228. path: /tmp/metadata
  229. -
  230. name: Set up Docker Buildx
  231. uses: docker/setup-buildx-action@v3
  232. with:
  233. # Temporary fix for https://github.com/docker/buildx/issues/2229
  234. version: "https://github.com/docker/buildx.git#master"
  235. -
  236. name: Login to DockerHub
  237. uses: docker/login-action@v3
  238. with:
  239. username: ${{ secrets.REGISTRY_USERNAME }}
  240. password: ${{ secrets.REGISTRY_PASSWORD }}
  241. -
  242. name: Create manifest list and push
  243. working-directory: /tmp/metadata
  244. run: |
  245. set -x
  246. # shellcheck disable=SC2046,SC2086
  247. docker buildx imagetools create $(jq -cr '.target."${{ matrix.target }}-${{ matrix.variant }}".tags | map("-t " + .) | join(" ")' <<< ${METADATA}) \
  248. $(printf "${IMAGE_NAME}@sha256:%s " *)
  249. env:
  250. METADATA: ${{ needs.prepare.outputs.metadata }}
  251. -
  252. name: Inspect image
  253. run: |
  254. # shellcheck disable=SC2046,SC2086
  255. docker buildx imagetools inspect $(jq -cr '.target."${{ matrix.target }}-${{ matrix.variant }}".tags | first' <<< ${METADATA})
  256. env:
  257. METADATA: ${{ needs.prepare.outputs.metadata }}