docker.yaml 10.0 KB

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