docker.yaml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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: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: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. ${{ (github.event_name == 'pull_request') && '*.args.NO_COMPRESS=1' || '' }}
  162. *.tags=
  163. *.platform=${{ matrix.platform }}
  164. builder-${{ matrix.variant }}.cache-from=type=gha,scope=builder-${{ matrix.variant }}-${{ needs.prepare.outputs.ref || github.ref }}-${{ matrix.platform }}
  165. builder-${{ matrix.variant }}.cache-from=type=gha,scope=refs/heads/main-builder-${{ matrix.variant }}-${{ matrix.platform }}
  166. builder-${{ matrix.variant }}.cache-to=type=gha,scope=builder-${{ matrix.variant }}-${{ needs.prepare.outputs.ref || github.ref }}-${{ matrix.platform }},ignore-error=true
  167. runner-${{ matrix.variant }}.cache-from=type=gha,scope=runner-${{ matrix.variant }}-${{ needs.prepare.outputs.ref || github.ref }}-${{ matrix.platform }}
  168. runner-${{ matrix.variant }}.cache-from=type=gha,scope=refs/heads/main-runner-${{ matrix.variant }}-${{ matrix.platform }}
  169. runner-${{ matrix.variant }}.cache-to=type=gha,scope=runner-${{ matrix.variant }}-${{ needs.prepare.outputs.ref || github.ref }}-${{ matrix.platform }},ignore-error=true
  170. ${{ fromJson(needs.prepare.outputs.push) && format('*.output=type=image,name={0},push-by-digest=true,name-canonical=true,push=true', env.IMAGE_NAME) || '' }}
  171. env:
  172. SHA: ${{ github.sha }}
  173. VERSION: ${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref || 'dev' }}
  174. PHP_VERSION: ${{ needs.prepare.outputs.php_version }}
  175. -
  176. # Workaround for https://github.com/actions/runner/pull/2477#issuecomment-1501003600
  177. name: Export metadata
  178. if: fromJson(needs.prepare.outputs.push)
  179. run: |
  180. mkdir -p /tmp/metadata/builder /tmp/metadata/runner
  181. builderDigest=$(jq -r '."builder-${{ matrix.variant }}"."containerimage.digest"' <<< "${METADATA}")
  182. touch "/tmp/metadata/builder/${builderDigest#sha256:}"
  183. runnerDigest=$(jq -r '."runner-${{ matrix.variant }}"."containerimage.digest"' <<< "${METADATA}")
  184. touch "/tmp/metadata/runner/${runnerDigest#sha256:}"
  185. env:
  186. METADATA: ${{ steps.build.outputs.metadata }}
  187. -
  188. name: Upload builder metadata
  189. if: fromJson(needs.prepare.outputs.push)
  190. uses: actions/upload-artifact@v3
  191. with:
  192. name: metadata-builder-${{ matrix.variant }}
  193. path: /tmp/metadata/builder/*
  194. if-no-files-found: error
  195. retention-days: 1
  196. -
  197. name: Upload runner metadata
  198. if: fromJson(needs.prepare.outputs.push)
  199. uses: actions/upload-artifact@v3
  200. with:
  201. name: metadata-runner-${{ matrix.variant }}
  202. path: /tmp/metadata/runner/*
  203. if-no-files-found: error
  204. retention-days: 1
  205. -
  206. name: Run tests
  207. if: ${{ !matrix.qemu && !fromJson(needs.prepare.outputs.push) }}
  208. run: |
  209. docker run --platform=${{ matrix.platform }} --rm \
  210. "$(jq -r '."builder-${{ matrix.variant }}"."containerimage.config.digest"' <<< "${METADATA}")" \
  211. sh -c 'go test -tags ${{ matrix.race }} -v ./... && cd caddy && go test -tags nobadger,nomysql,nopgx ${{ matrix.race }} -v ./...'
  212. env:
  213. METADATA: ${{ steps.build.outputs.metadata }}
  214. # Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
  215. push:
  216. runs-on: ubuntu-latest
  217. needs:
  218. - prepare
  219. - build
  220. if: fromJson(needs.prepare.outputs.push)
  221. strategy:
  222. fail-fast: false
  223. matrix:
  224. variant: ${{ fromJson(needs.prepare.outputs.variants) }}
  225. target: ['builder', 'runner']
  226. steps:
  227. -
  228. name: Download metadata
  229. uses: actions/download-artifact@v3
  230. with:
  231. name: metadata-${{ matrix.target }}-${{ matrix.variant }}
  232. path: /tmp/metadata
  233. -
  234. name: Set up Docker Buildx
  235. uses: docker/setup-buildx-action@v3
  236. -
  237. name: Login to DockerHub
  238. uses: docker/login-action@v3
  239. with:
  240. username: ${{ secrets.REGISTRY_USERNAME }}
  241. password: ${{ secrets.REGISTRY_PASSWORD }}
  242. -
  243. name: Create manifest list and push
  244. working-directory: /tmp/metadata
  245. run: |
  246. set -x
  247. # shellcheck disable=SC2046,SC2086
  248. docker buildx imagetools create $(jq -cr '.target."${{ matrix.target }}-${{ matrix.variant }}".tags | map("-t " + .) | join(" ")' <<< ${METADATA}) \
  249. $(printf "${IMAGE_NAME}@sha256:%s " *)
  250. env:
  251. METADATA: ${{ needs.prepare.outputs.metadata }}
  252. -
  253. name: Inspect image
  254. run: |
  255. # shellcheck disable=SC2046,SC2086
  256. docker buildx imagetools inspect $(jq -cr '.target."${{ matrix.target }}-${{ matrix.variant }}".tags | first' <<< ${METADATA})
  257. env:
  258. METADATA: ${{ needs.prepare.outputs.metadata }}