docker.yaml 9.1 KB

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