docker.yaml 12 KB

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