docker-build.yaml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. name: Create and publish Docker images with specific build args
  2. on:
  3. workflow_dispatch:
  4. push:
  5. branches:
  6. - main
  7. - dev
  8. tags:
  9. - v*
  10. env:
  11. REGISTRY: ghcr.io
  12. IMAGE_NAME: ${{ github.repository }}
  13. FULL_IMAGE_NAME: ghcr.io/${{ github.repository }}
  14. jobs:
  15. build-main-image:
  16. runs-on: ubuntu-latest
  17. permissions:
  18. contents: read
  19. packages: write
  20. strategy:
  21. fail-fast: false
  22. matrix:
  23. platform:
  24. - linux/amd64
  25. - linux/arm64
  26. steps:
  27. - name: Prepare
  28. run: |
  29. platform=${{ matrix.platform }}
  30. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  31. - name: Checkout repository
  32. uses: actions/checkout@v4
  33. - name: Set up QEMU
  34. uses: docker/setup-qemu-action@v3
  35. - name: Set up Docker Buildx
  36. uses: docker/setup-buildx-action@v3
  37. - name: Log in to the Container registry
  38. uses: docker/login-action@v3
  39. with:
  40. registry: ${{ env.REGISTRY }}
  41. username: ${{ github.actor }}
  42. password: ${{ secrets.GITHUB_TOKEN }}
  43. - name: Extract metadata for Docker images (default latest tag)
  44. id: meta
  45. uses: docker/metadata-action@v5
  46. with:
  47. images: ${{ env.FULL_IMAGE_NAME }}
  48. tags: |
  49. type=ref,event=branch
  50. type=ref,event=tag
  51. type=sha,prefix=git-
  52. type=semver,pattern={{version}}
  53. type=semver,pattern={{major}}.{{minor}}
  54. flavor: |
  55. latest=${{ github.ref == 'refs/heads/main' }}
  56. - name: Extract metadata for Docker cache
  57. id: cache-meta
  58. uses: docker/metadata-action@v5
  59. with:
  60. images: ${{ env.FULL_IMAGE_NAME }}
  61. tags: |
  62. type=ref,event=branch
  63. flavor: |
  64. prefix=cache-${{ matrix.platform }}-
  65. - name: Build Docker image (latest)
  66. uses: docker/build-push-action@v5
  67. id: build
  68. with:
  69. context: .
  70. push: true
  71. platforms: ${{ matrix.platform }}
  72. labels: ${{ steps.meta.outputs.labels }}
  73. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  74. cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
  75. cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
  76. build-args: |
  77. BUILD_HASH=${{ github.sha }}
  78. - name: Export digest
  79. run: |
  80. mkdir -p /tmp/digests
  81. digest="${{ steps.build.outputs.digest }}"
  82. touch "/tmp/digests/${digest#sha256:}"
  83. - name: Upload digest
  84. uses: actions/upload-artifact@v4
  85. with:
  86. name: digests-main-${{ env.PLATFORM_PAIR }}
  87. path: /tmp/digests/*
  88. if-no-files-found: error
  89. retention-days: 1
  90. build-cuda-image:
  91. runs-on: ubuntu-latest
  92. permissions:
  93. contents: read
  94. packages: write
  95. strategy:
  96. fail-fast: false
  97. matrix:
  98. platform:
  99. - linux/amd64
  100. - linux/arm64
  101. steps:
  102. - name: Prepare
  103. run: |
  104. platform=${{ matrix.platform }}
  105. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  106. - name: Checkout repository
  107. uses: actions/checkout@v4
  108. - name: Set up QEMU
  109. uses: docker/setup-qemu-action@v3
  110. - name: Set up Docker Buildx
  111. uses: docker/setup-buildx-action@v3
  112. - name: Log in to the Container registry
  113. uses: docker/login-action@v3
  114. with:
  115. registry: ${{ env.REGISTRY }}
  116. username: ${{ github.actor }}
  117. password: ${{ secrets.GITHUB_TOKEN }}
  118. - name: Extract metadata for Docker images (cuda tag)
  119. id: meta
  120. uses: docker/metadata-action@v5
  121. with:
  122. images: ${{ env.FULL_IMAGE_NAME }}
  123. tags: |
  124. type=ref,event=branch
  125. type=ref,event=tag
  126. type=sha,prefix=git-
  127. type=semver,pattern={{version}}
  128. type=semver,pattern={{major}}.{{minor}}
  129. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=cuda
  130. flavor: |
  131. latest=${{ github.ref == 'refs/heads/main' }}
  132. suffix=-cuda,onlatest=true
  133. - name: Extract metadata for Docker cache
  134. id: cache-meta
  135. uses: docker/metadata-action@v5
  136. with:
  137. images: ${{ env.FULL_IMAGE_NAME }}
  138. tags: |
  139. type=ref,event=branch
  140. flavor: |
  141. prefix=cache-cuda-${{ matrix.platform }}-
  142. - name: Build Docker image (cuda)
  143. uses: docker/build-push-action@v5
  144. id: build
  145. with:
  146. context: .
  147. push: true
  148. platforms: ${{ matrix.platform }}
  149. labels: ${{ steps.meta.outputs.labels }}
  150. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  151. cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
  152. cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
  153. build-args: |
  154. BUILD_HASH=${{ github.sha }}
  155. USE_CUDA=true
  156. - name: Export digest
  157. run: |
  158. mkdir -p /tmp/digests
  159. digest="${{ steps.build.outputs.digest }}"
  160. touch "/tmp/digests/${digest#sha256:}"
  161. - name: Upload digest
  162. uses: actions/upload-artifact@v4
  163. with:
  164. name: digests-cuda-${{ env.PLATFORM_PAIR }}
  165. path: /tmp/digests/*
  166. if-no-files-found: error
  167. retention-days: 1
  168. build-ollama-image:
  169. runs-on: ubuntu-latest
  170. permissions:
  171. contents: read
  172. packages: write
  173. strategy:
  174. fail-fast: false
  175. matrix:
  176. platform:
  177. - linux/amd64
  178. - linux/arm64
  179. steps:
  180. - name: Prepare
  181. run: |
  182. platform=${{ matrix.platform }}
  183. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  184. - name: Checkout repository
  185. uses: actions/checkout@v4
  186. - name: Set up QEMU
  187. uses: docker/setup-qemu-action@v3
  188. - name: Set up Docker Buildx
  189. uses: docker/setup-buildx-action@v3
  190. - name: Log in to the Container registry
  191. uses: docker/login-action@v3
  192. with:
  193. registry: ${{ env.REGISTRY }}
  194. username: ${{ github.actor }}
  195. password: ${{ secrets.GITHUB_TOKEN }}
  196. - name: Extract metadata for Docker images (ollama tag)
  197. id: meta
  198. uses: docker/metadata-action@v5
  199. with:
  200. images: ${{ env.FULL_IMAGE_NAME }}
  201. tags: |
  202. type=ref,event=branch
  203. type=ref,event=tag
  204. type=sha,prefix=git-
  205. type=semver,pattern={{version}}
  206. type=semver,pattern={{major}}.{{minor}}
  207. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=ollama
  208. flavor: |
  209. latest=${{ github.ref == 'refs/heads/main' }}
  210. suffix=-ollama,onlatest=true
  211. - name: Extract metadata for Docker cache
  212. id: cache-meta
  213. uses: docker/metadata-action@v5
  214. with:
  215. images: ${{ env.FULL_IMAGE_NAME }}
  216. tags: |
  217. type=ref,event=branch
  218. flavor: |
  219. prefix=cache-ollama-${{ matrix.platform }}-
  220. - name: Build Docker image (ollama)
  221. uses: docker/build-push-action@v5
  222. id: build
  223. with:
  224. context: .
  225. push: true
  226. platforms: ${{ matrix.platform }}
  227. labels: ${{ steps.meta.outputs.labels }}
  228. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  229. cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }}
  230. cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max
  231. build-args: |
  232. BUILD_HASH=${{ github.sha }}
  233. USE_OLLAMA=true
  234. - name: Export digest
  235. run: |
  236. mkdir -p /tmp/digests
  237. digest="${{ steps.build.outputs.digest }}"
  238. touch "/tmp/digests/${digest#sha256:}"
  239. - name: Upload digest
  240. uses: actions/upload-artifact@v4
  241. with:
  242. name: digests-ollama-${{ env.PLATFORM_PAIR }}
  243. path: /tmp/digests/*
  244. if-no-files-found: error
  245. retention-days: 1
  246. merge-main-images:
  247. runs-on: ubuntu-latest
  248. needs: [ build-main-image ]
  249. steps:
  250. - name: Download digests
  251. uses: actions/download-artifact@v4
  252. with:
  253. pattern: digests-main-*
  254. path: /tmp/digests
  255. merge-multiple: true
  256. - name: Set up Docker Buildx
  257. uses: docker/setup-buildx-action@v3
  258. - name: Log in to the Container registry
  259. uses: docker/login-action@v3
  260. with:
  261. registry: ${{ env.REGISTRY }}
  262. username: ${{ github.actor }}
  263. password: ${{ secrets.GITHUB_TOKEN }}
  264. - name: Extract metadata for Docker images (default latest tag)
  265. id: meta
  266. uses: docker/metadata-action@v5
  267. with:
  268. images: ${{ env.FULL_IMAGE_NAME }}
  269. tags: |
  270. type=ref,event=branch
  271. type=ref,event=tag
  272. type=sha,prefix=git-
  273. type=semver,pattern={{version}}
  274. type=semver,pattern={{major}}.{{minor}}
  275. flavor: |
  276. latest=${{ github.ref == 'refs/heads/main' }}
  277. - name: Create manifest list and push
  278. working-directory: /tmp/digests
  279. run: |
  280. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  281. $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
  282. - name: Inspect image
  283. run: |
  284. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}
  285. merge-cuda-images:
  286. runs-on: ubuntu-latest
  287. needs: [ build-cuda-image ]
  288. steps:
  289. - name: Download digests
  290. uses: actions/download-artifact@v4
  291. with:
  292. pattern: digests-cuda-*
  293. path: /tmp/digests
  294. merge-multiple: true
  295. - name: Set up Docker Buildx
  296. uses: docker/setup-buildx-action@v3
  297. - name: Log in to the Container registry
  298. uses: docker/login-action@v3
  299. with:
  300. registry: ${{ env.REGISTRY }}
  301. username: ${{ github.actor }}
  302. password: ${{ secrets.GITHUB_TOKEN }}
  303. - name: Extract metadata for Docker images (default latest tag)
  304. id: meta
  305. uses: docker/metadata-action@v5
  306. with:
  307. images: ${{ env.FULL_IMAGE_NAME }}
  308. tags: |
  309. type=ref,event=branch
  310. type=ref,event=tag
  311. type=sha,prefix=git-
  312. type=semver,pattern={{version}}
  313. type=semver,pattern={{major}}.{{minor}}
  314. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=cuda
  315. flavor: |
  316. latest=${{ github.ref == 'refs/heads/main' }}
  317. suffix=-cuda,onlatest=true
  318. - name: Create manifest list and push
  319. working-directory: /tmp/digests
  320. run: |
  321. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  322. $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
  323. - name: Inspect image
  324. run: |
  325. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}
  326. merge-ollama-images:
  327. runs-on: ubuntu-latest
  328. needs: [ build-ollama-image ]
  329. steps:
  330. - name: Download digests
  331. uses: actions/download-artifact@v4
  332. with:
  333. pattern: digests-ollama-*
  334. path: /tmp/digests
  335. merge-multiple: true
  336. - name: Set up Docker Buildx
  337. uses: docker/setup-buildx-action@v3
  338. - name: Log in to the Container registry
  339. uses: docker/login-action@v3
  340. with:
  341. registry: ${{ env.REGISTRY }}
  342. username: ${{ github.actor }}
  343. password: ${{ secrets.GITHUB_TOKEN }}
  344. - name: Extract metadata for Docker images (default ollama tag)
  345. id: meta
  346. uses: docker/metadata-action@v5
  347. with:
  348. images: ${{ env.FULL_IMAGE_NAME }}
  349. tags: |
  350. type=ref,event=branch
  351. type=ref,event=tag
  352. type=sha,prefix=git-
  353. type=semver,pattern={{version}}
  354. type=semver,pattern={{major}}.{{minor}}
  355. type=raw,enable=${{ github.ref == 'refs/heads/main' }},prefix=,suffix=,value=ollama
  356. flavor: |
  357. latest=${{ github.ref == 'refs/heads/main' }}
  358. suffix=-ollama,onlatest=true
  359. - name: Create manifest list and push
  360. working-directory: /tmp/digests
  361. run: |
  362. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  363. $(printf '${{ env.FULL_IMAGE_NAME }}@sha256:%s ' *)
  364. - name: Inspect image
  365. run: |
  366. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ steps.meta.outputs.version }}