static.yaml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ---
  2. name: Build binary releases
  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. version:
  15. description: 'FrankenPHP version'
  16. required: false
  17. type: string
  18. schedule:
  19. - cron: '0 0 * * *'
  20. env:
  21. IMAGE_NAME: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/')) && 'dunglas/frankenphp' || 'dunglas/frankenphp-dev' }}
  22. LATEST: ${{ (github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/')) && '0' || '1' }}
  23. jobs:
  24. prepare:
  25. runs-on: ubuntu-latest
  26. outputs:
  27. push: ${{ toJson((steps.check.outputs.ref || (github.event_name == 'workflow_dispatch' && inputs.version) || startsWith(github.ref, 'refs/tags/') || (github.ref == 'refs/heads/main' && github.event_name != 'pull_request')) && true || false) }}
  28. platforms: ${{ steps.matrix.outputs.platforms }}
  29. metadata: ${{ steps.matrix.outputs.metadata }}
  30. ref: ${{ steps.check.outputs.ref }}
  31. steps:
  32. -
  33. name: Get version
  34. id: check
  35. if: github.event_name == 'schedule'
  36. run: |
  37. ref="${{ (github.ref_type == 'tag' && github.ref_name) || (github.event_name == 'workflow_dispatch' && inputs.version) || '' }}"
  38. if [[ -z "${ref}" ]]; then
  39. ref="$(gh release view --repo dunglas/frankenphp --json tagName --jq '.tagName')"
  40. fi
  41. echo "ref=${ref}" >> "${GITHUB_OUTPUT}"
  42. env:
  43. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  44. -
  45. uses: actions/checkout@v4
  46. with:
  47. ref: ${{ steps.check.outputs.ref }}
  48. -
  49. name: Set up Docker Buildx
  50. uses: docker/setup-buildx-action@v3
  51. with:
  52. version: latest
  53. -
  54. name: Create platforms matrix
  55. id: matrix
  56. run: |
  57. METADATA="$(docker buildx bake --print static-builder | jq -c)"
  58. {
  59. echo metadata="${METADATA}"
  60. echo platforms="$(jq -c 'first(.target[]) | .platforms' <<< "${METADATA}")"
  61. } >> "${GITHUB_OUTPUT}"
  62. env:
  63. SHA: ${{ github.sha }}
  64. VERSION: ${{ steps.check.outputs.ref || github.sha }}
  65. build-linux:
  66. strategy:
  67. fail-fast: false
  68. matrix:
  69. platform: ${{ fromJson(needs.prepare.outputs.platforms) }}
  70. include:
  71. - race: ""
  72. qemu: true
  73. - platform: linux/amd64
  74. qemu: false
  75. name: Build ${{ matrix.platform }} static binary
  76. runs-on: ubuntu-latest
  77. needs: [ prepare ]
  78. steps:
  79. -
  80. uses: actions/checkout@v4
  81. with:
  82. ref: ${{ needs.prepare.outputs.ref }}
  83. -
  84. name: Set up QEMU
  85. if: matrix.qemu
  86. uses: docker/setup-qemu-action@v3
  87. with:
  88. platforms: ${{ matrix.platform }}
  89. -
  90. name: Set up Docker Buildx
  91. uses: docker/setup-buildx-action@v3
  92. with:
  93. platforms: ${{ matrix.platform }}
  94. version: latest
  95. -
  96. name: Login to DockerHub
  97. if: ${{ fromJson(needs.prepare.outputs.push) }}
  98. uses: docker/login-action@v3
  99. with:
  100. username: ${{ secrets.REGISTRY_USERNAME }}
  101. password: ${{ secrets.REGISTRY_PASSWORD }}
  102. -
  103. name: Build
  104. id: build
  105. uses: docker/bake-action@v4
  106. with:
  107. pull: true
  108. load: ${{ !fromJson(needs.prepare.outputs.push) }}
  109. targets: static-builder
  110. set: |
  111. *.tags=
  112. *.platform=${{ matrix.platform }}
  113. *.cache-from=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder
  114. *.cache-from=type=gha,scope=refs/heads/main-static-builder
  115. *.cache-to=type=gha,scope=${{ needs.prepare.outputs.ref || github.ref }}-static-builder,ignore-error=true
  116. ${{ fromJson(needs.prepare.outputs.push) && format('*.output=type=image,name={0},push-by-digest=true,name-canonical=true,push=true', env.IMAGE_NAME) || '' }}
  117. env:
  118. SHA: ${{ github.sha }}
  119. VERSION: ${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref || github.sha}}
  120. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  121. -
  122. # Workaround for https://github.com/actions/runner/pull/2477#issuecomment-1501003600
  123. name: Export metadata
  124. if: fromJson(needs.prepare.outputs.push)
  125. run: |
  126. mkdir -p /tmp/metadata
  127. # shellcheck disable=SC2086
  128. digest=$(jq -r '."static-builder"."containerimage.digest"' <<< ${METADATA})
  129. touch "/tmp/metadata/${digest#sha256:}"
  130. env:
  131. METADATA: ${{ steps.build.outputs.metadata }}
  132. -
  133. name: Upload metadata
  134. if: fromJson(needs.prepare.outputs.push)
  135. uses: actions/upload-artifact@v3
  136. with:
  137. name: metadata-static-builder
  138. path: /tmp/metadata/*
  139. if-no-files-found: error
  140. retention-days: 1
  141. -
  142. name: Copy binary
  143. if: ${{ !fromJson(needs.prepare.outputs.push) }}
  144. run: |
  145. digest=$(jq -r '."static-builder"."containerimage.config.digest"' <<< "${METADATA}")
  146. docker create --platform=${{ matrix.platform }} --name static-builder "${digest}"
  147. docker cp "static-builder:/go/src/app/dist/${BINARY}" "${BINARY}"
  148. env:
  149. METADATA: ${{ steps.build.outputs.metadata }}
  150. BINARY: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}
  151. -
  152. name: Upload artifact
  153. if: ${{ !fromJson(needs.prepare.outputs.push) }}
  154. uses: actions/upload-artifact@v3
  155. with:
  156. name: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}
  157. path: frankenphp-linux-${{ matrix.platform == 'linux/amd64' && 'x86_64' || 'aarch64' }}
  158. # Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
  159. push:
  160. runs-on: ubuntu-latest
  161. needs:
  162. - prepare
  163. - build-linux
  164. if: fromJson(needs.prepare.outputs.push)
  165. #if: fromJson(needs.prepare.outputs.push) && (needs.prepare.outputs.ref || github.ref_type == 'tag')
  166. steps:
  167. -
  168. name: Download metadata
  169. uses: actions/download-artifact@v3
  170. with:
  171. name: metadata-static-builder
  172. path: /tmp/metadata
  173. -
  174. name: Set up Docker Buildx
  175. uses: docker/setup-buildx-action@v3
  176. with:
  177. version: latest
  178. -
  179. name: Login to DockerHub
  180. uses: docker/login-action@v3
  181. with:
  182. username: ${{ secrets.REGISTRY_USERNAME }}
  183. password: ${{ secrets.REGISTRY_PASSWORD }}
  184. -
  185. name: Create manifest list and push
  186. working-directory: /tmp/metadata
  187. run: |
  188. # shellcheck disable=SC2046,SC2086
  189. docker buildx imagetools create $(jq -cr '.target."static-builder".tags | map("-t " + .) | join(" ")' <<< "${METADATA}") \
  190. $(printf "${IMAGE_NAME}@sha256:%s " *)
  191. env:
  192. METADATA: ${{ needs.prepare.outputs.metadata }}
  193. -
  194. name: Inspect image
  195. run: |
  196. # shellcheck disable=SC2046,SC2086
  197. docker buildx imagetools inspect "$(jq -cr '.target."static-builder".tags | first' <<< "${METADATA}")"
  198. env:
  199. METADATA: ${{ needs.prepare.outputs.metadata }}
  200. -
  201. name: Copy binary
  202. run: |
  203. tag=$(jq -cr '.target."static-builder".tags | first' <<< "${METADATA}")
  204. docker cp "$(docker create --platform=linux/amd64 --name static-builder "${tag}"):/go/src/app/dist/frankenphp-linux-x86_64" frankenphp-linux-x86_64 ; docker rm static-builder
  205. docker cp "$(docker create --platform=linux/arm64 --name static-builder "${tag}"):/go/src/app/dist/frankenphp-linux-aarch64" frankenphp-linux-aarch64 ; docker rm static-builder
  206. env:
  207. METADATA: ${{ needs.prepare.outputs.metadata }}
  208. -
  209. name: Upload asset
  210. if: needs.prepare.outputs.ref || github.ref_type == 'tag'
  211. run: gh release upload "${{ (github.ref_type == 'tag' && github.ref_name) || needs.prepare.outputs.ref }}" frankenphp-linux-x86_64 frankenphp-linux-aarch64 --repo dunglas/frankenphp --clobber
  212. env:
  213. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  214. build-mac:
  215. strategy:
  216. fail-fast: false
  217. matrix:
  218. platform: ['arm64', 'x86_64']
  219. name: Build macOS ${{ matrix.platform }} binaries
  220. runs-on: ${{ matrix.platform == 'arm64' && 'macos-14' || 'macos-13' }}
  221. needs: [ prepare ]
  222. env:
  223. HOMEBREW_NO_AUTO_UPDATE: 1
  224. steps:
  225. -
  226. uses: actions/checkout@v4
  227. with:
  228. ref: ${{ needs.prepare.outputs.ref }}
  229. -
  230. uses: actions/setup-go@v5
  231. with:
  232. go-version: '1.22'
  233. cache-dependency-path: |
  234. go.sum
  235. caddy/go.sum
  236. -
  237. name: Set FRANKENPHP_VERSION
  238. run: |
  239. if [ "${GITHUB_REF_TYPE}" == "tag" ]; then
  240. export FRANKENPHP_VERSION=${GITHUB_REF_NAME:1}
  241. elif [ "${GITHUB_EVENT_NAME}" == "schedule" ]; then
  242. export FRANKENPHP_VERSION="${{ needs.prepare.outputs.ref }}"
  243. else
  244. export FRANKENPHP_VERSION=${GITHUB_SHA}
  245. fi
  246. echo "FRANKENPHP_VERSION=${FRANKENPHP_VERSION}" >> "${GITHUB_ENV}"
  247. -
  248. name: Build FrankenPHP
  249. run: ./build-static.sh
  250. env:
  251. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  252. RELEASE: ${{ (needs.prepare.outputs.ref || github.ref_type == 'tag') && '1' || '' }}
  253. -
  254. name: Upload artifact
  255. if: github.ref_type == 'branch'
  256. uses: actions/upload-artifact@v3
  257. with:
  258. name: frankenphp-mac-${{ matrix.platform }}
  259. path: dist/frankenphp-mac-${{ matrix.platform }}