docker.yaml 10 KB

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