docker.yaml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #checkov:skip=CKV_GHA_7
  20. version:
  21. description: 'FrankenPHP version'
  22. required: false
  23. type: string
  24. schedule:
  25. - cron: '0 4 * * *'
  26. permissions:
  27. contents: read
  28. env:
  29. IMAGE_NAME: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/')) && 'dunglas/frankenphp' || 'dunglas/frankenphp-dev' }}
  30. jobs:
  31. prepare:
  32. runs-on: ubuntu-latest
  33. outputs:
  34. # Push if it's a scheduled job, a tag, or if we're committing to the main branch
  35. 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 }}
  36. variants: ${{ steps.matrix.outputs.variants }}
  37. platforms: ${{ steps.matrix.outputs.platforms }}
  38. metadata: ${{ steps.matrix.outputs.metadata }}
  39. php_version: ${{ steps.check.outputs.php_version }}
  40. php82_version: ${{ steps.check.outputs.php82_version }}
  41. php83_version: ${{ steps.check.outputs.php83_version }}
  42. skip: ${{ steps.check.outputs.skip }}
  43. ref: ${{ steps.check.outputs.ref || (github.event_name == 'workflow_dispatch' && inputs.version) || '' }}
  44. steps:
  45. -
  46. name: Check PHP versions
  47. id: check
  48. env:
  49. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  50. run: |
  51. 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="; "")')
  52. 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="; "")')
  53. {
  54. echo php_version="${PHP_83_LATEST},${PHP_82_LATEST}"
  55. echo php82_version="${PHP_82_LATEST//./-}"
  56. echo php83_version="${PHP_83_LATEST//./-}"
  57. } >> "${GITHUB_OUTPUT}"
  58. # Check if the Docker images must be rebuilt
  59. if [[ "${GITHUB_EVENT_NAME}" != "schedule" ]]; then
  60. echo skip=false >> "${GITHUB_OUTPUT}"
  61. exit 0
  62. fi
  63. 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="; "")')
  64. 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="; "")')
  65. if [[ "${FRANKENPHP_82_LATEST}" == "${PHP_82_LATEST}" ]] && [[ "${FRANKENPHP_83_LATEST}" == "${PHP_83_LATEST}" ]]; then
  66. echo skip=true >> "${GITHUB_OUTPUT}"
  67. exit 0
  68. fi
  69. {
  70. echo ref="$(gh release view --repo dunglas/frankenphp --json tagName --jq '.tagName')"
  71. echo skip=false
  72. } >> "${GITHUB_OUTPUT}"
  73. -
  74. uses: actions/checkout@v4
  75. if: ${{ !fromJson(steps.check.outputs.skip) }}
  76. with:
  77. ref: ${{ steps.check.outputs.ref }}
  78. -
  79. name: Set up Docker Buildx
  80. if: ${{ !fromJson(steps.check.outputs.skip) }}
  81. uses: docker/setup-buildx-action@v3
  82. -
  83. name: Create variants matrix
  84. if: ${{ !fromJson(steps.check.outputs.skip) }}
  85. id: matrix
  86. run: |
  87. METADATA="$(docker buildx bake --print | jq -c)"
  88. {
  89. echo metadata="${METADATA}"
  90. echo variants="$(jq -c '.group.default.targets|map(sub("runner-|builder-"; ""))|unique' <<< "${METADATA}")"
  91. echo platforms="$(jq -c 'first(.target[]) | .platforms' <<< "${METADATA}")"
  92. } >> "${GITHUB_OUTPUT}"
  93. env:
  94. SHA: ${{ github.sha }}
  95. VERSION: ${{ (github.ref_type == 'tag' && github.ref_name) || steps.check.outputs.ref || 'dev' }}
  96. PHP_VERSION: ${{ steps.check.outputs.php_version }}
  97. build:
  98. runs-on: ubuntu-latest
  99. needs:
  100. - prepare
  101. if: ${{ !fromJson(needs.prepare.outputs.skip) }}
  102. strategy:
  103. fail-fast: false
  104. matrix:
  105. variant: ${{ fromJson(needs.prepare.outputs.variants) }}
  106. platform: ${{ fromJson(needs.prepare.outputs.platforms) }}
  107. include:
  108. -
  109. race: ""
  110. qemu: true
  111. -
  112. platform: linux/amd64
  113. qemu: false
  114. race: "-race" # The Go race detector is only supported on amd64
  115. -
  116. platform: linux/386
  117. qemu: false
  118. exclude:
  119. # arm/v6 is only available for Alpine: https://github.com/docker-library/golang/issues/502
  120. -
  121. variant: php-${{ needs.prepare.outputs.php82_version }}-bookworm
  122. platform: linux/arm/v6
  123. -
  124. variant: php-${{ needs.prepare.outputs.php83_version }}-bookworm
  125. platform: linux/arm/v6
  126. steps:
  127. -
  128. uses: actions/checkout@v4
  129. with:
  130. ref: ${{ needs.prepare.outputs.ref }}
  131. -
  132. name: Set up QEMU
  133. if: matrix.qemu
  134. uses: docker/setup-qemu-action@v3
  135. with:
  136. platforms: ${{ matrix.platform }}
  137. -
  138. name: Set up Docker Buildx
  139. uses: docker/setup-buildx-action@v3
  140. with:
  141. platforms: ${{ matrix.platform }}
  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@v5
  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 || 'dev' }}
  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. -
  233. name: Login to DockerHub
  234. uses: docker/login-action@v3
  235. with:
  236. username: ${{ secrets.REGISTRY_USERNAME }}
  237. password: ${{ secrets.REGISTRY_PASSWORD }}
  238. -
  239. name: Create manifest list and push
  240. working-directory: /tmp/metadata
  241. run: |
  242. set -x
  243. # shellcheck disable=SC2046,SC2086
  244. docker buildx imagetools create $(jq -cr '.target."${{ matrix.target }}-${{ matrix.variant }}".tags | map("-t " + .) | join(" ")' <<< ${METADATA}) \
  245. $(printf "${IMAGE_NAME}@sha256:%s " *)
  246. env:
  247. METADATA: ${{ needs.prepare.outputs.metadata }}
  248. -
  249. name: Inspect image
  250. run: |
  251. # shellcheck disable=SC2046,SC2086
  252. docker buildx imagetools inspect $(jq -cr '.target."${{ matrix.target }}-${{ matrix.variant }}".tags | first' <<< ${METADATA})
  253. env:
  254. METADATA: ${{ needs.prepare.outputs.metadata }}